Pyvenv puts both Python 2 and Python 3 in the same environment. Shocked!

Cameron Simpson cs at zip.com.au
Tue Dec 20 23:14:32 EST 2016


On 20Dec2016 18:37, Malik Rumi <malik.a.rumi at gmail.com> wrote:
>I just created a new venv using pyvenv from a 2.7 install. Now I am shocked to see that I can get both 2.7 and 3.4 in this same venv:
>
>(memory) malikarumi at Tetuoan2:~/Projects/cannon/New2.7Projects/memory$ python
>Python 2.7.12 (default, Nov 19 2016, 06:48:10)
>[GCC 5.4.0 20160609] on linux2
>Type "help", "copyright", "credits" or "license" for more information.
>>>> exit()
>(memory) malikarumi at Tetuoan2:~/Projects/cannon/New2.7Projects/memory$ python3
>Python 3.4.2 (default, Apr 17 2015, 18:47:05)
>[GCC 4.8.2] on linux
>Type "help", "copyright", "credits" or "license" for more information.
>
>I did not even know this was possible. Doesn’t that defeat the purpose? More 
>to the point, how do I make sure I am using the right one? If I want the 
>interpreter, that’s easy. But what happens when I want to install and use a 
>program like Django? How do I control which interpreter Django uses? I’ve been 
>through the official docs a couple of times today, but detailed explanations 
>of pyvenv, let alone this dual version feature, have not been found. If you 
>can point me to a good one, please do. Meanwhile...

I suspect you're confused. What do:

  which python

and

  which python3

say? Creating a virtual env does not do anything in particular to your 
environment. It provides a distinct environment to work in, but you do need to 
take a specific action to use it.

I'd inspect sys.path in each of your pythons:

  >>> import sys
  >>> sys.path

Watch on my own Mac:

  % /opt/local/bin/python3
  Python 3.5.2 (default, Oct 11 2016, 15:01:29)
  [GCC 4.2.1 Compatible Apple LLVM 7.3.0 (clang-703.0.31)] on darwin
  Type "help", "copyright", "credits" or "license" for more information.
  >>> import sys
  >>> sys.path
  ['', '/Users/cameron/lib/python', '/Users/cameron/rc/python', 
  '/opt/local/Library/Frameworks/Python.framework/Versions/3.5/lib/python35.zip', 
  '/opt/local/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5', 
  '/opt/local/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/plat-darwin', 
  '/opt/local/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/lib-dynload', 
  '/opt/local/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages']

versus this:

  % ~/var/venv/3/bin/python3
  Python 3.5.2 (default, Oct 11 2016, 15:01:29)
  [GCC 4.2.1 Compatible Apple LLVM 7.3.0 (clang-703.0.31)] on darwin
  Type "help", "copyright", "credits" or "license" for more information.
  >>> import sys
  >>> sys.path
  ['', '/Users/cameron/lib/python', '/Users/cameron/rc/python', 
  '/Users/cameron/var/venv/3/lib/python35.zip', 
  '/Users/cameron/var/venv/3/lib/python3.5', 
  '/Users/cameron/var/venv/3/lib/python3.5/plat-darwin', 
  '/Users/cameron/var/venv/3/lib/python3.5/lib-dynload', 
  '/opt/local/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5', 
  '/opt/local/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/plat-darwin', 
  '/Users/cameron/var/venv/3/lib/python3.5/site-packages', 
  '/Users/cameron/var/venv/3/lib/python3.5/site-packages/llfuse-1.1.1-py3.5-macosx-10.11-x86_64.egg', 
  '/opt/local/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages']

so you can see that the "native" python3 has one sys.path, and the one from my 
local python 3 virtualenv has a different one.

Check which pythons you're actually invoking, and what their respective 
sys.path values are.

Cheers,
Cameron Simpson <cs at zip.com.au>


More information about the Python-list mailing list