Why does virtualenv inherit $ PYTHONPATH from my shell?

So, I am transferring all my tools from python2 to python3.4 to a Ubuntu 14.04 machine. So far I have been doing the following:

  • aliased python for python3 in my zshrc just for my user
  • pip3 is installed on the system itself (but I will just use virtualenvs for everything, so I really won’t use it)
  • changed my virtualenvwrapper "make" alias to mkvirtualenv --python=/usr/bin/python3("workon" is called below as "v")

Now curious, and you can clearly see this below, starting python3 from the virtual environment virtualenv still inherits my $ PYTHONPATH, which is still configured for all my python2 paths. This leads to chaos when installing / running programs in my virtualenv, because python3 paths appear AFTER old python2 paths, so python2 modules are imported first into my programs. Dropping my $ PYTHONPATH until `` before starting virtualenv, this will be fixed and my programs will start as expected. But my questions are:

  • Is this $ PYTHONPATH inheritance in virtualenvs normal? Doesn't that defeat the whole goal?
  • Why set $ PYTHONPATH as env-var in a shell when python is already processing its own paths?
  • Am I using $ PYTHONPATH correctly? Should I just install it in my "zshrc" to list only my personal add-ons ($ HOME / dev) and not the backup "/ usr / local / lib /" locations?
  • I can easily export an alternative python3 path for use with my virtualenvs just before they are called and reset them when it's 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)
+4
3

$PYTHONPATH, . , $PYTHONPATH . , python, python, script .

, , $PYTHONPATH ( zshrc) "$ HOME/dev" . python2 , python3 , , virtualenv.

0

Pythonpath, virtualenv/bin/activate:

export PYTHONPATH = "/your/path"

:

:

,

OLD_PYTHONPATH = "$ PYTHONPATH"

bin/postdeactivate script.


, add2virtualenv .

+1

$PYTHONPATH , virtualenv , (-) PYTHONPATH .

One of the joys of working in virtual environments is that it is much less necessary to place additional directories on yours PYTHONPATH, but it seems that you involuntarily treat it as a global (for all shells) setting, when it is more suitable for setting up for each project.

0
source

Source: https://habr.com/ru/post/1547229/


All Articles