1

I am using windows 7. I have multiple python virtual environments. Say I added venv_1 to system PATH. In the command line, say I activate another venv_2, now the prompt line shows

(venv_2) C:\>

But if I type python here, it still runs the python in venv_1.

Is this the intended behavior?

Alex
  • 912
  • 2
  • 7
  • 19

1 Answers1

1

This is not the intended behaviour, but it probably means you either made a mistake in setting up the virtual environment, or in activating it.

To be sure what version is being run, try running:

where python

Whatever the top item in the resulting list is, will be the copy of Python Windows would start. If you're right and it does actually point to venv_1, then there must be something wrong with the setup in venv_2.

By running set, you should be able to see a list of all environment variables. Check for:

PATH=<long list of directory names, it should have the venv_2\Scripts at the start>

And:

_OLD_VIRTUAL_PATH=<the same list, without that entry>

It's this simple change to the path that causes Windows to find the Python in your virtual environment first, before the one in your other virtual environment, that you added to the global path.

Note that adding the Scripts folder of the one virtual environment probably isn't a good idea, since you'd only want to use that when the corresponding virtual environment is activated and all the environment variables set accordingly.

Grismar
  • 27,561
  • 4
  • 31
  • 54
  • Thank you. Following your procedure, I found the problem. I changed the path of `venv_2` before. with `set`, the `PATH` still shows the old path to `venv_2`. After looking around I realize venv's can't really be relocated (hmmm...). I guess I will just re-create the venv. – Alex Jan 30 '19 at 01:54
  • You can move a virtual environment, but it takes some extra work. More details here https://stackoverflow.com/questions/32407365/can-i-move-a-virtualenv - note that in most cases it won't be worth it. Recreating the venv and just quickly reinstalling packages using something like `pip install -r requirements.txt` after a `pip freeze > requirements.txt` tends to be a lot easier and quicker. – Grismar Jan 30 '19 at 06:45
  • Yes, I found out about `--relocatable`, and that it is experimental. I end up just re-create my venv. However some packages are not on PyPI, and they require a specific installation order, so I had to do it manually. In the requirements is there a way to tell pip to install a local `whl`? – Alex Jan 30 '19 at 07:44