Skip to content Skip to sidebar Skip to footer

Python Subprocess I Can Not Import Other Modules

I am trying to use a python subprocess to execute a script, which interests me to be able to do an import of my project. When running in another process, I only have the typical mo

Solution 1:

The my_project module needs to be in your PYTHONPATH so Python can find it. PYTHONPATH includes your current working directory, which is why it works in your first script when you run it. But when you invoke a subprocess, the cwd is different. So you need to add the path to my_project to PYTHONPATH and specify PYTHONPATH explicitly with the env argument to subprocess.call().

However, running Python code this way is awkward. Unless you have specific requirements that prevent this, I would suggest using the multiprocessing package instead to run Python code in a separate process.

Post a Comment for "Python Subprocess I Can Not Import Other Modules"