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
Cbhihe
  • 511
  • 9
  • 24
jck
  • 1,910
  • 4
  • 18
  • 25

4 Answers4

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.

Mehant Kammakomati
  • 852
  • 1
  • 8
  • 25
jfs
  • 399,953
  • 195
  • 994
  • 1,670
  • 1
    Running from the parent folder of main gives: /usr/bin/python2: No module named foldername – jck Feb 01 '12 at 19:23
  • @jck: move one directory up i.e., use the parent folder of *folder* "foldername". – jfs Feb 01 '12 at 19:25
  • Sorry. running from the home directory(Parent folder of foldername) also gives the same error. – jck Feb 01 '12 at 19:27
  • @jck: I've added the directory layout. It should be clear what the "Parent Folder" is. – jfs Feb 01 '12 at 19:53
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.

Community
  • 1
  • 1
istruble
  • 13,363
  • 2
  • 47
  • 52
  • 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. – jfs Feb 01 '12 at 20:01
  • 1
    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! – Evgen May 21 '16 at 00:28
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".

Has QUIT--Anony-Mousse
  • 76,138
  • 12
  • 138
  • 194
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.

Sundeep Pidugu
  • 2,377
  • 2
  • 21
  • 43