[英]run .sh script from python that initates another process
I was looking for an answer on StackOverflow, but most of them don't solve my entire task. 我一直在寻找关于StackOverflow的答案,但大多数都不能解决我的全部任务。
I have a software called cellprofiler, which works with its own GUI and runs in conda environment on Ubuntu. 我有一个名为cellprofiler的软件,该软件可使用其自己的GUI并在Ubuntu的conda环境中运行。
I want to automate running this cellprofiler externally with another python script. 我想使用另一个python脚本自动在外部运行此cellprofiler。
My steps: 我的步骤:
1) I create a bash script in python which runs the environment and software: 1)我在运行环境和软件的python中创建了一个bash脚本:
env_activate = 'path_to_sh/activate_env.sh'
with open(env_activate, 'w') as f:
f.write('#!/bin/sh\n')
f.write('. activate cellprofiler && cellprofiler')
f.close()
2) In my python script I then do: 2)然后在我的python脚本中执行以下操作:
processCP = subprocess.Popen(env_activate, stdout=subprocess.PIPE)
processCP.wait()
but this results in running it in a system python interpreter 3.5 rather than in conda environment 但这导致在系统python解释器3.5中而不是在conda环境中运行它
Traceback (most recent call last):
File "/home/**/.local/bin/cellprofiler", line 6, in <module>
from pkg_resources import load_entry_point
File "/home/**/.local/lib/python3.5/site-packages/pkg_resources/__init__.py", line 3126, in <module>
@_call_aside
File "/home/**/.local/lib/python3.5/site-packages/pkg_resources/__init__.py", line 3110, in _call_aside
f(*args, **kwargs)
File "/home/**/.local/lib/python3.5/site-packages/pkg_resources/__init__.py", line 3139, in _initialize_master_working_set
working_set = WorkingSet._build_master()
File "/home/**/.local/lib/python3.5/site-packages/pkg_resources/__init__.py", line 581, in _build_master
ws.require(__requires__)
File "/home/**/.local/lib/python3.5/site-packages/pkg_resources/__init__.py", line 898, in require
needed = self.resolve(parse_requirements(requirements))
File "/home/**/.local/lib/python3.5/site-packages/pkg_resources/__init__.py", line 784, in resolve
raise DistributionNotFound(req, requirers)
pkg_resources.DistributionNotFound: The 'CellProfiler' distribution was not found and is required by the application
Does anyone know why this happens? 有谁知道为什么会这样吗?
UPDATE: The python interpreter I need is python2.7 which is already in the conda environment. 更新:我需要的python解释器是已经在conda环境中的python2.7。 Cellprofiler works correctly if calling it in the terminal like: 如果在终端中调用Cellprofiler,则它可以正常工作,例如:
source activate cellprofiler
cellprofiler
在终端中运行
pip install cellprofiler
To answer my own question: it helped to read through Running subprocess within different virtualenv with python 回答我自己的问题:它有助于使用python读取不同virtualenv中的Running子进程
So, one needs to use subprocess.Popen
to create a process. 因此,需要使用subprocess.Popen
创建一个进程。 Another trick was to use she-ban #!/bin/bash\\n
in the bash script. 另一个技巧是在bash脚本中使用she-ban #!/bin/bash\\n
。 after all it was created as: 毕竟它被创建为:
env_activate = 'path_to/activate_env.sh'
with open(env_activate, 'w') as f:
f.write('#!/bin/bash\n')
f.write('source activate cellprofiler\n')
f.write('cellprofiler')
f.close()
and run by: 并运行:
processCP = subprocess.Popen(env_activate, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.