Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I use subprocess.Popen in one of the my views:

path = os.path.join(os.path.dirname(__file__), 'foo/bar.py')
subprocess.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.

Strictly speaking your shouldn't add the whole of sys.path into PYTHONPATH just in case the WSGI application wasn't running with the default Python version but an alternate one. Ie. 'python' is Python 2.6 but mod_wsgi was compiled for Python 2.7. – Graham Dumpleton Aug 11, 2011 at 10:39

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.