简体   繁体   中英

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

I am using Pycharm for Python development. 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 ).

So I need to deactivate the Pycharm venv when calling the script via subprocess.call() , as far as I understand. 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. 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.

As an example, here's what two Python binary paths look like on my system:

> 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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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