3

Environment & Package Management

 3 years ago
source link: https://towardsdatascience.com/environment-package-management-55168c56b77
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Environment & Package Management

Beginner’s Guide To Anaconda

Image for post
Image for post
Photo by David Clode on Unsplash

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.

Why Conda?

Condais 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.

Environment Management

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

1. New environment creation

To create a new Python environment, open the Anaconda prompt, and use the following command:

#### Syntax
conda create --name environment_name packages#### Sample Command
conda create --name new_env pandas=0.22 numpy
Image for post
Image for post
Environment Creation (Image by Author)

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.

2. Listing existing 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/ Command
conda env list
Image for post
Image for post
Listing Environments (Image by Author)

Notice that this command is a keyword-based command starting with conda followed by env and list.

3. Using a specific environment

To activate a specific environment or to switch from one to another, use the following command:

#### Syntax
conda activate environment_name#### Sample Command
conda activate new_env
Image for post
Image for post
Activating Environment (Image by Author)

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.

4. Deleting a specific environment

Removing environments using Conda is very simple. We can use the following command for the same:

#### Syntax
conda env remove --name environment_name#### Sample Command
conda env remove --name new_env
Image for post
Image for post
Removing Environment (Image by Author)

5. Sharing environment with others using .yml files

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.

#### Syntax
conda env export --name environment_name --file file_name#### Sample Command
conda 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.

6. Setting up a new environment using .yml files

Setting up the Python environment using the .yml file is simple. Replace the keyword export with keyword create. Refer to the sample command below:

#### Syntax
conda env create --name environment_name --file file_name#### Sample Command
conda 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.

Using Conda for Package Management

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

1. Listing packages in a specific environment

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 environment
conda activate new_env#### Get a list of all the packages
conda list
Image for post
Image for post
Listing Packages (Image by Author)

2. Installing a new package

Use the following command to install a new package in your python environment:

#### Syntax
conda install package_name#### Sample command
conda 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.

Image for post
Image for post
Install Packages (Image by Author)

3. Installing a package from a specific channel

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.

#### Syntax
conda install -c channel_name package_name#### Sample command
conda 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

Image for post
Image for post
Package Installation — Alternate Channel (Image by Author)

3. Updating an existing package

For this, replace the install keyword with another keyword update. Sample command below:

#### Syntax
conda update package_name#### Sample Command
conda update pandas

4. Removing an existing package

To remove an existing package, replace the keyword update with another keyword remove. Sample code added below:

#### Syntax
conda remove package_name#### Sample command
conda remove pandas

Closing note

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 ! ! !


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK