I have bash script which runs Python program. I use virtualenv.
Firs I include env to bash:
source ./ENV/bin/activate
Then I see (ENV) prefix in bash prompt.
$ echo $PATH
/project/ENV/bin:/usr/local/sbin:/usr/local/bin:/usr/bin:/usr/bin/core_perl
When I try to run my Python program from bash script, it runs with wrong version of Python. ENV uses Python 2.6, while the system has 3.2 by default.
I print Python version from Python script, and it prints 3.
But why?
ls -la
-rw-r--r-- 1 wnc wnc 2219 Sep 27 01:42 activate
-rw-r--r-- 1 wnc wnc 1275 Sep 27 01:42 activate.csh
-rw-r--r-- 1 wnc wnc 2414 Sep 27 01:42 activate.fish
-rw-r--r-- 1 wnc wnc 1129 Sep 27 01:42 activate_this.py
-rwxr-xr-x 1 wnc wnc 357 Sep 27 01:42 easy_install
-rwxr-xr-x 1 wnc wnc 365 Sep 27 01:42 easy_install-2.6
-rwxr-xr-x 1 wnc wnc 318 Sep 27 01:42 pip
-rwxr-xr-x 1 wnc wnc 326 Sep 27 01:42 pip-2.6
lrwxrwxrwx 1 wnc wnc 9 Sep 27 01:42 python -> python2.6
lrwxrwxrwx 1 wnc wnc 9 Sep 27 01:42 python2 -> python2.6
-rwxr-xr-x 1 wnc wnc 6240 Sep 27 01:42 python2.6
1 Answer 1
Sanity check:
source /path/to/ENV/bin/activate
python -V
deactivate
python -V
The first python -V should show print Python 2.6 and the second Python 3.2, right?
When you run your Python script, the one you want to use the above virtualenv, make sure to source /path/to/ENV/bin/activate first, for example if you run it from inside a bash script:
#!/bin/bash
source /path/to/ENV/bin/activate
python /path/to/script.py
Tell me which step doesn't work and any error messages you get.
If your python program needs to run in a different way, not from a shell script, for example by wsgi, then I'll have more tips for you. The bottom line is: don't forget to source the virtualenv activate script before using your python script that needs it.
which python?ls -la /project/ENV/bin?python -c 'import sys, pprint; pprint.pprint(sys.version_info)'.