Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Shell script to set up virtualenv and pip

I'm writing a shell script to set up my virtual env environment and install all related python packages via pip.

virtualenv -q -p /usr/bin/python3.5 $1
/bin/bash $1/bin/activate
pip install -r requirements.txt

$1 is the name of the virtualenv. The problem I have is that the pip command does not work in my virtualenv but is executed globally instead.

like image 951
Anh Tuan Nguyen Avatar asked Apr 24 '17 07:04

Anh Tuan Nguyen


2 Answers

As I mean to know you have to activate the virtualenv with:

source activate

I am not sure if this can be done from within a shell script, but you can try it as follows:

virtualenv -q -p /usr/bin/python3.5 $1
source $1/bin/activate
$1/bin/pip install -r requirements.txt
# pip install -r requirements.txt

Excerpt from activate:

$ cat activate
# This file must be used with "source bin/activate" *from bash*
# you cannot run it directly
like image 181
Claudio Avatar answered Oct 11 '22 01:10

Claudio


Looks like you've found the solution to your problem, but for future reference, you don't need to activate the virtualenv in order to run pip inside it:

#!/bin/bash
virtualenv -q -p /usr/bin/python3.5 $1
$1/bin/pip install -r requirements.txt
like image 34
Adam Byrtek Avatar answered Oct 10 '22 23:10

Adam Byrtek