How to set up PYTHONPATH and project layout
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
.
![Admin](/assets/logo_square_200-5d0d61d6853298bd2a4fe063103715b4daf2819fc21225efa21dfb93e61952ea.png)
Admin
Updated on June 19, 2022Comments
-
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 about 12 yearsAfter I typed in "export PYTHONPATH=$PYTHONPATH:/Users/myname/python_stuff/projects/GothonWeb/gothonweb", I entered it in just like you said and it worked. Why does it only work like that?
-
Admin about 12 yearsYeah yeah! I saw it after I asked it. I guess I forgot that lesson. Thanks.
-
Weboide over 11 yearsDoes PYTHONPATH need to include ~/path/to/gothonweb/ for this solution to work?