Cannot Use Pip Within Venv

1.2k Views Asked by At

I'm aware there are many similar questions but I have been through them all to no avail.

On Ubuntu 18.04, I have Python 2 and Python 3.6. I create a venv using the command below and attempt to install a package using pip. However, it attempts to install on the global system and not in the venv.

python3 -m venv v1

When I run 'which python' it correctly picks the python within the venv. I have checked he v1/bin folder and pip is installed. The path within the pip script is correctly pointed to toward python in the venv.

I have tried reinstalling python3 and venv, destroying and recreating the virtual environment and many other things. Wondering is there some rational way to understand and solve this.

1

There are 1 best solutions below

0
On BEST ANSWER

The problem in my case was that the mounted drive I was working on was not mounted as executable. So pip couldn't be executed from within the venv on the mount.

This was confirmed because I was able to get a pip install using 'python -m pip install numpy' but when importing libraries, e.g. 'import numpy', was then faced with further error of:

multiarray_umath.cpython-36m-x86_64-linux-gnu.so: failed to map segment from shared object

which led back to the permissions issue as per github issue below. Fix for that by dvdabelle in comments then fixes dependent and original issue.

https://github.com/numpy/numpy/issues/15102

In his case, he could just switch drive. I have to use this drive. So the fix was to unmount my /data disk where I was working and remount it with exec option!

sudo umount /data

sudo mount -o exec /dev/sda4 /data

'which pip' now points to the pip in the venv correctly

Note: to make it permanent add the exec switch to the line for the drive in fstab as per https://download.tuxfamily.org/linuxvillage/Informatique/Fstab/fstab.html (make exec the last parameter in the options or user will override it) E.g.

UUID=1332d6c6-da31-4b0a-ac48-a87a39af7fec /data auto rw,user,auto,exec 0 0