从控制台中的.py运行subprocess.Popen()的Python问题 [英] Python issue running subprocess.Popen() from a .py in console

查看:267
本文介绍了从控制台中的.py运行subprocess.Popen()的Python问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在运行一个使用subprocess.Popen()来执行某些任务的程序(我无法使用os.system()).尽管可以通过在控制台中写入fg来恢复该程序,但是该程序可以在IDE中正确运行(我将在后面进行解释),但是我可以从控制台中运行该程序.

I am running a program which uses subprocess.Popen() in order to do some tasks (I cannot use os.system() for them). The program, which correctly runs within an IDE (I will explain later) stops when I run it from the console, although I can resume it by writing fg in the console.

代码如下(这是一段代码,完整的代码实现了发生相同问题的相似任务)...

The code is the following (this is a piece of code,the full code implements similar tasks to which the same problem occurs)...

import subprocess


p,o = subprocess.Popen(['/bin/bash', '-c', '-i', 'which python3.5'], stdout=subprocess.PIPE).communicate()
p = p.decode('ascii')
print(p)


print('Installing pysamstats...')
subprocess.Popen(['/bin/bash', '-c', '-i', 'conda install -y -c bioconda     pysamstats']).communicate()
print('OK')

如果代码是从IDE运行的(我将PyCharm与anaconda一起用作解释器),则会发生输出,并且所有脚本都可以平稳运行,但每次调用Popen时都会出现以下消息...

If the code is run from an IDE (I use PyCharm with anaconda as interpreter) the output occurs and all the script runs smoothly, with the exception that the following message appears each time Popen is called...

bash: cannot set terminal process group (3556): Inappropriate ioctl for device
bash: no job control in this shell

除我所说的错误"外,代码均已正确执行.

With the exception of the "error", as I have said, the code is correctly executed.

但是,如果我从控制台运行.py文件...

However, if I run the .py file from the console...

python3 '/DIRTOFILE/ZZZ.py' 

出现以下消息...

/home/labcombio1/anaconda3/bin/python3.5

Installing pysamstats...

[1]+  Stopped                 python3 '/media/labcombio1/ZZZ.py'

即,执行了第一个命令,并且正确打印了输出,而停止了第二个命令.如果我使用fg恢复该命令,它将正常工作.如果没有运行第一个Popen命令,即仅运行第二个Popen命令,也会发生同样的情况.

That is, the first command is executed, and the output is correctly printed, whereas the second command is stopped. If I resume the command with fg, it works fine. The same happens if the first Popen command is not run, that is, only with the second one.

我尝试了shell=True,删除了.communicate(),添加了stdout=subprocess.PIPE,但是这些都不能解决控制台上的"Stopped"行.其他命令产生相同的结果.

I have tried shell=True, removing .communicate(), adding stdout=subprocess.PIPE, and none of these things solves the "Stopped" line at the console. Other commands yield the same results.

最后,我尝试运行以下命令:

Finally, I have tried running the following commands:

subprocess.Popen('conda install -y -c bioconda pysamstats', shell=True).communicate()

subprocess.Popen(['/bin/bash', '-c', 'conda install -y -c bioconda     pysamstats']).communicate()

尽管它们都可以在Pycharm中正常工作(它们甚至不提示bash: cannot set terminal process group (3556): Inappropriate ioctl for device),但是终端仍然会失败,并显示以下消息:

And although they both work correctly in Pycharm (they don't even prompt bash: cannot set terminal process group (3556): Inappropriate ioctl for device), the terminal still fails, with the following message:

/bin/sh: 1: conda: not found

我对子流程模块了解甚少,尽管阅读了其他问题以及与此有关的一些页面,但我仍无法解决该问题.

I know little about subprocess module, and despite reading other questions and some pages taking about it, I have been unable to solve that problem.

提前谢谢

推荐答案

第一个问题:

bash: cannot set terminal process group (3556): Inappropriate ioctl for device
bash: no job control in this shell

bash解释器中删除-i标志.

-i如果存在-i选项,则该外壳是交互式的.

-i If the -i option is present, the shell is interactive.

示例:

p, o = subprocess.Popen(['/bin/bash', '-c', 'which python3.5'], stdout=subprocess.PIPE).communicate()

第二个问题:

/bin/sh: 1: conda: not found

conda路径未在您的环境中导出.

The condapath is not exported in your environment.

可能的解决方案

1)将其加载到您的IDE中.

1) Load it in your IDE.

2)使用可执行文件的完整路径.

2) Use the full path to the executable.

subprocess.Popen(['/path/to/conda/conda', 'install', '-y', '-c', 'bioconda', 'pysamstats']).communicate()

3)将env传递到Popen.

subprocess.Popen(['conda', 'install', '-y', '-c', 'bioconda', 'pysamstats'], env={"PATH": "/path/to/conda"}).communicate()

这篇关于从控制台中的.py运行subprocess.Popen()的Python问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆