05 Apr 2022 / 4 minutes to read
It might be challenging to manage different projects and their requirements when we do Python coding with loads of varying package versions and intricate setups. Luckily, we have a secret tool for managing and switching between different setups or environments. Conda is a package manager allowing us to work with different environments from a command line. Please do not mix it up with the Anaconda, which is helpful in scientific computing and includes a set of packages including NumPy, Scipy, Jupiter notebooks, and Conda.
Using Conda package manager, we can create, list, remove, and update environments with different versions of Python and packages installed. In this introductory post, I will describe the process of creating and using Conda environments. This could be a good starting point for using Conda if you did not use it yet. For more details on Conda usage, read its official documentation. In this post, I will do a concise review of the most useful Conda commands to start with.
Prerequesites and installation
You can install the Conda on Windows, macOS, and Linux platforms, following the Installation instructions. In this post, I will use conda 4.11.0 with my macOS system. Please beware that some commands will be different if you use conda versions below 4.6.
Conda was installed with the Miniforge3-MacOSX-arm64 downloaded from Miniforge Releases and by running the bash script:
The following command creates a new environment with the name “env1” stored into the “/envs” directory in the path wherein conda is installed. On my computer, created environments are saved in the “/Users/elena/miniforge3/envs/” folder since Conda was installed together with the Miniforge.
conda create --name env1
In one of my posts, I have created a new Conda environment for managing TensorFlow installation for my deep learning experiments.
It is very convenient that we can specify the Python version installed into our new environment.
conda create --name tensorflow_m1 python==3.9
Alternatively, we can also create an environment with defined Python version and a required set of packages (possibly, with their versions).
conda create --name myenv python=3.9 scipy=1.7.3 pandas=1.3.5
Activating Environments and Installing Requirements
Next, we activate this environment, which means that the environment with this name becomes active that we can further work with it and install required packages.
conda activate tensorflow_m1
# Installing matplotlib
conda install matplotlib
I need the wget command, which is not installed by default on macOS, and I used this Conda command for installing wget.
conda install -c anaconda wget
However, unfortunately, not everything works out of the box. When I have tried using wget in my Jupyter notebook, I have got a message “zsh:1: command not found: wget”, which was solved with installing wget with Homebrew.
brew install wget
With “conda env list” command, we can see the list of created environments. A current environment, “tensorflow_m1” is emphasised with an asterisk sign.
conda env list
This post is a starting point of using Conda, and it gives a short explanation about creating and switching between Conda environments. I will further update this post with more insights helpful for managing Conda environments for productive development work.
Did you like this post? Please let me know if you have any comments or suggestions.Posts about development tools and Python coding
Elena, a PhD in Computer Science, simplifies AI concepts and helps you use machine learning.