ueronica
ueronica

Reputation: 75

Create virtualenv with most recent version of python

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!

Upvotes: 2

Views: 487

Answers (2)

ueronica
ueronica

Reputation: 75

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
    

Upvotes: 1

Seth
Seth

Reputation: 46453

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.

Upvotes: 4

Related Questions