2022-05-30

Python venv doesn't work if directory names change

I changed my python project directory, then venv (virtual environment) didn't work properly. Easy way to fix is to remove the venv and just create a new one. But there's another way if you will.

/Users/minhokim

Make a directory

mkdir project1
cd project1

Create a virtual environment inside

python3 -m venv venv
source venv/bin/activate 
(venv) > which python
/Users/minhokim/project1/venv/bin/python3.8
 
(venv) > echo $VIRTUAL_ENV
/Users/minhokim/project1/venv

Change parent directory name

project1 -> project2

cd ../
mv project1 project2
cd project2
(venv) > deactivate
source venv/bin/activate 

It's broken; venv is still pointing to previous directory name, project1

(venv) > echo $VIRTUAL_ENV
/Users/minhokim/project1/venv

Change path to current

project1 -> project2

code venv/bin/activate

project2/venv/bin/activate

...
VIRTUAL_ENV="/Users/minhokim/project1/venv"
export VIRTUAL_ENV
...

code venv/bin/pip

code venv/bin/pip3

code venv/bin/pip3.8

... etc

#!/Users/minhokim/project1/venv/bin/python3.8
...