简体   繁体   English

Python 子进程找不到 Pythonpath 模块

[英]Python subprocess can't find Pythonpath module

I am trying to use subprocess.run(['python3.9', "scripts/example.py"], check=True) .我正在尝试使用subprocess.run(['python3.9', "scripts/example.py"], check=True)

example.py uses a module, that I have added to the PYTHONPATH. example.py使用我已添加到 PYTHONPATH 的模块。 However, whenever I run the above line, the module is not found.但是,每当我运行上面的行时,都找不到该模块。

The confusing part for me is, that printing sys.path inside of example.py I do see the path to my module.令我困惑的部分是,在example.py中打印sys.path我确实看到了我的模块的路径。 But when I am running os.system("which python") or os.system("echo $PYTHONPATH") inside example.py, it returns/prints nothing.但是当我在 example.py 中运行os.system("which python")os.system("echo $PYTHONPATH")时,它不返回/打印任何内容。

Looks like you need to check the doc forthe env parameter of subprocess.run and set it appropriately.看起来您需要检查 subprocess.runsubprocess.run env参数的文档并适当地设置它。

Side note: typically you would want to use the exact same Python interpreter for the sub-process call, so you would write: subprocess.run([sys.executable, 'scripts/example.py'], ...) , unless of course you really do want 'python3.9' explicitly and nothing else (which would be surprising).旁注:通常您希望对子流程调用使用完全相同的 Python 解释器,因此您可以编写: subprocess.run([sys.executable, 'scripts/example.py'], ...) ,除非当然,您确实确实明确想要'python3.9'而不是其他任何东西(这会令人惊讶)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM