How to run virtualenv activate in bash script

How do you create a bash script to activate a Python virtual file?

I have a directory structure like:

.env bin activate ...other virtualenv files... src shell.sh ...my code... 

I can activate my virtualenv:

 user@localhost:src$ . ../.env/bin/activate (.env)user@localhost:src$ 

However, doing the same with the Bash script does nothing:

 user@localhost:src$ cat shell.sh #!/bin/bash . ../.env/bin/activate user@localhost:src$ ./shell.sh user@localhost:src$ 

What am I doing wrong?

+73
python bash virtualenv
Oct 29 '12 at 12:57
source share
8 answers

When you use the source, you load the activation script into your active shell.

When you do this in a script, you load it into the shell that terminates when your script ends, and you return to your original unactivated shell.

Your best option is to do it in function

 activate () { . ../.env/bin/activate } 

or nickname

 alias activate=". ../.env/bin/activate" 

Hope this helps.

+57
Oct 29
source share

You can invoke a bash script using the source.

Here is an example:

 #!/bin/bash # Let call this script venv.sh source "<absolute_path_recommended_here>/.env/bin/activate" 

On your shell, simply name it like this:

 > source venv.sh 

Or as @outmind suggested: (Note that this does not work with zsh)

 > . venv.sh 

There you will see an indication of the shell at your invitation.

+44
Feb 02 '16 at
source share

Although it does not add the prefix "(.env)" to the shell prompt, I have found that this script works as expected.

 #!/bin/bash script_dir=`dirname $0` cd $script_dir /bin/bash -c ". ../.env/bin/activate; exec /bin/bash -i" 

eg.

 user@localhost:~/src$ which pip /usr/local/bin/pip user@localhost:~/src$ which python /usr/bin/python user@localhost:~/src$ ./shell user@localhost:~/src$ which pip ~/.env/bin/pip user@localhost:~/src$ which python ~/.env/bin/python user@localhost:~/src$ exit exit 
+13
Oct 29
source share

Sourcing runs shell commands in your current shell. When you are inside the script, as you do above, you are working on the environment for that script, but when the script completes, the changes to the environment are discarded, since they have actually gone out of scope.

If you intend to run shell commands in virtualenv, you can do this in your script after the search to activate the script. If you intend to interact with the shell inside virtualenv, you can create a sub-shell inside your script that inherits the environment.

+8
Oct 29
source share

You can also do this using a subshell to better limit usage - here is a practical example:

 #!/bin/bash commandA --args # Run commandB in a subshell and collect its output in $VAR VAR=$( PATH=$PATH:/opt/bin . /path/to/activate > /dev/null commandB # tool from /opt/bin that requires virtualenv ) # Use the output of the commandB later commandC "${VAR}" 

This style is especially useful when

  • commandA or commandB exist in /opt/bin
  • these commands fail under virtualenv
  • you need many different virtualenvs
+1
Jun 13 '18 at 5:54
source share

What happens with finding a bash script for?

  • If you intend to switch between multiple virtual windows or quickly enter a single virtualenv, do you try virtualenvwrapper ? It provides many utils like workon venv , mkvirtualenv venv etc.

  • If you just run the python script in a specific virtualenv, use /path/to/venv/bin/python script.py to run it.

0
Oct 29
source share

Here is a script that I often use

 #!/bin/bash -x PWD='pwd' /usr/local/bin/virtualenv --python=python3 venv echo $PWD activate () { . $PWD/venv/bin/activate } activate 
0
Jun 24 '19 at 19:01
source share

You must use several commands on the same line. eg:

 os.system(". Projects/virenv/bin/activate && python Projects/virenv/django-project/manage.py runserver") 

When you activate your virtual environment on one line, I think that it forgets about the other command lines, and you can prevent this by using several commands on the same line. It worked for me :)

0
Jul 05 '19 at 15:38
source share



All Articles