19

I have created a virtual environment using python 3.6, then I've made a system upgrade and I've got python 3.7 installed system wide. Now I can't execute python files in that virtual environment because it's searching for python 3.6.

How can I upgrade the virtualenv python version to match the system wide version or how to downgrade the python version for that particular virtual environment?

I'm using manjaro.

dAn
  • 517
  • 1
  • 6
  • 10

1 Answers1

16

EDIT 1

Did some testing and found another more "graceful" way to (at least) update the executable. Let's assume the virtual env was initially created like so virtualenv -p /path/to/my/python2.7 .venv. The executable can be updated to a specific python version like so: virtualenv --clear -p /path/to/my/python3.6 .venv. Please validate the python symlink in .venv/bin/python is updated using ls -la .venv/bin/python. The old executable(s) will still be in ./venv/bin/.

Note: You need to have the specific target version of python installed.


See this link which explains it well.

Virtualenvwrapper comes with some convenient commands for managing your virtualenvs.

To change your Python version:

  1. Deactivate your current environment session.

  2. If you have many packages or libraries installed, it would be a good idea to make a requirements.txt file. Remember to edit version as necessary.

  3. Remove the virtualenv with the wrapper command: rmvirtualenv

    • This will remove the virtualenv, but leave your project files.
  4. Make a new virtualenv with the Python version you want.

    • Example: mkvirtualenv -p python3 env-name

    • You can specify the Python version with the -p flag and version. If you have a requirements.txt file, you can specify that with -r requirements.txt

  5. Now bind your new virtualenv to your project directory. You can specify the full paths, but it is easier to have your new virtualenv activated and be in your project directory. Then, run the command:

Example: setvirtualenvproject

Please let me/us know if this answer was helpful to you!

tafaust
  • 1,457
  • 16
  • 32
  • 4
    Thanks for quick answer. For some reasons I can't use the virtualenv wrapper but what I did is to manually remove bin/ include/ and lib/ folders of the virtualenv and then just run virtualenv -p python3 env – dAn Aug 19 '18 at 08:08
  • @dAn Happy to hear that it worked out for you! – tafaust Aug 19 '18 at 08:10
  • sounds like more of a headache than just deleting and recreating a new virtualenv. – dtc Jul 09 '19 at 21:48
  • 2
    This isn't a great answer, AFAICT because (1) when python is upgraded you lose the ability to create a new `requirements.txt` file (if that's not true, please explain how to do this); (2) This seems like "crush the old virtual environment and rebuild it" which is a pretty unsatisfactory answer. – Robert P. Goldman Aug 01 '19 at 13:53
  • @RobertP.Goldman IMHO (1) is not valid. One merely has to *manually update* the dependencies *by hand* to guarantee compatibility with the new python executable. There is no other way to do this AFAIK. To (2), it is mostly like this... even the "graceful" way (see my EDIT 1). – tafaust Oct 14 '19 at 17:21
  • Genereate a `requirements.txt` is a good idea. Tried some ways but failed. So, \n I have to rebuild a new venv and import the `requirements.txt`. – Allis Gao Jun 06 '21 at 12:36