You can create a virtual environment in Python conda using those four steps:
- 1. Check if Conda is Installed
- 2. Create Virtual Environment
- 3. Activate Your Virtual Environment
- 4. Install Packages in Virtual Environment
- 5. Deactivate Virtual Environment
In this article, you’ll learn about an important concept in Python: virtual environments.
Why Virtual Environments?
Programs evolve over time. If you are a productive coder, you’ll ship your code early and often. Over time, you’ll fix the bugs as they appear and integrate feedback of users of your code.
And this is what all coders are doing. Suppose you are programming a crypto trading bot using a library to connect to the API of crypto exchanges (so that your bot can buy and sell currencies). An example of such a library is ccxt. Your code requires the exact API commands from ccxt (version 1.1). Say you install the
ccxt library (version 1.1) on your computer.
A few years later, the
ccxt library has changed, for example, there may be a new version 3.3, and you decide that it’s time to create a new bot. You install the new version 3.3 on your computer. But suddenly weird things begin to happen. Your old bot still runs as a background process (using ccxt version 1.1) but the new bot cannot start (using ccxt version 3.3).
Without virtual environments, you would have to decide for installing either version ccxt 1.1 or 3.3. Then, you would have to rewrite all old code using older versions.
But what if the library developer has removed a critical feature of your application?
Fortunately, Python provides you with virtual environments. A virtual environment allows you to define and install different dependencies for different projects. This is exactly what you need. You would install environment A for the first bot requiring version 1.1 and environment B for the second bot requiring version 3.3. That’s it!
Using virtual environments, you can separately and independently install different packages (even different versions of packages) for different Python projects. These installations are NOT visible globally. This has the advantage that you don’t clutter your global Python installation on your computer with myriads of possibly conflicting packages.
How to Create Virtual Environments?
There are many different tools to create virtual environments (
venv). But if you understood the conceptual idea introduced in the previous paragraphs, it’s easy to understand and use all of these tools.
Personally, I am using the Anaconda distribution which is a popular Python distribution which is a free and open-source Python distribution specifically designed for scientific computing. In particular, Anaconda makes package management super easy.
As a side note: Catalina, one of the readers of my Email Python Academy, pointed out that she uses the Spyder code editor from the Anaconda distribution. I agree that this editor provides some great features and makes writing code easier and more convenient than the default IDLE editor.
In the Anaconda distribution, you can use the “
conda” tool to install new Python packages. The conda command combines the functionalities of the package manager pip with the functionalities of virtual environment tools such as
venv. Roughly speaking, you can install new packages globally on your computer, as well as separately for each project in a virtual environment.
1. Check if Conda is Installed
After installing the Anaconda distribution, you can simply run the following command in your shell or command line to check whether conda is already installed:
2. Create Virtual Environment
Then, you create your virtual environment by navigating to your project folder and executing the following command:
conda create -n yourproject python=3.7.3 anaconda
Just replace the dummy value “
yourproject” with the name of your project and the Python version with your Python version. To check your Python version, simply run the command “
python -V” in your shell.
Now, wait until your virtual environment is created. You should see a new folder “
yourproject” at your file path.
3. Activate Your Virtual Environment
At this point, you have created the virtual environment but it is not activated, yet. So simply execute the following command in your shell:
source activate yourproject
4. Install Packages in Virtual Environment
Now, you can install new packages specifically for this environment:
conda install -n yourproject scikit-learn
In this example, I’ve installed the scikit-learn package for machine learning. If you fail to specify the “
-n yourproject” option, it will install the package globally and not in the virtual environment.
Now, you can simply run “
python” in your shell and use the installed package “
5. Deactivate Virtual Environment
Let’s deactivate the virtual environment:
Quiz: If you open a new python shell now, can you still import the sklearn library?
If you haven’t globally installed the
So, you see that using virtual environments is pretty straightforward when using the Anaconda installation. You can find the official conda documentation here.
In case you want to improve your Python skills, check out my free email academy with Python cheat sheets and more learning material:
While working as a researcher in distributed systems, Dr. Christian Mayer found his love for teaching computer science students.
To help students reach higher levels of Python success, he founded the programming education website Finxter.com that has taught exponential skills to millions of coders worldwide. He’s the author of the best-selling programming books Python One-Liners (NoStarch 2020), The Art of Clean Code (NoStarch 2022), and The Book of Dash (NoStarch 2022). Chris also coauthored the Coffee Break Python series of self-published books. He’s a computer science enthusiast, freelancer, and owner of one of the top 10 largest Python blogs worldwide.
His passions are writing, reading, and coding. But his greatest passion is to serve aspiring coders through Finxter and help them to boost their skills. You can join his free email academy here.