简体   繁体   中英

How to activate virtualenv using python subprocess module?

I am trying to invoke a shell script using python's subprocess module. The shell script activates a virtualenv using virtualenvwrapper and in turn invokes a python script. The last invoked python script needs libraries installed in virtualenv and it is crashing.

tried activating virtualenv again in python script but of no use

Parent Python code-

command = "/home/arman/analysis_server/new_analysis/run"

output = subprocess.Popen([command], shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)

/run script -

#!/bin/bash

export WORKON_HOME=~/Envs

source /usr/local/bin/virtualenvwrapper.sh

workon analytics

python /home/arman/analysis_server/new_analysis/AnalysisWrapper.py

AnalysisWrapper.py -

cmd = "python /home/arman/analysis_server/new_analysis/DataHandlerWrapper.py " + instrument + " &"

subprocess.Popen(cmd, shell=True, executable='/bin/bash', stdout=out, stderr=out)

The DataHandlerWrapper.py script needs virtualenv but it is crashing

I think your issue is that Popen spawns a subshell, so you activating the virtualenv in one subprocess and trying to use it in another is never going to work.

If there's nothing happening in between you could perhaps try chaining your commands into the same process:

command = "/home/arman/analysis_server/new_analysis/run && python /home/arman/analysis_server/new_analysis/DataHandlerWrapper.py " + instrument + " &"

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