python, python3, python3.7 files don't work in a venv (can't be activated)

529 Views Asked by At

I am trying to run some code inside a server. In that server, we use docker images to create notebooks inside directories, with commands like:

docker run -it --gpus "device=1" -p 8886:8886 -v /folder/directory:/workspace/work --name container-name --shm-size=60g --ulimit memlock=-1 --ulimit stack=67108864 --rm imageid jupyter notebook --port=8886 --ip=0.0.0.0 --allow-root --no-browser

Once created the notebook with an image, we have two different environments with two different python versions in the folder that were designed to execute the code inside /folder/directory: venv3.6 and venv3.7.

Even if I didn't create them, I am confident that the environments worked at some point (there are checkpoints obtained from the execution of the code by a colleague that worked on it before me). However, it must have been messed up with at some point, maybe after some modifications on the libraries of the docker image.

The problem is that, whenever I try to activate venv3.7 by using source ./venv3.7/bin/activate and run a script with python script_name.py, the python version that is executed is not 3.7, but rather 3.6.10. When going into /venv3.7/bin/activate and trying to access or download the python, the python3 or the python3.7 files, they cannot be accessed, moved or activated (i.e., if I enter /venv3.7/bin/python3.7 on the terminal, I obtain the file not found error).

When the environment is activated:

root@XXXX:/workspace/work/path# which python
/opt/conda/bin/python

root@XXXX:/workspace/work/path# source ./venv3.7/bin/activate
(venv3.7) root@XXXX:/workspace/work/path# 

Following this stackoverflow post, I make the following comprobations

(venv3.7) root@XXXX:/workspace/work/path# python -V
Python 3.6.10 :: Anaconda, Inc.

(venv3.7) root@XXXX:/workspace/work/path# echo $PATH
/workspace/work/path/venv3.7/bin:/usr/local/nvm/versions/node/v15.0.1/bin:/opt/conda/bin:/opt/cmake-3.14.6-Linux-x86_64/bin/:/usr/local/mpi/bin:/usr/local/nvidia/bin:/usr/local/cuda/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/local/ucx/bin:/opt/tensorrt/bin

(venv3.7) root@XXXX:/workspace/work/path# which python
/opt/conda/bin/python

(venv3.7) root@XXXX:/workspace/work/path# alias
alias egrep='egrep --color=auto'
alias fgrep='fgrep --color=auto'
alias grep='grep --color=auto'
alias l='ls -CF'
alias la='ls -A'
alias ll='ls -alF'
alias ls='ls --color=auto'

Which shows that the path is added correctly and there is no alias for python that could be messing with the activation but, still, python command uses the version from /opt/conda/bin/python instead of /workspace/work/path/venv3.7/bin

I also have checked that the path VIRTUAL_ENV in activate script (venv3.7/bin/activate) is correct.

I noticed that the directory: /venv3.7/pyvenv.cfg contains:

home = /usr/bin
include-system-site-packages = false
version = 3.7.5

And when I go to the directory /usr/bin, which should contain the python in which the environment is based, it only has python2.7 files. Could that mean that, when the first directory in $PATH is followed, no valid version of Python is found?

My guess is that the python (python, python3, python3.7) files were symlinks that were broken because the python version changed in /usr/bin. However, I don't want to risk to update the version of python in that directory, because it would probably change the default python in /opt/conda/bin/python instead, and I don't know much about docker images. Do you think it would work? In that case, how would I do it?

As additional info, the python files inside venv3.6/bin seems to work well (it can be executed and copied), but maybe because /venv3.6/pyvenv.cfg leads to the default python instead (in /opt/conda/bin/python). Also, after asking the original creator of the code, she doesn't know how to solve this issue either.

I need the environment to work, and recreating it is problematic, since many libraries were downloaded from different places (it was delicate work).

What do you suggest?

EDIT

I have tried recreating the environment with the python version I need (3.7.5). Do you know of an easy way to install the same libraries than in the other environment, considering that I can't activate it?

I was thinking to use the folders with the libraries located in /venv3.7/lib, but It is not straight forward. Any idea on how to do it?

Also, would you recommend me to create the new environment with virtualenv (to have a separate python version) or, rather, with anaconda?

Thank you so much for reading me!

1

There are 1 best solutions below

0
On

After checking the python3.7 file in the environment:

root@XXXX:/# cd workspace/work/path/venv3.7/bin

root@XXXX:/workspace/work/path/venv3.7/bin# stat python
  File: python -> python3.7
  Size: 9               Blocks: 0          IO Block: 4096   symbolic link
Device: XXXX      Inode: XXXX    Links: 1
Access: (XXXX)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2022-12-06 10:31:18.165001523 +0000
Modify: 2022-05-20 12:28:37.481538688 +0000
Change: 2022-05-20 12:28:37.481538688 +0000
 Birth: -

root@XXXX:/workspace/work/path/venv3.7/bin# stat python3.7
  File: python3.7 -> /usr/bin/python3.7
  Size: 18              Blocks: 0          IO Block: 4096   symbolic link
Device: XXXX      Inode: XXXX    Links: 1
Access: (XXXX)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2022-12-06 10:31:18.165001523 +0000
Modify: 2022-05-20 12:28:37.481538688 +0000
Change: 2022-05-20 12:28:37.481538688 +0000
 Birth: -

It became obvious that, as stated in the post, /usr/bin should be the directory where python3.7 should be installed. That means the problem could be solved by installing it in that folder.

As I didn't know that was the default folder for the Python installation, I tried installing python from source as exposed in several guides. However, even if now the environment started accessing python3.7 in the folder, that installation didn't work either.

So I just tried apt-get install python3.7. It took around 10 seconds and, when I tried the code again, it worked!

Next time, when your environments fails because the wrong python version is executed, and the aliases and $PATH are right (see this post for more details), just remember to check where the python files in the environment point to and verify that the python installation is correct!

I hope this is useful for you.