27

I used pyenv, pyenv-virtualenv for managing python virtual environment.

I have a project working in Python 3.4 virtual environment.

So all installed packages(pandas, numpy etc) are not newest version.

What I want to do is to upgrade Python version from 3.4 to 3.6 as well as upgrade other package version to higher one.

How can I do this easily?

Keto
  • 1,470
  • 1
  • 12
  • 25
user3595632
  • 5,380
  • 10
  • 55
  • 111

4 Answers4

34

Here is how you can switch to 3.9.0 for a given virtual environement venv-name:

pip freeze > requirements-lock.txt
pyenv virtualenv-delete venv-name
pyenv virtualenv 3.9.0 venv-name
pip install -r requirements-lock.txt

Once everything works correctly you can safely remove the temporary requirements lock file:

rm requirements-lock.txt

Note that using pip freeze > requirements.txt is usually not a good idea as this file is often used to handle your package requirements (not necessarily pip freeze output). It's better to use a different (temporary) file just to be sure.

cglacet
  • 8,873
  • 4
  • 45
  • 60
11

Use pip freeze > requirements.txt to save a list of installed packages.

Create a new venv with python 3.6.

Install saved packages with pip install -r requirements.txt. When pip founds an universal wheel in its cache it installs the package from the cache. Other packages will be downloaded, cached, built and installed.

phd
  • 82,685
  • 13
  • 120
  • 165
  • 1
    This is half the solution so I don't understand why it's chosen as the solution for this post. Not only do you have to save your requirements, but you have to nuke your old virtual env, create a new one with the new python version and then restore your requirements! @cglacet has the right answer above – Vandad Nahavandipoor Oct 27 '22 at 02:54
4

OP asked to upgrade the packages alongside Python. No other answers address the upgrade of packages. Lock files are not the answer here.


Save your packages to a requirements file without the version.

pip freeze | cut -d"=" -f1 > requirements-to-upgrade.txt

Delete your environment, create a new one with the upgraded Python version, then install the requirements file.

pyenv virtualenv-delete venv-name
pyenv virtualenv 3.6.8 venv-name
pip install -r requirements-to-upgrade.txt

The dependency resolver in pip should try to find the latest package. This assumes you have the upgrade Python version installed (e.g., pyenv install 3.6.8).

Keto
  • 1,470
  • 1
  • 12
  • 25
-4

If you use anaconda, just type

conda install python==$pythonversion$

JaeWoo So
  • 568
  • 5
  • 18