我在StackOverflow上寻找答案,但他们中的大多数并不能解决我的全部任务。
我有一个名为cellprofiler的软件,它与自己的GUI一起工作,并在Ubuntu上的conda环境中运行。
我想用另一个python脚本在外部自动运行这个cellprofiler。
我的脚步:
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)然后在我的python脚本中这样做:
processCP = subprocess.Popen(env_activate, stdout=subprocess.PIPE)
processCP.wait()但是这导致它在系统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有人知道为什么会这样吗?
更新:是我需要的python2.7解释器,它已经在conda环境中了。如果在终端中调用它,Cellprofiler就可以正常工作,如:
source activate cellprofiler
cellprofiler发布于 2018-11-19 12:01:41
回答我自己的问题:它有助于阅读Running subprocess within different virtualenv with python
因此,我们需要使用subprocess.Popen来创建一个进程。另一个窍门是在bash脚本中使用the ban #!/bin/bash\n。毕竟,它是以下列方式创建的:
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()然后经过:
processCP = subprocess.Popen(env_activate, stdout=subprocess.PIPE, stderr=subprocess.PIPE)发布于 2018-11-16 13:26:44
在终点站运行
pip install cellprofilerhttps://stackoverflow.com/questions/53338564
复制相似问题