45

I used pyenv and pyenv-virtualenv for managing a Python virtual environment.

I have a project working in a Python 3.4 virtual environment.

So all installed packages (Pandas, NumPy, etc.) are not the newest version.

I want to upgrade the Python version from 3.4 to 3.6 as well as upgrade other package versions to higher ones.

How can I do this easily?

1
  • 1
    maybe create a new virtual environment? Commented Jun 22, 2017 at 7:28

4 Answers 4

52

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 install -s 3.9.0 pyenv virtualenv 3.9.0 venv-name pyenv activate 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 safe.

Sign up to request clarification or add additional context in comments.

3 Comments

If you don't have 3.9.0 install on your system, you'll have to do run pyenv install -v 3.9.0 before you create the new virtualenv.
You are right, I'll add that line just in case
You also need to activate the virtual environment by running pyenv activate venv-name before pip install -r requirements-lock.txt.
10

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.

1 Comment

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
9

The 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).

Comments

-5

If you use Anaconda, just type

conda install python==$pythonversion$ 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.