2

I recently updated my Mac's python version to 2.7.10, and I can confirm that this change has occurred:

$ python --version
Python 2.7.10

However, when I make a new virtualenv, the python version is still an old one:

(ENV)$ python --version
Python 2.7.6

Any suggestions for how to create a virtualenv that uses Python 2.7.10?

To be clear, my question is different from this one. There are not distinct binaries called python2.7.6 and python2.7.10 in /usr/bin/; instead, there is a single binary called python2.7. I have already tried the following virtualenv -p sequence to no effect. :(

$ virtualenv -p /usr/bin/python2.7 ENV
$ source ENV/bin/activate
(ENV)$ python --version
Python 2.7.6

Any additional thoughts/suggestions would be much appreciated. Thanks!

Community
  • 1
  • 1
ueronica
  • 75
  • 8
  • `/usr/local/bin/python2.7`? what does `which -a python` output? – Padraic Cunningham Aug 06 '15 at 21:16
  • Yes, **it is a duplicate** - the answer is to specify the appropriate *specific* interpreter with `-p`. If you want to know how to *find* a particular interpreter, that's a different question, which @PadraicCunningham's comment gives you the answer to. Otherwise, without an exhaustive history of the Python versions you've installed and how (and possibly not even then), how can we tell you where that one is? – jonrsharpe Aug 06 '15 at 21:21

2 Answers2

3

The virtualenv command itself is a wrapper script that is run with the python it's installed in.

On my system, I have several versions of python installed - 3.4 from Python.org, the system python, and 2.7.9 from homebrew. My virtualenv looks like this:

#!/Library/Frameworks/Python.framework/Versions/3.4/bin/python3.4

# -*- coding: utf-8 -*-
import re
import sys

from virtualenv import main

if __name__ == '__main__':
    sys.argv[0] = re.sub(r'(-script\.pyw|\.exe)?$', '', sys.argv[0])
    sys.exit(main())

In your case, it's calling your old python (2.7.6).

Each time you install virtualenv, the wrapper script is replaced. Because of this, I personally never use the virtualenv wrapper script, I always call the module directly with python, so I know which python I'm using.

$ python -m virtualenv <your-env>

If you get a "No module named virtualenv" error, then that means your new python does not have virtualenv installed in its site packages.


Regarding comments about -p: It's worth noting that if you've removed your old python, virtualenv -p doesn't work. You will get an unfriendly "bad interpreter" error from bash.

Seth
  • 45,033
  • 10
  • 85
  • 120
1

I was able to fix my issue. Conceptually, the issue was as @Seth described, but I just wanted to share the exact steps that I used in case others encounter this issue in the future.

  1. Open a python terminal that is running your preferred version of python.

    $ python
    
  2. Run the following commands in the python terminal:

    >>> import sys
    >>> print sys.executable
    /path/to/python/that/is/being/used/
    >>> exit()
    
  3. Create a new virtualenv using the following command and your newly-acquired python path:

    virtualenv -p /path/to/python/that/is/being/used/ ENV
    
ueronica
  • 75
  • 8