Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 modules, and not those of my project when doing an import. How can I get my modules imported?

Example:

first_script.py

import subprocess
from my_project.any_module import any_module

def __init__(self):
    subprocess.call(['python', 'path/to/exec/second_script.py'])

second_script.py

from my_project.any_module import any_module

def __init__(self):
    print any_module.argument

In the first script, import any_module works, in the second it does not.

Any ideas? Thx.

like image 933
Francesc Avatar asked Sep 05 '25 03:09

Francesc


1 Answers

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.

like image 92
Erik Cederstrand Avatar answered Sep 07 '25 23:09

Erik Cederstrand