How can a venv shared between machines use the exact same interpreter?

Viewed 67

I have a directory accessible from multiple Linux machines on the same network (the precise terminology for this is a directory on a "mounted network drive", right?), within which I have a python virtual environment, made with python3 -m venv venv.

I've tried checking the python version from a few of the machines using this:

$ . venv/bin/activate
$ python --version

and I get back different versions depending on what machine it is. Some show 3.8.5, some 3.8.10. I believe I've figured out that this is because the "interpreter" in the venv is really a link to the interpreter in /usr/bin, which is machine-specific:

$ ls -lah ./venv/bin/ | grep python
lrwxrwxrwx  1 echols14 cs236ta    7 Sep 29 02:08 python -> python3
lrwxrwxrwx  1 echols14 cs236ta   16 Sep 29 02:08 python3 -> /usr/bin/python3

When I take a look at the contents of /usr/bin, it seems like there are more things there relating to python than just the single interpreter file python3.8:

$ ls -lah /usr/bin | grep python
-rwxr-xr-x  1 root   root    2.3K Jun 20  2017 dh_python3-ply
lrwxrwxrwx  1 root   root      23 Sep 28 16:10 pdb3.8 -> ../lib/python3.8/pdb.py
lrwxrwxrwx  1 root   root      31 Mar 13  2020 py3versions -> ../share/python3/py3versions.py
lrwxrwxrwx  1 root   root       9 Mar 13  2020 python3 -> python3.8
-rwxr-xr-x  1 root   root    5.3M Sep 28 16:10 python3.8
lrwxrwxrwx  1 root   root      33 Sep 28 16:10 python3.8-config -> x86_64-linux-gnu-python3.8-config
lrwxrwxrwx  1 root   root      16 Mar 13  2020 python3-config -> python3.8-config
-rwxr-xr-x  1 root   root     364 Dec 17  2019 python3-qr
-rwxr-xr-x  1 root   root    3.2K Sep 28 16:10 x86_64-linux-gnu-python3.8-config
lrwxrwxrwx  1 root   root      33 Mar 13  2020 x86_64-linux-gnu-python3-config -> x86_64-linux-gnu-python3.8-config

I'd like the venv to use the same python version regardless of which machine it is used from. Is that possible?

My first thought is to copy the python3.8 file from one machine's /usr/bin and place it in the venv's bin, then adjust the links so that it is the one that gets used when the venv is activated. Would I need to copy other python-related files from /usr/bin for it to function? Is this method safe, or should it be avoided for some reason?

If that method is not a good idea, what other way could I get the shared venv to always use the same version of python, regardless of the machine?

NOTE: I don't have root/sudo permissions on all machines in question, but I do have root/sudo permissions on one of the machines.

1 Answers

Found it. When creating the virtual environment, use the --copies option:

--copies Try to use copies rather than symlinks, even when symlinks are the default for the platform

documentation

Which looks like this:

python3 -m venv --copies venv

And results in this:

$ ls -lah venv/bin/ | grep python
-rwxr-xr-x  1 echols14 cs236ta 5.3M Oct  7 18:26 python
-rwxr-xr-x  1 echols14 cs236ta 5.3M Oct  7 18:26 python3
Related