I’m getting started with Python and Google App Engine. One of the first things I had to figure out was how Python handles paths and module names.
You can see what paths python is looking on with this command sequence:
$ python
['', '/opt/local/var/macports/software/python25/2.5.2_2+darwin_8/opt/local/lib/python25.zip', '/opt/local/var/macports/software/python25/2.5.2_2+darwin_8/opt/local/lib/python2.5', '/opt/local/var/macports/software/python25/2.5.2_2+darwin_8/opt/local/lib/python2.5/plat-darwin', '/opt/local/var/macports/software/python25/2.5.2_2+darwin_8/opt/local/lib/python2.5/plat-mac', '/opt/local/var/macports/software/python25/2.5.2_2+darwin_8/opt/local/lib/python2.5/plat-mac/lib-scriptpackages', '/opt/local/var/macports/software/python25/2.5.2_2+darwin_8/opt/local/lib/python2.5/lib-tk', '/opt/local/var/macports/software/python25/2.5.2_2+darwin_8/opt/local/lib/python2.5/lib-dynload', '/opt/local/var/macports/software/python25/2.5.2_2+darwin_8/opt/local/lib/python2.5/site-packages']
Python 2.5.2 (r252:60911, Jun 1 2008, 16:53:35)
[GCC 4.0.1 (Apple Computer, Inc. build 5367)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.path
>>> exit()
Inside a python script Modules are included with this syntax:
from google.appengine.ext.bulkload import constants
All that means is that python is going to look on the paths above for /google/appengine/ext/bulkload/constants.py
In my case, the script I was trying to run sets up the paths for a secondary main script with some code like this:
EXTRA_PATHS = [
DIR_PATH,
os.path.join(GOOGLE_PATH, 'lib', 'django'),
os.path.join(GOOGLE_PATH, 'lib', 'webob'),
os.path.join(GOOGLE_PATH, 'lib', 'yaml', 'lib'),
]
if __name__ == '__main__':
sys.path = EXTRA_PATHS + sys.path
script_path = os.path.join(DIR_PATH, BULKLOAD_CLIENT_PATH)
execfile(script_path, globals())
So, you can see here that sys.path is assignable and retained when calling other scripts.
I also learned that you can set the $PYTHONPATH environment variable to a list of paths, just like the normal $PATH variable. However, unlike Windows, you have “export” the environment variable.
$ PYTHONPATH=$PYTHONPATH:/include/this:/and/this
export $PYTHONPATH
The export command takes the local shell variable and makes it visible as a global variable (in this case to Python).
Webmentions
[…] Had some trouble with Python paths. Just beginner […]