I am looking how to import modules by setting the pythonpath.
To check the paths python is looking at:
import sys
print "modules will be searched in the following paths:"
for path in sys.path:
print path
print "python version: " + sys.version
I added some extra paths in PYTHONPATH environment variable (windows gui), but when executing the code above as a cgi file in apache (in the browser), it does not show my added paths…(and indeed, it will not load modules from the missing paths)
When I execute the code with python locally, all paths are shown (so that’s ok).
#!C:\tools\programming\Python27\python.exe
import cgi
import cgitb
cgitb.enable()
print "Content-type: text/html"
print
import sys
#import timeOperations
for path in sys.path:
print path + "<br>"
print "python version: " + sys.version
print "<html>"
print "<center>Lode doet het weer!</center>"
print "</html>"
I did a computer reboot, no succes. There is also only one python version installed.
Why gives the PYTHONPATH a different value? I don’t know.
Any help is appreciated!
Lode
I personally don’t like adding things to the PYTHONPATH through an environment variable for this very reason. I suspect that the reason is that your PYTHONPATH environment variable has a different scope than the one Apache is using. For example, your variable might be local to your user, while Apache runs with just the system environment variables.
I find it more reliable to bypass environment variables completely by adding a “.pth” file to your site-packages directory. Execute the following code from the python interpreter:
This will print the location of your “site-packages” directory. Visit that directory (from the command-line or Windows Explorer) and create a file in it called “MyPythonPath.pth” (or something else with the extension “.pth”). In it, include each value you wanted to add to your PYTHONPATH on separate lines:
Next time you run Python, these folders will be added to your path, no matter where you run it—as long as you are always using the same Python interpreter.