Question:I am writing a simple pygame name generator for a little project and am trying to import a function I made but it says it does not exist. You can see: that my from and imports are correct. The script that runs is fantasy name generator yet this error (in the screenshot) is produced. Any help would be appreciated, thanks.
My folder structure:
generate_name.pyis in a deeply nested package alongside the other module. Python 3 uses absolute imports by default, relative imports must be performed explicitly.
The relative import you’d want here is (note leading
.indicating a relative name):
The absolute import path would match the same one you used for
from .prefix_code_gen import prefix_code_gen
generate_nameif you wanted to use it:
I’m inclined to favor the relative import, since it will continue to work even if a refactoring changes the absolute path, as long as the two modules are still installed under the same package directory.
from functional.generators.prefix_code_gen import prefix_code_gen
If you have better answer, please add a comment about this, thank you!