Currently trying to work in Python3 and use absolute imports to import one module into another but I get the error ModuleNotFoundError: No module named '__main__.moduleB'; '__main__' is not a package
. Consider this project structure:
proj __init__.py3 (empty) moduleA.py3 moduleB.py3
moduleA.py3
from .moduleB import ModuleB ModuleB.hello()
moduleB.py3
class ModuleB: def hello(): print("hello world")
Then running python3 moduleA.py3
gives the error. What needs to be changed here?
__main__ is the name of the environment where top-level code is run. “Top-level code” is the first user-specified Python module that starts running. It's “top-level” because it imports all other modules that the program needs. Sometimes “top-level code” is called an entry point to the application.
File size That can be caused by too many fields or records in the file, too many columns, or too many rows. The import error can be caused by limits set by the program using the file or the amount of available memory on the system.
.moduleB
is a relative import. Relative only works when the parent module is imported or loaded first. That means you need to have proj
imported somewhere in your current runtime environment. When you are are using command python3 moduleA.py3
, it is getting no chance to import parent module. You can:
from proj.moduleB import moduleB
ORrun.py
, to invoke from proj import moduleA
Good luck with your journey to the awesome land of Python.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With