Python file does not recognize imported functions from other Python files

Viewed 34

I'm trying to make my code more organized by creating separate files for various functions. I'm testing out printing scenes of dialogue with a text scrolling code, and instead of keeping every line of dialogue in one file, I have moved it to a scene0.py file in the folder scenes. In that file, I define the function playscene0.

In a different folder called scenetext, I have a file called textstuff.py which contains the defined functions print_italics (which prints italics text), texttype (which makes each character type out individually like some video games do), and textscroll (which reads lists of dialog and formats it, plus timings with time.sleep).

That aforementioned scene0.py file uses from scenetext.textstuff import * to import the above functions, plus additional variables that go along with them (unique to that file). VSCode seems to initially recognize that all the functions being used in the playscene0 function have been successfully imported.

In my importtest file:

from scenes.scene0 import playscene0

playscene0()

This gives me:

NameError: name 'print_italics' is not defined

Though it very much is defined in the file it is derived from.

I've already looked into circular imports and how they cause problems, but that doesn't seem to be the cause here. I've tried importing both time and everything from scenetext.textstuff in the importtest file directly, but the problem persists.

Any suggestions are welcome, thank you

EDIT: After adding from scenetext.textstuff import print_italics to the importtest file, it gives me a different error upon running: ImportError: cannot import name 'print_italics' from 'scenetext.textstuff' (/Users/[me]/vscode/VOYAGE/scenetext/textstuff.py)

Not sure what causes this.

2 Answers

You need to add an import to the importtest file - from scenetext.textstuff import print_italics, so that the function becomes "visible".

You should import the textstuff functions BEFORE you import the playscene function that uses them

from scenetect.textstuff import print_italics
from scenes.scene0 import playscene0
playscene0()
Related