55

I tried activating a VirtualEnv through a shell script like the one below but it doesn't seem to work,

#!/bin/sh
source ~/.virtualenvs/pinax-env/bin/activate

I get the following error

$ sh virtualenv_activate.sh 
virtualenv_activate.sh: 2: source: not found

but if I enter the same command on terminal it seems to work

$ source ~/.virtualenvs/pinax-env/bin/activate
(pinax-env)gautam@Aspirebuntu:$

So I changed the shell script to

#!/bin/bash
source ~/.virtualenvs/pinax-env/bin/activate

as suggested and used

$ bash virtualenv_activate.sh 
gautam@Aspirebuntu:$

to run the script .

That doesn't throw an error but neither does that activate the virtual env

So any suggestion on how to solve this problem ?

PS : I am using Ubuntu 11.04

Gautam
  • 7,868
  • 12
  • 64
  • 105

5 Answers5

73

TLDR

Must run the .sh script with source instead of the script solely

source your-script.sh

and not your-script.sh

Details

sh is not the same as bash (although some systems simply link sh to bash, so running sh actually runs bash). You can think of sh as a watered down version of bash. One thing that bash has that sh does not is the "source" command. This is why you're getting that error... source runs fine in your bash shell. But when you start your script using sh, you run the script in an shell in a subprocess. Since that script is running in sh, "source" is not found.

The solution is to run the script in bash instead. Change the first line to...

#!/bin/bash

Then run with...

./virtualenv_activate.sh

...or...

/bin/bash virtualenv_activate.sh

Edit:

If you want the activation of the virtualenv to change the shell that you call the script from, you need to use the "source" or "dot operator". This ensures that the script is run in the current shell (and therefore changes the current environment)...

source virtualenv_activate.sh

...or...

. virtualenv_activate.sh

As a side note, this is why virtualenv always says you need to use "source" to run it's activate script.  

bryn
  • 3,155
  • 1
  • 16
  • 15
Mark Hildreth
  • 42,023
  • 11
  • 120
  • 109
  • 1
    or use the borne shell equivalent of source, i.e. `. envFile`, often refered to as 'dot the envFile'. Good luck ;-) – shellter Sep 10 '11 at 02:54
  • 1
    Thanks but that doesn't seem to solve the problem , When I use bash it does not throw an error but neither does it activate the virtualenv . Could You please elaborate on .envFile – Gautam Sep 10 '11 at 02:58
  • 5
    When you run these scripts the way I've shown, it actually creates a NEW shell, and runs the script in that shell. Since the entire point of the "source" script is to change the current shell, this defeats the purpose. Run your script using "source virtualenv_activate.sh" or ". virtualenv_activate.sh" (notice the space after that first period). – Mark Hildreth Sep 10 '11 at 03:43
  • On Ubuntu 11.10 here's the little .sh script that worked for me: `. virtual-folder/bin/activate` `sudo service postgresql restart` `python manage.py runserver` – Massagran Apr 10 '12 at 03:22
8

source is an builtin shell command in bash, and is not available in sh. If i remember correctly then virtual env does a lot of path and environment variables manipulation. Even running it as bash virtualenv_blah.sh wont work since this will simply create the environment inside the sub-shell.

Try . virtualenv_activate.sh or source virtualenv_activate.sh this basically gets the script to run in your current environment and all the environment variables modified by virtualenv's activate will be available.

HTH.

Edit: Here is a link that might help - http://ss64.com/bash/period.html

arunkumar
  • 32,803
  • 4
  • 32
  • 47
5

On Mac OS X your proposals seems not working.

I have done it this way. I'am not very happy with solution, but share it anyway here and hope, that maybe somebody will suggest the better one:

In activate.sh I have

echo 'source /Users/andi/.virtualenvs/data_science/bin/activate'

I give execution permissions by: chmod +x activate.sh

And I execute this way:

`./activate.sh`

Notice that there are paranthesis in form of ASCII code 96 = ` ( Grave accent )

andilabs
  • 22,159
  • 14
  • 114
  • 151
2

For me best way work as below.

Create start-my-py-software.sh and pest below code

#!/bin/bash
source "/home/snippetbucket.com/source/AIML-Server-CloudPlatform/bin/activate"
python --version
python /home/snippetbucket.com/source/AIML-Server-CloudPlatform/main.py

Give file permission to run like below.

chmod +x start-my-py-software.sh

Now run like below

.start-my-py-software.sh

and that's it, start my python based server or any other code.

ubuntu #18.0

Community
  • 1
  • 1
Tejas Tank
  • 1,100
  • 2
  • 16
  • 28
0

In my case, Ubuntu 16.04, the methods above didn't worked well or it needs much works.

I just made a link of 'activate' script file and copy it to home folder(or $PATH accessible folder) and renamed it simple one like 'actai'.

Then in a terminal, just call 'source actai'. It worked!

Jaeyoon Jeong
  • 569
  • 4
  • 8