Setting Up a Virtual Environment with Conda
From the previous post, we’ve seen how to set up a virtual environment with Python’s venv. In this post, we will be taking a look at how to set up a Python developing environment with conda when you’re dealing with complex environments. The venv is great enough when you’re developing a simple program or web application, in my opinion. However, if you’re trying to develop data science-related things, AI models, or such, I believe it is much easier to use conda, since conda is able to create multiple Python environments with specific Python versions.
You can either install anaconda or miniconda (both are conda) based on your preference. I am using miniconda since it’s much lighter than the anaconda. Either way, it works the same way, the only difference between those two is just anaconda comes with various pre-installed packages so it might be a little bulky.
Installation
This page shows how to install miniconda with command line scripts.
After the installation, every command will start with conda. When conda command not found is displayed, make sure to add environment variable into your .zshrc or .bashrc file.
# ~/.zshrc (or .bashrc)
export PATH="$HOME/miniconda3/bin:$PATH"
If the terminal displays (base), you’re all set.
Creating a new virtual environment
You can simply create a new conda environment with the following command.
conda create --name <env-name> python=<version>
Activating
After creating a new virtual environment, you can activate the one as follows:
conda activate <env-name>
Then, the name of the virtual environment from the terminal will change from (base) to (env-name).
To verify whether the environment is reall ready, try which python and see if the result is something like ~/miniconda3/envs/<env-name>/bin/python.
Installing packages
It is always a good practice to use requirements.txt file. As described from the previous post, using pip-tools will make your life much easier.
From this step, you can follow the steps of last post’s package installation steps.