简体   繁体   English

在Pycharm Python virtualenv中使用subprocess.call()-如何为子流程停用venv

[英]Using subprocess.call() within Pycharm Python virtualenv — how to deactivate venv for subprocess

I am using Pycharm for Python development. 我正在使用Pycharm进行Python开发。 Code is running in Pycharm venv from where I want to call a bash script which activates and deactivates conda environments (this one: https://github.com/lykaust15/DeepSimulator ). 代码在Pycharm venv中运行,我想从该位置调用bash脚本来激活和停用conda环境(此脚本: https : //github.com/lykaust15/DeepSimulator )。

So I need to deactivate the Pycharm venv when calling the script via subprocess.call() , as far as I understand. 因此,据我所知,当通过subprocess.call subprocess.call()调用脚本时,我需要停用Pycharm venv。 Otherwise I get errors. 否则我会出错。 How do I do this? 我该怎么做呢?

My call is: 我的电话是:

        result = subprocess.call([deppsim_path, "-i", fasta, "-c", "4",
                              "-C", "1", "-n", "100",
                              "-H", os.path.dirname(deppsim_path),
                              "-o", tmp_path], shell=False)

EDIT: 编辑:

Some of the errors: 一些错误:

/home/user/path/bin/venv/bin/python /home/user/path/bin/sim_seq.py
Pre-process input genome...
/home/user/path/bin/DeepSimulator/deep_simulator.sh: Zeile 207: deactivate: Datei oder Verzeichnis nicht gefunden
Pre-process input genome done!
Executing the preprocessing step...
Traceback (most recent call last):
  File "/home/user/path/bin/DeepSimulator/util/genome_sampling.py", line 5, in <module>
    import scipy.stats as st
ImportError: No module named scipy.stats

You can always call a particular install of python, including virtual environments, using the full path to the python executable. 您始终可以使用python可执行文件的完整路径来调用特定的python安装,包括虚拟环境。 So that's your answer. 这就是你的答案。 Use a full path to a python binary that is in the actual Python installation you want to use to run the subprocesses, and you won't involve any virtual environments in running the subprocesses. 使用要用于运行子流程的实际Python安装中的python二进制文件的完整路径,并且在运行子流程时不会涉及任何虚拟环境。

As an example, here's what two Python binary paths look like on my system: 例如,这是我的系统上的两个Python二进制路径:

> which python
/usr/local/bin/python
> venv development
> which python
/Users/stevenjohnson/envs/development/bin/python

So /usr/local/bin/python is my actual Python 2.7 installation, and /Users/stevenjohnson/envs/development/bin/python is my "development" virtual environment. 因此, / usr / local / bin / python是我实际的Python 2.7安装, / Users / stevenjohnson / envs / development / bin / python是我的“开发”虚拟环境。 So if I run the main program in my virtual environment, but if I call /usr/local/bin/python in my subprocess call, The subprocess will run using the base Python 2.7 installation. 因此,如果我在虚拟环境中运行主程序,但是如果我在子进程调用中调用/ usr / local / bin / python ,则该子进程将使用基本的Python 2.7安装运行。

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

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