I know this question has been asked many times here and I'v probably read most of the answers (including this and that) as well as the python documentation but still can not find an answer to my very simple import problem. It's so simple that I must miss something stupid but I don't see it yet. I have setup the following structure:
myproject
myscript.py
MyPackage
__init.py__
mymodule.py
I just want to load mymodule.p
y from myscript.py
(or the commandline python interpreter which should be the same).
myscript.py
contains:
#!/usr/bin/python
import MyPackage
__init.py__
contains:
from . import mymodule
mymodule.py
contains
#!/usr/bin/python
def myfunction():
print "mymessage"
My goal is to call myfunction
from myscript.py
but if I try to call the module I get
$python myscript.py
Traceback (most recent call last):
File "myscript.py", line 2, in <module>
import MyPackage
ImportError: No module named MyPackage
PYTHONPATH
environment variable to the myproject
directory as well as to .
and to both.__init.py__
blankmyproject
directoryI tried the following import statements:
from MyPackage import mymodule
import MyPackage.mymodule
import MyPackage.mymodule as module
all without success (same error message).
If I put mymodule.py
in the project directory without using a package, import works fine. But I don't see why the import from the subpackages is not working.
Any idea how I can get that to work?
Thanks for help!
The __init__.py file makes Python treat directories containing it as modules. Furthermore, this is the first file to be loaded in a module, so you can use it to execute code that you want to run each time a module is loaded, or specify the submodules to be exported.
The PYTHONPATH is the environment variable that contains the path of the directories that Python searches to import the packages. Therefore, if we add the subdirectory to the PYTHONPATH , Python will first look at the directories in PYTHONPATH and import it from there.
The __init__.py files are required to make Python treat directories containing the file as packages. This prevents directories with a common name, such as string , unintentionally hiding valid modules that occur later on the module search path.
path looking for the package subdirectory. The __init__.py files are required to make Python treat directories containing the file as packages. This prevents directories with a common name, such as string , unintentionally hiding valid modules that occur later on the module search path.
While editing the formatting of your post, I noticed you're calling your file __init.py_
. That causes python to not recognize your MyPackage
directory as a package, hence the ImportError: No module named MyPackage
.
It should instead be __init__.py
(Name __init__
, extension .py
). Then it will work, your project structure and import statements are otherwise correct.
One minor point though: You should also use lower_underscore
style for naming your package. Packages are modules as well in Python, and they should follow the same naming conventions. See PEP8 for details on recommended style and naming conventions. This is just a convention though, it has nothing to do with your problem.
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