Python 2.7: Running a python file within a package
Question: I have three folders:

    
    
    /main
        __init__.py
        main.py
        /p1
            __init__.py
            p1.py
        /p2
            __init__.py
            p2.py
    

However some parts of `p1` depend on `p2` and the way in which I import `p2`
from `p1` is by using an absolute package and not a relative one:

    
    
    from main.p2.p2 import p2class
    

The problem arises when I want to run `p1` individually by typing in:

    
    
    cd main\p1 
    python p1.py
    

However, it says:

    
    
    ImportError: No module named main.p2.p2
    

How can I run it individually?

Sources for the files are shown below:

`main\p1\p1.py`:

    
    
    from main.p2.p2 import p2print
    
    def p1print():
        print "p1 printing"
    
    if __name__ == "__main__":
        p2print()
        p1print()
    

`main\p2\p2.py`:

    
    
    def p2print():
        print "p2 printing"
    
    if __name__ == "__main__":
        p2print()
    


Answer: From outside of the /main directory, run it using `python -m`.

    
    
    ➜ python -m main.p1.p1
    p2 printing
    p1 printing
    

