The best way to work on any application is to mimic the production environment into your local, so that you get the same environment configuration as in production. This way you can make sure there won’t be any surprise as you commit new code fixes and deploy that into production.
However, what if you have multiple projects to work on in your local system? Let’s say one project uses Python version 2.7.x, and you have another project that uses some of the advanced features that uses Python version 3.x. In this scenario, if you have installed Python 2.7.x and you run the project that uses 3.x, that might not be the best approach. You might be facing version specific errors for the mismatch of the Python versions.
“The right approach to managing multiple Python versions is using ‘Conda’ as a Python package manager.”
Installing Conda
The installation steps will vary based on whether you use Windows, Mac OS, or Linux. Since I’m running on a Mac, I will run the command below in my Zsh (Z Shell) to install Conda:
brew install miniconda
Once the command has run successfully, I will verify Conda has been installed properly by the following command:
conda --version
This will print the version of conda that was installed.
Now, as Conda has been installed successfully we’ll create a virtual environment.
Creating a Virtual Environment
Create a virtual environment using the command below:
conda create -n "venv_python2.7.18" python==2.7.18
Here’s what the command is doing step by step:
- “conda create” is the command for creating a virtual env using conda.
- “-n” is short for “–name” parameter that configures the name for the virtual env.
- “venv_python2.7.18” will be the environment name.
- “python==2.7.18” specifies which Python version will be used for this virtual environment.
From the running log, you will notice the environment has been installed at this location: /opt/miniconda3/envs/venv_2.7.18. It also mentions the command to activate or deactivate the specific environment:
- Activate: conda activate venv_2.7.28
- Deactivation: conda deactivate
Activating a Virtual Environment
Run the following command to activate the virtual environment we newly created:
conda activate venv_2.7.18
Notice how on the far left “base” changed to “venv_2.7.18”. Also notice checking the active Python version shows “Python 2.7.18”.
Deactivating a Virtual Environment
As we’ve confirmed that we’re getting the right Python version, let’s deactivate the virtual environment using the command below:
conda deactivate
This will deactivate the currently active virtual environment:
Switching to a different virtual environment using Conda
Let’s build a new virtual environment using “conda create” command and activate the virtual environment:
Let’s start by creating a new virtual environment with Python 3.13.5.
conda create -n venv_3.13.5 python==3.13.5
Output:
Now, let’s activate the Python 3.13.5 virtual environment:
conda activate venv_3.13.5
Now, as we’ve successfully switched to the virtual env for Python 3.13.5, we can verify the Python version using the command below:
python --version
Output:
As you can see, this way we can successfully switch from one Python version to a different version without any complexity.
Ideal workflow for proper version management
We can create different virtual environments and specify which Python version to use for that particular virtual environment. The virtual environment is created in a central location. This way, regardless of our project directory, we can easily switch (activate) to the appropriate virtual environment as per the project’s needs.
To summarize, here’s the Conda workflow:
- Clone your project from Github/Gitlab/Bitbucket etc. into your local.
- Create virtual environments using conda by specifying which Python version to use. If a virtual environment already exists, skip this step.
- Activate the virtual environment.