Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python cannot import module from subdirectory even with a file named __init.py__ in the directory

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.py 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

What I already tried:

  • I tried everything under OSX and Ubuntu Linux to reduce the possibility of a faulty python installation.
  • I set the PYTHONPATH environment variable to the myproject directory as well as to . and to both.
  • I left __init.py__ blank
  • I tried the import statements also from the python interpreter started from the myproject directory
  • I 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!

like image 611
mhelwig Avatar asked Dec 15 '13 10:12

mhelwig


People also ask

What does an __ init __ py file do?

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.

Does Python search subdirectories for import?

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.

Is __ init __ py necessary?

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.

What should __ init __ py contain?

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.


1 Answers

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.

like image 162
Lukas Graf Avatar answered Oct 29 '22 23:10

Lukas Graf