0

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?

user2309803
  • 541
  • 5
  • 15
  • Can't reproduce this issue. Seems to work as expected for me. – sinoroc Feb 10 '20 at 22:16
  • @sinoroc I have just double-checked and I get the same results as stated above. However, if I first activate the virtual environment with `source ~/dev/venv/3.7/bin/activate` then I see the virtual environment's `site-packages` directory in `sys.path`. Was your virtual environment activated when you tried to reproduce the issue? – user2309803 Feb 12 '20 at 03:00
  • Oh, I think I got what the issue is. It wasn't clear to me where the `py` link was placed, so I assumed it was in the `venv/3.7/bin` directory, but it isn't, is it? Where is the `py` link? – sinoroc Feb 12 '20 at 08:50
  • @sinoroc No, the link to /home/sss/dev/venv/3.7/bin/python was placed in a separate directory. I will update my question to make that clear. – user2309803 Feb 12 '20 at 12:16
  • I am pretty sure this can't work then. `py` would need to find some files in directories relative to its location, example it looks for `../pyvenv.cfg`. If it can't find it, then it's not a virtual environment. See my answer for a possible alternative with a shell script, that could potentially still offer the intended behavior. – sinoroc Feb 12 '20 at 12:25

1 Answers1

1

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.

sinoroc
  • 18,409
  • 2
  • 39
  • 70