简体   繁体   English

如何在 Python 中调试到 subprocess.call()?

[英]How to debug into subprocess.call() in Python?

I am trying to understand a project's source code these days.这些天我试图理解一个项目的源代码。 I run the project, line by line, everything works fine until this line:我一行一行地运行项目,一切正常,直到这一行:

res = subprocess.call(command, env=os.environ)

I checked the variable "command" and realized that this function just throw a command to another python script and try to execute it in a subprocess.我检查了变量“command”并意识到这个函数只是向另一个python脚本抛出一个命令并尝试在子进程中执行它。 So I jumped out of Eclipse, and tried to execute the command through Terminal while under the same directory.于是跳出Eclipse,尝试在同一目录下通过终端执行命令。

Now this is what I got:现在这就是我得到的:

Traceback (most recent call last):
  File "/home/elderry/Projects/git/tahoe-lafs/support/bin/tahoe", line 6, in <module>
    from pkg_resources import load_entry_point
  File "/usr/lib/python2.7/site-packages/pkg_resources.py", line 2850, in <module>
    working_set.require(__requires__)
  File "/usr/lib/python2.7/site-packages/pkg_resources.py", line 696, in require
    needed = self.resolve(parse_requirements(requirements))
  File "/usr/lib/python2.7/site-packages/pkg_resources.py", line 594, in resolve
    raise DistributionNotFound(req)
pkg_resources.DistributionNotFound: allmydata-tahoe==1.10.0.post27

Then I completely lost my direction, where did the subprocess continue to run?然后我就完全迷失了方向,子进程哪里继续运行了? Why did the script work well in program but not in Terminal?为什么脚本在程序中运行良好,但在终端中不起作用? Since that script is also included in the project, with some hope I set some break points in it in Eclipse, which didn't catch anything.由于该脚本也包含在项目中,因此我希望我在 Eclipse 中设置了一些断点,但没有捕获任何内容。 Is there any way to debug into the subprocess, not dive into the code of subprocess module's code itself?有没有办法调试到子流程中,而不是深入到子流程模块代码本身的代码中?

I guess your main project alters PYTHONPAH ( sys.path ).我猜你的主要项目改变了PYTHONPAH ( sys.path )。 Look in os.environ of your project and try to run second script with this environment.查看项目的os.environ并尝试在此环境中运行第二个脚本。

I have the same problem, could you tell me how to deal with it? 我有同样的问题,您能告诉我如何处理吗? Thank you. 谢谢。 My code is subprocess.call(command,shell=True,env=dict(os.environ, **env) I want to debug the subprocess and see the output in it. I try to use Pycharm to fix the problem but I failed. 我的代码是subprocess.call(command,shell = True,env = dict(os.environ,** env)我想调试子流程并查看其中的输出。我尝试使用Pycharm修复问题,但失败了。

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

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