为什么 virtualenv 从我的 shell 继承 $PYTHONPATH? [英] Why does virtualenv inherit $PYTHONPATH from my shell?

查看:30
本文介绍了为什么 virtualenv 从我的 shell 继承 $PYTHONPATH?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我在 Ubuntu 14.04 机器上将所有工具从 python2 迁移到 python3.4.到目前为止,我已经完成了以下工作:

  1. 在我的 zshrc 中为我的用户将 python 别名为 python3
  2. 在系统本身上安装了 pip3(但无论如何我都会使用 virtualenvs,所以我不会真正使用它)
  3. 将我的 virtualenvwrapper "make" 别名更改为 mkvirtualenv --python=/usr/bin/python3('workon' 在下面被调用为 'v')

现在奇怪的是,您可以在下面清楚地看到,从 virtualenv 激活的环境中运行 python3 仍然继承我的 $PYTHONPATH,它仍然为我的所有 python2 路径设置.这会在我的 virtualenv 中安装/运行程序时造成严重破坏,因为在旧的 python2 路径之后会显示 python3 路径,因此首先在我的程序中导入 python2 模块.在启动 virtualenv 之前将我的 $PYTHONPATH 清零可以解决这个问题,我的程序会按预期启动.但我的问题是:

  1. virtualenvs 中 $PYTHONPATH 的这种继承正常吗?这不是违背了整个目的吗?
  2. 当 python 已经在内部处理自己的路径时,为什么要在 shell 中将 $PYTHONPATH 设置为 env-var?
  3. 我是否正确使用了 $PYTHONPATH?我是否应该在我的zshrc"中设置它只列出我的个人添加($HOME/dev)而不是多余的/usr/local/lib/"位置?
  4. 我可以很容易地导出一个备用的 python3 路径,以便在调用它们之前与我的 virtualenvs 一起使用,并在完成后重置它们,但这是解决此问题的最佳方法吗?

<前>○ 回声 $PYTHONPATH/usr/local/lib/python2.7/site-packages:/usr/local/lib/python2.7/dist-packages:/usr/lib/python2.7/dist-packages:/home/brian/devbrian@zeus:~/.virtualenvs○ 蟒蛇2Python 2.7.6(默认,2014 年 3 月 22 日,22:59:56)[GCC 4.8.2] 在 linux2 上输入帮助"、版权"、信用"或许可"以获取更多信息.>>> 导入系统,pprint>>> pprint.pprint(sys.path)['','/usr/local/lib/python2.7/dist-packages/pudb-2013.3.4-py2.7.egg','/usr/local/lib/python2.7/dist-packages/Pygments-1.6-py2.7.egg','/usr/local/lib/python2.7/dist-packages/urwid-1.1.1-py2.7-linux-x86_64.egg','/usr/local/lib/python2.7/dist-packages/pythoscope-0.4.3-py2.7.egg','/usr/local/lib/python2.7/site-packages','/usr/local/lib/python2.7/dist-packages','/usr/lib/python2.7/dist-packages','/home/brian/dev','/usr/lib/python2.7','/usr/lib/python2.7/plat-x86_64-linux-gnu','/usr/lib/python2.7/lib-tk','/usr/lib/python2.7/lib-old','/usr/lib/python2.7/lib-dynload','/usr/lib/python2.7/dist-packages/PILcompat','/usr/lib/python2.7/dist-packages/gst-0.10','/usr/lib/python2.7/dist-packages/gtk-2.0','/usr/lib/pymodules/python2.7','/usr/lib/python2.7/dist-packages/ubuntu-sso-client','/usr/lib/python2.7/dist-packages/ubuntuone-client','/usr/lib/python2.7/dist-packages/ubuntuone-storage-protocol','/usr/lib/python2.7/dist-packages/wx-2.8-gtk2-unicode']>>>brian@zeus:~/.virtualenvs○ v py3venv(py3venv)brian@zeus:~/.virtualenvs○ 蟒蛇3Python 3.4.0(默认,2014 年 4 月 11 日,13:05:11)[GCC 4.8.2] 在 Linux 上输入帮助"、版权"、信用"或许可"以获取更多信息.>>> 导入系统,pprint>>> pprint.pprint(sys.path)['','/usr/local/lib/python2.7/site-packages','/usr/local/lib/python2.7/dist-packages','/usr/lib/python2.7/dist-packages','/home/brian/dev','/home/brian/.virtualenvs/py3venv/lib/python3.4','/home/brian/.virtualenvs/py3venv/lib/python3.4/plat-x86_64-linux-gnu','/home/brian/.virtualenvs/py3venv/lib/python3.4/lib-dynload','/usr/lib/python3.4','/usr/lib/python3.4/plat-x86_64-linux-gnu','/home/brian/.virtualenvs/py3venv/lib/python3.4/site-packages']>>>(py3venv)

解决方案

我偶然发现了关于 $PYTHONPATH 的这个答案刚刚为我解决了这个问题.本质上,设置 $PYTHONPATH 是可选的,对用户来说很方便.它应该只包含用户想要添加到他们的 python 路径中的额外路径,这样用户就不必为了从终端运行脚本而在 python 本身中执行此操作.

所以为了解决我上面的问题,我将我的 $PYTHONPATH(在我的 zshrc 中)设置为我的附加文件夹$HOME/dev",没有别的.这消除了我的路径中对 python2 的引用,并且我的所有 python3 程序都在我的 virtualenv 中按预期启动.

So I'm migrating all my tools from python2 to python3.4 on an Ubuntu 14.04 machine. So far I've done the following:

  1. aliased python to python3 in my zshrc for just my user
  2. installed pip3 on the system itself (but I'll just be using virtualenvs for everything anyway so I won't really use it)
  3. changed my virtualenvwrapper "make" alias to mkvirtualenv --python=/usr/bin/python3 ('workon' is invoked below as 'v')

Now curiously, and you can clearly see it below, running python3 from a virtualenv activated environment still inherits my $PYTHONPATH which is still setup for all my python2 paths. This wreaks havoc when installing/running programs in my virtualenv because the python3 paths show up AFTER the old python2 paths, so python2 modules are imported first in my programs. Nulling my $PYTHONPATH to '' before starting the virtualenv fixes this and my programs start as expected. But my questions are:

  1. Is this inheritance of $PYTHONPATH in virtualenvs normal? Doesn't that defeat the entire purpose?
  2. Why set $PYTHONPATH as an env-var in the shell when python already handles it's own paths internally?
  3. Am I using $PYTHONPATH correctly? Should I just be setting it in my 'zshrc' to only list my personal additions ($HOME/dev) and not the redundant '/usr/local/lib/' locations?
  4. I can very easily export an alternate python3 path for use with my virtualenvs just before invoking them, and reset them when done, but is this the best way to fix this?

    ○ echo $PYTHONPATH
    /usr/local/lib/python2.7/site-packages:/usr/local/lib/python2.7/dist-packages:/usr/lib/python2.7/dist-packages:/home/brian/dev

    brian@zeus:~/.virtualenvs
    ○ python2
    Python 2.7.6 (default, Mar 22 2014, 22:59:56)
    [GCC 4.8.2] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import sys, pprint
    >>> pprint.pprint(sys.path)
    ['',
     '/usr/local/lib/python2.7/dist-packages/pudb-2013.3.4-py2.7.egg',
     '/usr/local/lib/python2.7/dist-packages/Pygments-1.6-py2.7.egg',
     '/usr/local/lib/python2.7/dist-packages/urwid-1.1.1-py2.7-linux-x86_64.egg',
     '/usr/local/lib/python2.7/dist-packages/pythoscope-0.4.3-py2.7.egg',
     '/usr/local/lib/python2.7/site-packages',
     '/usr/local/lib/python2.7/dist-packages',
     '/usr/lib/python2.7/dist-packages',
     '/home/brian/dev',
     '/usr/lib/python2.7',
     '/usr/lib/python2.7/plat-x86_64-linux-gnu',
     '/usr/lib/python2.7/lib-tk',
     '/usr/lib/python2.7/lib-old',
     '/usr/lib/python2.7/lib-dynload',
     '/usr/lib/python2.7/dist-packages/PILcompat',
     '/usr/lib/python2.7/dist-packages/gst-0.10',
     '/usr/lib/python2.7/dist-packages/gtk-2.0',
     '/usr/lib/pymodules/python2.7',
     '/usr/lib/python2.7/dist-packages/ubuntu-sso-client',
     '/usr/lib/python2.7/dist-packages/ubuntuone-client',
     '/usr/lib/python2.7/dist-packages/ubuntuone-storage-protocol',
     '/usr/lib/python2.7/dist-packages/wx-2.8-gtk2-unicode']
    >>>

    brian@zeus:~/.virtualenvs
    ○ v py3venv
    (py3venv)
    brian@zeus:~/.virtualenvs
    ○ python3
    Python 3.4.0 (default, Apr 11 2014, 13:05:11)
    [GCC 4.8.2] on linux
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import sys, pprint
    >>> pprint.pprint(sys.path)
    ['',
     '/usr/local/lib/python2.7/site-packages',
     '/usr/local/lib/python2.7/dist-packages',
     '/usr/lib/python2.7/dist-packages',
     '/home/brian/dev',
     '/home/brian/.virtualenvs/py3venv/lib/python3.4',
     '/home/brian/.virtualenvs/py3venv/lib/python3.4/plat-x86_64-linux-gnu',
     '/home/brian/.virtualenvs/py3venv/lib/python3.4/lib-dynload',
     '/usr/lib/python3.4',
     '/usr/lib/python3.4/plat-x86_64-linux-gnu',
     '/home/brian/.virtualenvs/py3venv/lib/python3.4/site-packages']
    >>>
    (py3venv)

解决方案

I stumbled onto this answer about $PYTHONPATH which solved this for me just now. Essentially, setting $PYTHONPATH is optional and is a convenience to the user. It should only contain additional paths the user wants to add to their python path so that the user doesn't have to do this in python itself just to run a script from the terminal.

So to solve my problem above, I set my $PYTHONPATH (in my zshrc) to only my additional folder of '$HOME/dev' and nothing else. This eliminated the references to python2 in my path and all my python3 programs are starting as expected in my virtualenv.

这篇关于为什么 virtualenv 从我的 shell 继承 $PYTHONPATH?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆