How to set up PYTHONPATH and project layout

16,827

Solution 1

Run the application like this:

$ cd ~/path/to/gothonweb/
python bin/app.py

However, if you did this:

$ cd bin/   # WRONG! WRONG! WRONG!
$ python app.py  # WRONG! WRONG! WRONG!

A lot of users tend to do this mistake, and here is a nice explanation by Zed Shaw, on why not to run your app like this!

Then you are doing it wrong. In all python projects you do not cd into a lower directory to run things. You stay at the top and run everything from there so that all of the system can access all the modules and files.

Solution 2

Alright to add to your python path in python, the easiest way to do it is at the TOP of your module.

import sys
sys.path.append(stringofpath)

You can recall that for each path you want to add.

stringofpath is the /path/to/dir/ where dir is the directory your project sits in. NOT your project directory. Then use the normal dot notation starting from your project's directory to keep descending into all your sub directories.

In your case your stringofpath is 'User/myname/python_stuff'

As you advance you'll have to spend some serious time learning about your python path, there are a lot of things that are hard to find. And environment variables--in linux at least-- are permanently set in /etc/environment.

Share:
16,827
Admin
Author by

Admin

Updated on June 19, 2022

Comments

  • Admin
    Admin about 2 years

    So I've been working the last exercise of Learn Python the Hard Way and it states that before I run the program Ive been working on, you have to set the Pythonpath environment variable like this:

    export PYTHONPATH=$PYTHONPATH:.
    

    I've done this, and after it wasn't working (Im assuming thats why when I try to run my program, I get an ImportError), I did some research on pythonpath. It's essentially says that Pythonpath is to find where to look for modules to import. So I set Pythonpath to the actual location of the module I'm trying to import and still to no avail.

    Here's my directory:

    - User/_myname_/python_stuff/projects/GothonWeb
        - bin/
            - __init__.py
            - app.py
        - gothonweb/
            - __init__.py
            - maps.py
        - templates (web.py)
            - game.html
            - index.html
            - layout.html
        - so forth and so on.
    

    Im trying to run the app.py, which tries to import the main.py module:

    from gothonweb import maps
    

    But I still get this error:

    Traceback (most recent call last):
    File "python_stuff/projects/GothonWeb/bin/app.py", line 2, in <module>
    from gothonweb import maps
    ImportError: No module named gothonweb
    

    Anyone know whats up? Oh, some other details. Running on Python 2.7 on Mac Os X Lion, if that helps any.

  • Admin
    Admin about 12 years
    After I typed in "export PYTHONPATH=$PYTHONPATH:/Users/myname/python_stuff/projects/G‌​othonWeb/gothonweb", I entered it in just like you said and it worked. Why does it only work like that?
  • Admin
    Admin about 12 years
    Yeah yeah! I saw it after I asked it. I guess I forgot that lesson. Thanks.
  • Weboide
    Weboide over 11 years
    Does PYTHONPATH need to include ~/path/to/gothonweb/ for this solution to work?