8

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
4
  • 2
    Make sure, that the parent directory of foldername is in your python path (environment variable PYTHONPATH). Commented Feb 1, 2012 at 19:15
  • 1
    you can't use flags - It should be foldername.flags. Commented Feb 1, 2012 at 19:15
  • 1
    Make sure your foldername is not also main. And main.py should not be inside the same folder. Commented Feb 1, 2012 at 19:30
  • @Anony-Mousse: main.py may be inside the same folder. Python 2.7 even supports it explicitly with __main__.py name. Commented Feb 1, 2012 at 19:38

4 Answers 4

7

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.

Sign up to request clarification or add additional context in comments.

4 Comments

Running from the parent folder of main gives: /usr/bin/python2: No module named foldername
@jck: move one directory up i.e., use the parent folder of folder "foldername".
Sorry. running from the home directory(Parent folder of foldername) also gives the same error.
@jck: I've added the directory layout. It should be clear what the "Parent Folder" is.
5

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.

2 Comments

It is not PYTHONPATH issue. It is python path issue (sys.path). My PYTHONPATH environment variable is always empty. python implicitly adds script's directory to sys.path. Sometimes It is called sys.path[0] initialization for scripts.
Wow, this solved my mystery problem! I did not realize the parent path must also be in the PYTHONPATH! So the same script sitting a level above what I want to import runs fine. But once I move it into some sub-directory, it won't import even with the right sys.path - until I add the parent dir to sys.path that is. Thanks!
3

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

Comments

0

If you want to import the a module in python3 simply go to the root folder

python3 -mModuleName

Make sure you do not remove the -m before the module name and then this you can import this module anywhere inside the project directory.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.