Environment & Package Management. Beginner’s Guide To Anaconda | by Ujjwal Dalmia | Towards Data Science

A good handle on Package and Environment management ensures that we continue to avail the benefits of the latest package functionalities while ensuring that projects running on older package releases do not break down. This tutorial is a beginner’s guide to learn both package and environment management using Conda that comes bundled with Anaconda distribution. If you are new to Python and don’t have Anaconda setup on your system, we recommend that you read through this tutorial before proceeding further.
Conda is known to be a complete environment and package management tool. It does wonders when it comes to cross-platform package installation and environment management. Following is the list of benefits that one experiences when working with it.
Conda installs packages that are in binary format. A binary package format means that all the package dependencies, whether Python or Non-Python based, are kept pre-compiled in the package bundle and hence ensures smooth and faster package installations.
At the heart of Conda is SAT solver. SAT solver guarantees that any version update of an existing Python package is in line with the environmental requirements, which means that there is no impact on existing Python codes.
Conda is a complete manager. It not only supports package management but also handles environments efficiently.
Consider a scenario where you are working on multiple Python projects. These projects are working on a shared Python environment and are using the same packages. If one project warrants the need to update one of the shared packages, there is a possibility that post update, the other project might not function as expected. It is because the updated package might modify/ deprecate functionalities that were supported by earlier versions. To ensure that the projects’ functionalities don’t interfere with each other, maintaining separate environments for these projects is highly recommended.
This section will go through a few of the most common tasks associated with Python environment management. A list of these is as follows:
New environment creation
Listing existing environments
Using a specific environment
Deleting a specific environment
Sharing environment with others using .yml files
Creating a new Python environment using .yml files
To create a new Python environment, open the Anaconda prompt, and use the following command:
#### Syntaxconda create --name environment_name packages#### Sample Commandconda create --name new_env pandas=0.22 numpy 
Explanation
The syntax to create the new Python environment starts with the keyword conda followed by another keyword, create.
We then pass the argument --name and the actual environment name.
Finally, we add the list of packages we want to install.
If we do not provide any package name, the environment creation will be blank, which means that even the python instance will not be available in this environment.
When working on multiple projects, it is common for one to forget the name of the various environment they have created. Use the following command on the command line to get the list of the existing environment:
#### Syntax/ Commandconda env list
Notice that this command is a keyword-based command starting with conda followed by env and list.
To activate a specific environment or to switch from one to another, use the following command:
#### Syntaxconda activate environment_name#### Sample Commandconda activate new_env
Explanation
Notice the presence of the activated environment name before the command prompt. It indicates that the environment is now active.
Conda, by default, activates the base environment if the environment name is not there.
Any python script executed using the command prompt will get executed in the activated environment.
Removing environments using Conda is very simple. We can use the following command for the same:
#### Syntaxconda env remove --name environment_name#### Sample Commandconda env remove --name new_env
When sharing your Python project with others, it is also necessary to share the complete environment used by your Python project. Conda provides an option to export the environment details into a .yml file. We can then share this file along with the project folders.
#### Syntaxconda env export --name environment_name --file file_name#### Sample Commandconda env export --name new_env --file new_env.yml
Explanation
In the command, we use the keyword export after conda and env
The --name argument is optional. Without it, Conda exports the currently active environment.
Argument --file provides the option to name the exported file.
Setting up the Python environment using the .yml file is simple. Replace the keyword export with keyword create. Refer to the sample command below:
#### Syntaxconda env create --name environment_name --file file_name#### Sample Commandconda env create --name new_env --file new_env.yml
Explanation
Just like the export command, we have used arguments - -name & --file to indicate the name of the new environment to be created and the .yml file which contains the package details.
Without the --name argument, the new environment will have the same name as the name of the .yml file.
This section will go through a few of the most common tasks associated with package management. A list of these is as follows:
Listing packages in a specific environment
Installing new package
Installing a package from a specific channel
Updating an existing package
Removing packages
To get a list of all the packages in a specific Python environment, activate it first(refer to point 3 in environment management). Once done, use the following command on the command prompt:
#### Activate the preferred environmentconda activate new_env#### Get a list of all the packagesconda list
Use the following command to install a new package in your python environment:
#### Syntaxconda install package_name#### Sample commandconda install numpy
After we enter the command, Conda will resolve the environment requirements and will identify package dependencies. It then prompts us to check if we want to proceed with these additional dependencies. When prompted, press the letter y and press enter. The package will get installed.
Just like pip uses python package index (PyPI) for sourcing the package wheels, Conda has a base channel that is its default source of package binaries.
If working behind a firewall (in a secure corporate environment) or if the required package is not available in the base channel, we are required to source them from alternate channels. In the corporate setups, these channels are custom built by IT support teams. One of the commonly used alternate channels is conda-forge. The process of installation remains the same. The only change is the addition of a command argument that indicates the use of a specific channel for package installation.
#### Syntaxconda install -c channel_name package_name#### Sample commandconda install -c conda-forge geopandas
Notice the use of command-line argument -c and the channel name conda-forge used just before specifying the package name, geopandas
For this, replace the install keyword with another keyword update. Sample command below:
#### Syntaxconda update package_name#### Sample Commandconda update pandas
To remove an existing package, replace the keyword update with another keyword remove. Sample code added below:
#### Syntaxconda remove package_name#### Sample commandconda remove pandas
I hope this tutorial was informative, and you now have a ready guide for your package and environment management needs.
I will keep adding more to the getting started guides. Till then:
HAPPY LEARNING ! ! !