Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python unable to import custom module despite having __init__.py

Tags:

python

I have a folder with a __init__.py

File __init__.py:

#!/usr/bin/python2
flags="test"

File main.py:

#!/usr/bin/python2
import foldername

def main():
    print foldername.flags

if __name__ == '__main__':
main()

Now, when I run ./main.py (from inside the folder), I get the error

ImportError: No module named foldername
like image 409
jck Avatar asked Feb 01 '12 19:02

jck


People also ask

Why can't I import modules in Python?

This is caused by the fact that the version of Python you're running your script with is not configured to search for modules where you've installed them. This happens when you use the wrong installation of pip to install packages.

What is __ init __ py in Python?

The __init__.py files are required to make Python treat the directories as containing packages; this is done to prevent directories with a common name, such as string, from unintentionally hiding valid modules that occur later on the module search path.

Why is __ init __ py module used in Python?

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.

What should __ init __ py contain?

The gist is that __init__.py is used to indicate that a directory is a python package. (A quick note about packages vs. modules from the python docs: "From a file system perspective, packages are directories and modules are files.").


3 Answers

Run from the parent folder for foldername:

    $ python -m foldername.main

If you rename main.py to __main__.py then you could run it as (since Python 2.7):

    $ python -m foldername

python -m adds implicitly current directory to your python path (sys.path).

Parent Folder/
└── foldername
    ├── __init__.py
    │   #    flags="test"
    └── __main__.py
        #    import foldername
        #   
        #    def main():
        #        print foldername.flags
        #   
        #    if __name__=="__main__":
        #        main()

If the parent directory for foldername is in your python path then you could run the above commands from any directory.

like image 133
jfs Avatar answered Nov 13 '22 00:11

jfs


PYTHONPATH issue. Make sure that "foldername" is available in your path. If you are running it from inside "foldername" it might not be available. Try running from the parent of "foldername".

Here is a question about finding your PYTHONPATH.

like image 28
istruble Avatar answered Nov 13 '22 02:11

istruble


Make sure your layout is like this:

./folder/__init__.py
./main.py

and there is not file named folder.py!

Change to the parent folder, so that ls folder/__init__.py works.

Next try running python -c "import folder".

like image 30
Has QUIT--Anony-Mousse Avatar answered Nov 13 '22 00:11

Has QUIT--Anony-Mousse