0

I am attempting to execute a function within a Python script as a subprocess using its own interpreter, specified in the subprocess.run() call. This interpreter may include packages not present in my local Python environment. When I debug the file, errors occur due to these missing libraries in my local python instead of the one linked in the command. Am I misunderstanding something?

try:
    proc = subprocess.run(
        [
            r'C:\Users\user\AppData\Local\Programs\Python\Python311\python.exe',
            r'C:\subprocess_python\my_file_subprocess.py',
            str(self.x), str(self.y), str(self.z)
        ],
        capture_output=True,
        check=True
    )
except subprocess.CalledProcessError as proc_err:
    print("An exception occurred in the subprocess: \n ", proc_err)
    print("stdout : \n", proc_err.stdout.decode())
    print("stderr : \n", proc_err.stderr.decode())
    exit(1)
6
  • Can you explain what you mean by "local interpreter" vs. "linked interpreter"? Those aren't standard terms, and it's unclear what you mean by them. Commented Mar 7, 2024 at 14:54
  • "When I debug the file, errors occur due to these missing libraries in my local python instead of the one linked in the command." I'd need to see proof of that. I suspect you are misinterpreting whatever error message you are getting, or that you aren't correctly identifying which packages my_file_subprocess.py requires. Commented Mar 7, 2024 at 15:05
  • @chepner: It's also possible they did something terrible like define a PYTHONPATH environment variable (which is used by all Python's that launch) rather than installing things properly per-interpreter. Commented Mar 7, 2024 at 20:32
  • @ShadowRanger by linked one the one whose path is in the subprocess\.run() command and by local i mean the local IDE where I am calling this subprocess.run() file Commented Mar 8, 2024 at 6:13
  • @chepner I am pretty confident about the packages , whenever I run the file my_file_subprocess.py , it works just fine with all the packages it needs are available in the system interpreter , but whenever I dubug while calling it form the subprocess , it only complains about the package missing which is not available in the local intepreter that I am using to call this subprocess.run command Commented Mar 8, 2024 at 6:15

1 Answer 1

0

Use sys.executable to determine what's the path of currently ran interpreter.

Sign up to request clarification or add additional context in comments.

2 Comments

sys.executable returns the path for the linked one but pkgutil.iter_modules() returns the path of the local interpreter
pkgutil.iter_modules() is a generator that list of ModuleInfo objects, not a single path

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.