DEV Community

Cover image for How to upgrade the Python version in a virtual environment
Maria Campbell
Maria Campbell

Posted on

1

How to upgrade the Python version in a virtual environment

Photo by Mitchell Luo on unsplash.com

Table of Contents

I was working on a new project today (the official Django Polls app
tutorial as a matter of fact), and I noticed that my virtual environment
was using Python 3.12. But in my project, I am using Python
3.13.0. Using different versions of Python in the virtual environment
vs in the code can create problems and conflicts. I had to upgrade
my version of Python in my venv folder pronto!

Activating the virtual environment

First, I had to make sure that my virtual environment was activated in
my project:

source venv/bin/activate
Enter fullscreen mode Exit fullscreen mode

Saving my current dependencies

Next, I had to make sure to save my application's current
dependencies:

pip freeze > requirements.txt
Enter fullscreen mode Exit fullscreen mode

De-activating the virtual environment

Next, I had to deactivate the virtual environment:

deactivate
Enter fullscreen mode Exit fullscreen mode

Deleting the current venv folder

Next, I had to delete the current venv folder. That is what I
call my virtual environment folder. You might call it
something else!

Creating a new venv folder

Next, I had to create a new venv folder using 3.13 since that is
what I wanted to upgrade to:

python3.13 -m venv venv
Enter fullscreen mode Exit fullscreen mode

Re-activating the virtual environment

Next, I had to re-activate the virtual environment:

source venv/bin/activate
Enter fullscreen mode Exit fullscreen mode

Installing saved dependencies

Next, I had to install my saved dependencies:

pip install -r requirements.txt
Enter fullscreen mode Exit fullscreen mode

And that's it!

Conclusion

In this post, I showed how to upgrade the Python version being used
in a Django application's virtual environment.

Related Resources

Related Posts

Heroku

This site is built on Heroku

Join the ranks of developers at Salesforce, Airbase, DEV, and more who deploy their mission critical applications on Heroku. Sign up today and launch your first app!

Get Started

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs