- Managing Python
- Viewing a list of available Python versions
- Installing a different version of Python
- Installing PyPy
- Using a different version of Python
- Updating or upgrading Python
- Using Different Python Versions with virtualenv
- 1. Quick Guide to Python Version Controlling with Virtualenv
- 2. Installing Virtualenv
- 3. Creating a Virtual Environment with virtualenv
- 4. Activating a Virtual Environment
- 5. Deactivating a Virtual Environment
- 6. Using Virtualenv with Different Python Versions
- 7. Summary and Conclusion
- You may also like reading:
- AlixaProDev
Managing Python
Conda treats Python the same as any other package, so it is easy to manage and update multiple installations.
Conda supports Python 3.8, 3.9, 3.10, and 3.11.
Viewing a list of available Python versions
To list the versions of Python that are available to install, in your terminal window or an Anaconda Prompt, run:
This lists all packages whose names contain the text python .
To list only the packages whose full name is exactly python , add the —full-name option. In your terminal window or an Anaconda Prompt, run:
conda search --full-name python
Installing a different version of Python
To install a different version of Python without overwriting the current version, create a new environment and install the second Python version into it:
- Create the new environment:
To create the new environment for Python 3.9, in your terminal window or an Anaconda Prompt, run:
conda create -n py39 python=3.9 anaconda
Note Replace py39 with the name of the environment you want to create. anaconda is the metapackage that includes all of the Python packages comprising the Anaconda distribution. python=3.9 is the package and version you want to install in this new environment. This could be any package, such as numpy=1.19 , or multiple packages .
Installing PyPy
To use the PyPy builds you can do the following:
conda config --add channels conda-forge conda config --set channel_priority strict conda create -n pypy pypy conda activate pypy
Using a different version of Python
To switch to an environment that has different version of Python, activate the environment .
Updating or upgrading Python
Use the terminal or an Anaconda Prompt for the following steps.
If you are in an environment with Python version 3.4.2, the following command updates Python to the latest version in the 3.4 branch:
The following command upgrades Python to another branch—3.8—by installing that version of Python. It is not recommended, rather it is preferable to create a new environment. The resolver has to work very hard to determine exactly which packages to upgrade. But it is possible, and the command is:
© Copyright 2017, Anaconda, Inc. Revision bf5876c2 .
Using Different Python Versions with virtualenv
Sometimes we would be required to run different versions of Python by using the virtualenv. Python Virtual environments enable us to run your python script with different versions.
As Python is being released new versions regularly, we would be required to keep our project code compatible with a newer version. Also, to avoid conflicts between packages and dependencies, developers use virtual environments to isolate their projects. In this article, we will learn how to use different versions of python with virtualenv .
The virtualenv package allows you to create and manage multiple Python environments on a single machine. With virtualenv , you can create a separate environment for each project, with specific Python versions and dependencies, making it easy to switch between projects.
1. Quick Guide to Python Version Controlling with Virtualenv
Before getting into detail, this section will give you a quick guide on how to use the virtualenv package in python to deal with different versions of python.
- Install the virtualenv package
- Now create a virtual environment
- Activate the virtual environment
- If you want to create another virtual environment, deactivate the virtual environment
- Now create the new Virtual Environment with a different version of python
- As needed, you can switch to any of these virtual environment that runs on different versions.
These are the steps that you should follow each time for working with a Python virtual environment having different versions of Python. Below is a step-by-step guide in detail:
2. Installing Virtualenv
As you might know virtualenv is a third-party Python package, so you need to install it first before using it. You can install it using any package manager in Python. It can be pip or conda. In this article, I will use the pip command to install packages.
To install virtualenv use the following command on your terminal:
# Install virtualenv using pip pip install virtualenv
If you are using conda than use the following command:
# Install virtualenv using conda conda install virtualenv
You can make sure that you have installed the virtualenv package by running the below command on your terminal:
# Verify virtualenv is installed succesfully virtualenv --version
3. Creating a Virtual Environment with virtualenv
Once you have installed the virtualenv package. The next step is to create a virtual environment. The virutalenv package allows you to create the virtual environment using any of the python version. This means that you can have global environment different than your virtual environment.
First, navigate to the directory where you want to create your virtual environment, and then run the below command on your terminal:
# Create virtual environment python -m venv myenv
This command will create a virtual environment in your current directory. The version of Python in the virtual environment using this command will be the same is the global version of python.
If you want to create a Python virtual environment using a specific version of Python, follow the below steps:
- Make sure you have installed the required version of Python. Suppose you want to create a virtual environment with python version 3.9 so make sure you have that version of Python installed.
- Locate the installation directory of Python. It will be in C Drive by default.
- Now copy the path and run the below command
python -m venv -p (python-exe-path) env-name
- python-exe-path is the path you copied
- env-name is the environment name you want to create
See the below command in my case:
# Create virtual environment with Python 3.11 version python -m virtualenv -p D:\Python311\python.exe my_second_env
The most important thing is to put the python.exe at the end of the path as you will face the access-denied error when creating a virtual environment using virtualenv . This error is quite misleading and can waste tons of time.
4. Activating a Virtual Environment
Now you have two virtual environments with two different Python versions. You can activate any of them using the activate.bat file. Run the below command on your terminal to activate the environment.
To activate Python virtual environment run the below command on your terminal.
# Activete virtual environment env/Scripts/activate
A lot of the time this code can produce an error that you are not authorized to use this script, you can change the authorization policy by running the below code on your terminal:
Set-ExecutionPolicy -Scope CurrentUser -ExecutionPolicy Unrestricted
5. Deactivating a Virtual Environment
When you feel the need to deactivate the virtual environment, you can run the below command on your terminal. It is a good practice to deactivate the virtual environment before exiting the project.
# Deactive virtual environment env/Scripts/deactivate
6. Using Virtualenv with Different Python Versions
As discussed earlier, the Python package virtualenv allows us to work with different Python versions at the same time. You can switch to any version of the Python virtual environment by deactivating one and activating another.
See the below picture, I have two virtual environments, one is Python with a Python version 3.9 and the other is 3.11 . I can switch to any of these by following the above methods.
7. Summary and Conclusion
In this article, we have discussed how to use different Python versions with virtualenv. We have learned, how to create, activate and deactivate a virtual environment. Working with different versions of Python is easy as you just have to switch from one version to another. Let me know if you have any questions.
You may also like reading:
AlixaProDev
I am a software Engineer with extensive 4+ years of experience in Programming related content Creation.