pythonpython-3.xsymlinkpython-venvsite-packages

How to correctly create a symbolic link to the Python interpreter?


On Ubuntu 18.04, I installed Python 3.7 and a virtual environment in /home/sss/dev/venv/3.7 The venv site packages are in /home/sss/dev/venv/3.7/lib/python3.7/site-packages -

~/dev/venv/3.7/bin/python -m site
sys.path = [
    '/home/sss/dev',
    '/usr/lib/python37.zip',
    '/usr/lib/python3.7',
    '/usr/lib/python3.7/lib-dynload',
    '/home/sss/dev/venv/3.7/lib/python3.7/site-packages',
]
USER_BASE: '/home/shane/.local' (exists)
USER_SITE: '/home/shane/.local/lib/python3.7/site-packages' (exists)
ENABLE_USER_SITE: False

Next, I created a symbolic link in my home directory targeting the Python interpreter in the virtual environment -

ln -s /home/sss/dev/venv/3.7/bin/python ~/py
ls -l  py
lrwxrwxrwx 1 sss sss 35 feb  5 08:52 py -> /home/sss/dev/venv/3.7/bin/python

but when I use this link in place of /home/sss/dev/venv/3.7/bin/python, I would expect it to have access to packages in /home/sss/dev/venv/3.7/lib/python3.7/site-packages, but this is not the case -

./py -m site
sys.path = [
    '/home/sss/dev',
    '/usr/lib/python37.zip',
    '/usr/lib/python3.7',
    '/usr/lib/python3.7/lib-dynload',
    '/home/sss/.local/lib/python3.7/site-packages',
    '/usr/local/lib/python3.7/dist-packages',
    '/usr/lib/python3/dist-packages',
]
USER_BASE: '/home/sss/.local' (exists)
USER_SITE: '/home/sss/.local/lib/python3.7/site-packages' (exists)
ENABLE_USER_SITE: True

Is there a work-around to this problem?


Solution

  • I believe this can't work. The location of py matters. In order to take into account the virtual environment, py would look for some specific files at locations relative to its own. For example it would look for ../pyvenv.cfg and if it can't find that file then the virtual environment is completely ignored (whether it is active or not doesn't matter in such a case). I believe py has to be in the virtual environment's bin directory and nowhere else.


    Not sure exactly what the original intention is, but maybe you could write a py shell wrapper instead of a symbolic link, such as the following (just an example, it probably needs to be improved to be really useful):

    #!/usr/bin/env sh
    
    /home/sss/dev/venv/3.7/bin/python "$@"
    

    Such a script could be placed anywhere and it would always take the virtual environment into account.