How does one enter a Python virtual environment and run shell commands in it from a shell script?

393
December 10, 2016, at 11:54 AM

I have been writing a shell script to be run in a CentOS 7 Docker container in order to create an AppImage. In this script I would like to run the Shell command pip install -U spyder from within a Python virtual environment (started by running source AppDir/usr/bin/activate) started by the shell script. The problem is that I don't know how to do this because lines in the script after the source AppDir/usr/bin/activate line are ignored (as at this point in the script, the shell has entered the Python virtual environment). So is there some option I need to pass the source AppDir/usr/bin/activate command so that it will run the pip install -U spyder command in this Python virtual environment?

Answer 1

You can install your requirement without activating virtual environment, but with providing full path to your virtualenv pip.

<path_to_virtualenv>/bin/pip install -U spyder

Because what activate is doing it's putting your virtualenv bin/ folder before the PATH, so that pip, python commands would be references to your viartualenv's before global ones. From source of activate:

VIRTUAL_ENV="<path_to_env>"
export VIRTUAL_ENV
_OLD_VIRTUAL_PATH="$PATH"
PATH="$VIRTUAL_ENV/bin:$PATH"
export PATH
READ ALSO
Copying only worksheet of XLS to become one of many worksheets in another XLS using Python?

Copying only worksheet of XLS to become one of many worksheets in another XLS using Python?

Using the Python test code below I am trying to copy three single worksheet Excel files into a single new Excel file with three worksheets named the same as the original files

390
function in python for searching posible combination for specific name in a big file

function in python for searching posible combination for specific name in a big file

I have a huge file(50,000 row) with 2 columns(id and name)One id can have different names but I am only looking for specific names and their ids

316
Ignoring new lines in python regular expression

Ignoring new lines in python regular expression

I have a xml data, having multiple rows and I wan't certain processing on each rowBut I was having trouble in fetching each row with tags <Row> to </Row>

263
TFLearn &ldquo;cannot feed value of shape.&rdquo;

TFLearn “cannot feed value of shape.”

I started using a very basic Deep Belief Network in Nodejs but it wasn't fast enough

300