python + django: unable to find module with Popen
I use subprocess.Popen in one of the my views:
path = os.path.join(os.path.dirname(__file__), 'foo/bar.py')
subp开发者_StackOverflow中文版rocess.Popen(["python",path])
In my wsgi file, I have
import os
import sys
ppath = '/home/socialsense/ss/src'
if ppath not in sys.path:
sys.path.append(ppath)
os.environ['DJANGO_SETTINGS_MODULE'] = 'ss.settings'
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()
and under src
i have ss
, my django project.
But when I check my log file, bar.py encountered an error, ImportError: No module named ss.discovery.models
. Now it seems that the module ss
itself is not in sys.path
when using Popen
...
anything i did wrongly here?
It will only be in sys.path
for the current Python instance. To get it for another, use the env
argument to Popen
with os.pathsep
:
import subprocess
import os
import sys
subprocess.Popen(["python",path], env = {'PYTHONPATH': os.pathsep.join(sys.path)})
You should really look into the multiprocessing
module for running multiple instances of Python.
Edit: @Graham pointed out in a comment that you might want to run this external script with a different version of Python than the one you're calling it from. That sounds unlikely to me, but if so, you'd need most of PYTHONPATH
to be different for it to work, so you'd need to just add /home/socialsense/ss/src
.
you need to add
/home/socialsense/ssto the python path as well .
精彩评论