"venv activate" doesn't not change my Python path
Asked Answered
G

6

15

I create a virtual environment; let's say test_venv, and I activate it. All successful.

HOWEVER, the path of the Python Interpreter doesn't not change. I have illustrated the situation below.

For clarification, the python path SHOULD BE ~/Desktop/test_venv/bin/python.

>>> python3 -m venv Desktop/test_venv

>>> source Desktop/test_venv/bin/activate

(test_venv) >>> which python
/usr/bin/python 
Gills answered 21/12, 2020 at 9:13 Comment(5)
What is the version of python you are using? Did you try different python versions?Orphaorphan
Also, are you using some other python version management tools, like pyenv?Orphaorphan
Hi, I am using Python 3 and my problem only seems to be happening for that specific virtual environment. Meaning, for my other virtual environments it seems to correctly locate the python interpreter in ./venv_1/bin/python when activated. So, I eventually >>> rm -rf ... the problematic venv and re-made it again. Now, it seems to work fine; although, I will never know what the problem was. Do you have guesses or similar experience?Gills
If there was only few seconds between the creation and usage of the venv, the only possibility is that the venv was not created correctly in the first place. If it happens again, do not remove the faulty venv but compare the venv files with some difftool against a working one. It will probably point out the reason for you.Orphaorphan
as stated below this most likely happens when you move your venv (they are only designed to be static to the path they were created at, although you can edit the relevant files in the activate scripts).Mertz
I
13

Please make sure to read Note #2.


This is what you should do if you don't want to create a new virtual environment:

In venv/bin folder there are 3 files that store your venv path explicitly and if the path is wrong they take the normal python path so you should change the path there to your new path.

change: set -gx VIRTUAL_ENV "what/ever/path/you/need" in activate.fish

change: VIRTUAL_ENV="what/ever/path/you/need" in activate

change: setenv VIRTUAL_ENV "what/ever/path/you/need" in activate.csh

Note #1:
the path is to /venv and not to /venv/bin

Note #2:
If you reached this page it means that you are probably not following Python's best practice for a project structure. If you were, the process of creating a new virtual environment was just a matter of one command line.

Please consider using one of the following methods:

Thanks to Khalaimov Dmitrii, I didn't thought it was because I moved the folder.

Indiscernible answered 2/1, 2022 at 8:3 Comment(2)
To fix pip, I also had to change the path at the top of venv/bin/pipMusing
These are not the only files that store the path - almost everything in ./bin does for my install...Mendelian
P
21

It is not an answer specifically to your question, but it corresponds the title of the question. I faced similar problem and couldn't find solution on Internet. Maybe someone use my experience.

I created virtual environment for my python project. Some time later my python interpreter also stopped changing after virtual environment activation. Similar to how you described.

My problem was that I moved the project folder to a different directory some time ago. And if I return the folder to its original directory, then everything starts working again.

There is following problem resolution. You save all package requirements (for example, using 'pip freeze' or 'poetry') and remove 'venv'-folder (or in your case 'test_venv'-folder). After that we create virtual environment again, activate it and install all requirements.

This approach resolved my problem.

Periwig answered 24/9, 2021 at 19:2 Comment(2)
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Vercelli
This is exactly what happened to me. Thank you for saving me so much time!Chretien
I
13

Please make sure to read Note #2.


This is what you should do if you don't want to create a new virtual environment:

In venv/bin folder there are 3 files that store your venv path explicitly and if the path is wrong they take the normal python path so you should change the path there to your new path.

change: set -gx VIRTUAL_ENV "what/ever/path/you/need" in activate.fish

change: VIRTUAL_ENV="what/ever/path/you/need" in activate

change: setenv VIRTUAL_ENV "what/ever/path/you/need" in activate.csh

Note #1:
the path is to /venv and not to /venv/bin

Note #2:
If you reached this page it means that you are probably not following Python's best practice for a project structure. If you were, the process of creating a new virtual environment was just a matter of one command line.

Please consider using one of the following methods:

Thanks to Khalaimov Dmitrii, I didn't thought it was because I moved the folder.

Indiscernible answered 2/1, 2022 at 8:3 Comment(2)
To fix pip, I also had to change the path at the top of venv/bin/pipMusing
These are not the only files that store the path - almost everything in ./bin does for my install...Mendelian
Z
6

Check the value of VIRTUAL_ENV in /venv/bin/activate . If you renamed your project directory or moved it, then the value may still be the old value. PyCharm doesn't update your venv files if you used PyCharm to rename the project. You can delete the venv and recreate a new one if the path is wrong, or try the answer that talks about where to change it.

Zigzagger answered 20/10, 2022 at 23:48 Comment(1)
Exactly my issue, thank you. I had created the env, then refactored and moved the folder. I should have assumed that would cause some kind of issue like that, but never thought about it.Bozo
P
0

the following is at the top of bin/activate

# This file must be used with "source bin/activate" from bash
# you cannot run it directly

this worked for me whereas it didn't work when I typed bin/activate

Plugugly answered 4/2, 2024 at 18:3 Comment(1)
you need to put more information like what was the issue and what you wanted to do actually?Ironware
F
0

How I fixed the same issue:

  1. remote-ssh to the server with VScode

  2. from VScode, open the folder where the virtual environment was created

  3. in the VScode "Explorer" side-tab, open the "bin/activate.csh" file

  4. copy the quoted directory assigned to "setenv VIRTUAL_ENV"

  5. in the VScode "Search" side-tab, paste the directory in to the "Search" box

  6. in the "Replace" box, type the new directory of the virtual environment

  7. in VScode, select the "Terminal" menu at the top of the application. Then select "New Terminal"

  8. from the terminal, "cd" to the virtual environment directory

  9. run the command "deactivate"

  10. run the command "source bin/activate"

NOTE - All of this can be done, without VScode, locally on the system containing the virtual environment.

Fulgurate answered 12/4, 2024 at 13:53 Comment(0)
D
0

In my case, this problem occurs because I moved the project from its original path. To solve this:

#replace .venv with your virtual environment folder
rm -fr .venv
python3 -m venv .venv
source .venv/bin/activate

#optative verify the fixed path 
which pip

#install
pip install -r requirements.txt
Dereism answered 27/7, 2024 at 15:59 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.