f



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

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@Tetuoan2:~/Projects/cannon/New2.7Projects/memory$ pytho=
n
Python 2.7.12 (default, Nov 19 2016, 06:48:10)=20
[GCC 5.4.0 20160609] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> exit()
(memory) malikarumi@Tetuoan2:~/Projects/cannon/New2.7Projects/memory$ pytho=
n3
Python 3.4.2 (default, Apr 17 2015, 18:47:05)=20
[GCC 4.8.2] on linux
Type "help", "copyright", "credits" or "license" for more information.

I did not even know this was possible. Doesn=E2=80=99t that defeat the purp=
ose? More to the point, how do I make sure I am using the right one? If I w=
ant the interpreter, that=E2=80=99s easy. But what happens when I want to i=
nstall and use a program like Django? How do I control which interpreter Dj=
ango uses? I=E2=80=99ve been through the official docs a couple of times to=
day, but detailed explanations of pyvenv, let alone this dual version featu=
re, have not been found. If you can point me to a good one, please do. Mean=
while...

I hoped maybe using different versions of pip would solve the problem, but =
pip2 list and pip3 list are identical =E2=80=93 hence, the very problem I t=
hought venv=E2=80=99s were supposed to avoid.=20

Thanks
0
Malik
12/21/2016 2:37:35 AM
comp.lang.python 77058 articles. 3 followers. Post Follow

3 Replies
37 Views

Similar Articles

[PageSpeed] 22

On Wednesday 21 December 2016 13:37, Malik Rumi 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:

I'm not an expert on pyvenv, but my guess is this:

Would you expect a command like "ls" or "grep" to continue to work from inside 
a venv? I expect you would say Yes.

How about "perl"?

If your venv has shadowed python, using the venv instead of the system python, 
there's no reason to expect that python3 will be any different than the same 
command outside of the venv. It will just run the same executable found in the 
PATH as normal, just like (say) "perl" or "grep".

> 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? 

Run this command from the shell prompt:

    which python

and that should tell you which executable you are getting.

(I think that should work inside a venv.)


> 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...

How do you control which interpreter Django uses outside of a venv?




-- 
Steven
"Ever since I learned about confirmation bias, I've been seeing 
it everywhere." - Jon Ronson

0
Steven
12/21/2016 4:06:42 AM
On 20Dec2016 18:37, Malik Rumi <malik.a.rumi@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@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@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@zip.com.au>
0
Cameron
12/21/2016 4:14:32 AM
Malik Rumi <malik.a.rumi@gmail.com> writes:

> 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@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@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.

A venv doesn't remove everything from your path. Presumably python3 is
some system installed python. What does "which python3" say?

If you just invoke "python" then you'll get the venv one.


0
Paul
12/21/2016 7:21:54 AM
Reply: