1

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
Wolph
80.4k12 gold badges142 silver badges152 bronze badges
asked Oct 6, 2013 at 21:02
7
  • 2
    What is the result of which python? Commented Oct 6, 2013 at 21:14
  • Can you add the output of ls -la /project/ENV/bin? Commented Oct 6, 2013 at 21:15
  • [code] 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 [code] Commented Oct 6, 2013 at 21:40
  • It's not very readable when you post that as a Comment. Can you add it to your Answer. Also, what's the output of python -c 'import sys, pprint; pprint.pprint(sys.version_info)'. Commented Oct 6, 2013 at 21:45
  • in the interpreter which python returns env/bin python in the bash script I run user /usr/bin/python Commented Oct 6, 2013 at 21:46

1 Answer 1

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.

answered Oct 7, 2013 at 6:34
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.