Monday, March 28, 2022

[SOLVED] Activating virtualenv in linux isn't working

Issue

I've installed virtualenv on a Ubuntu 20.04. After sourcing the activate script, it seems that it is activated, but installing packages seems to install it in the user's site-packages rather than in the virtual environment.

(venv) $ python3 -m pip install pytest
Requirement already satisfied: pytest in /home/xxx/.local/lib/python3.8/site-packages (6.2.5)
Requirement already satisfied: packaging in /home/xxx/.local/lib/python3.8/site-packages (from pytest) (20.9)
Requirement already satisfied: pluggy<2.0,>=0.12 in /home/xxx/.local/lib/python3.8/site-packages (from pytest) (1.0.0)
Requirement already satisfied: py>=1.8.2 in /home/xxx/.local/lib/python3.8/site-packages (from pytest) (1.10.0)
Requirement already satisfied: attrs>=19.2.0 in /home/xxx/.local/lib/python3.8/site-packages (fro

Also, when I run Python and look at the sys.path:

['', '/usr/lib/python38.zip', '/usr/lib/python3.8', '/usr/lib/python3.8/lib-dynload', '/home/xxx/.local/lib/python3.8/site-packages', '/usr/local/lib/python3.8/dist-packages', '/usr/lib/python3/dist-packages']


Solution

I'll take a stab in the dark and say, inside a virtualenv, you probably don't need to invoke python3 rather, the python command directly points to the Python 3 interpreter for that virtualenv. And in this instance, invoking python3 explicitly points to your global Python 3 env. After all, modern virtualenvs are created with Python 3 as default.



Answered By - skytreader
Answer Checked By - Gilberto Lyons (WPSolving Admin)