python import from the sub-directories
Question: I have the following directories:

    
    
    |-- project
    |   |-- __init__.py
    |   |-- proj1
    |   |   |-- file.py
    |   |   |-- file.py~
    |   |   `-- __init__.py
    |   `-- proj2
    |       |-- call.py
    |       |-- call.py~
    |       `-- __init__.py
    

And the call.py has:

    
    
    from proj1.file import hello
    
    hello('nishant')
    

And the file.py has.

    
    
    def hello(arg):
        print 'hello ,' + arg
    

When i am trying to call outside the project directory `python
project/proj2/call.py` i get the import Error: No module named proj1.file

Any Idea ..?


Answer: This is because imports are relative to paths in your PATH environment
variable (or `sys.path` in python). By default this var only contains the
current directory, which is the directory you run from.

Run you project from within the `project` directory: `python proj2/call.py`

Alternatively, you can append your project root to `sys.path` before doing the
imports:

    
    
    import sys
    sys.path.append(YOUR_PROJECT_ROOT)
    

