Conda Environments…

Installing python packages can be frustrating when using libraries that are not as widely used. It seems the development of those libraries is often more of a hobby and sometimes it expands into something more useful and the installation of those packages is a bit unwieldly.
Although I use several different python package managers, this article provides several tips of how to use conda. In my experience, it can take several hours to get the right combination to make a library work and most of this time is just letting conda determine what to install. In some cases I have only been able to get packages to install if I have the order of the installations correct. In these cases, I find myself incredibly frustrated.
For example, I enjoy working with geospacial packages and recently was using hvplot. It has many dependencies but in many cases these dependencies are not explicitly written in the documentation. Even by creating a clean conda environment, I was able to install the packages without problems but immediately had missing dependencies and version conflicts when running code that I knew should work. Mostly by luck, I knew that geopandas uses most of the same packages and their documentation about how to create a virtual environment is better. I also had gotten that library to work before after a few tweeks but the error messages were very similar to those of hvplots. So after creating a functioning geopandas environment, I was able to install hvplots and the associated downgrades to packges all worked so the environment functioned. This process without doing any troubleshooting took about 45 minutes.
I don’t know if there is a way of getting around this environment testing phase but here are some nice practices that may help along the way:
- Always create a new environment when starting. This will make it easier to troubleshooting and duplicate later.
# Create new environment named as `envname`
conda create --name envname
- In the new conda environment, set the channels that you would like to use. This can decrease some of the conflicts.
# Set channel
conda config --env --add channels conda-forge
conda config --env --set channel_priority strict
- Once you have the environment functioning with your code, save the contents of your environment to a .yml file. This will make it so you can duplicate the environment very quickly. Meaning, the 45 minutes it took me to have conda figure out the versions initially are already specified in the file so it just needs to do the installation without any extra checks.
# Create file containing environment specifications
conda env export > envname.yml
- Using the .yml file, recreate the environment so others can directly install it. The .yml file needs to be same directory as the terminal that runs this command.
# Create environment from .yml
conda env create -f envname.yml
- If you forget your environment names then these commands might help.
conda env list
conda info --envs
- Also there will be times when you completely destroy an environment and deleting it will be necessary. Sometimes it is also necessary to go to the directory location and manually delete the environment’s folder. Often the environments are held in a folder in the
root
directory withinAnaconda3/envs
.
# Remove environment and its dependencies
conda remove --name envname --all
- More information about conda environments can be found in the documentation