import numpyLoading and Installing Libraries
This lesson introduces Python libraries, showing how to install packages, import them into your environment, and explore library functions to extend Python for data analysis.
Import libraries, Package management, Bioconda, PyPI, Conda-forge
Approximate time: XX minutes
Learning Objectives
In this lesson, we will:
- Explain different ways to install external Python libraries
- Demonstrate how to load a library and how to find functions specific to a library
Overview of lesson
Most of the power of Python comes from its rich ecosystem of libraries. Instead of writing everything from scratch, you can install and import packages that handle tasks like reading csv files, working with biological sequences, or creating complex plots. The open source community has developed a vast array of libraries to support various domains and use cases. In this way, you do not have to reinvent the wheel every time you need to perform a common task. In this lesson, you will learn how to install, load, and explore libraries so you can tap into this broader ecosystem for your own work.
Libaries in Python
Libraries are collections of Python functions, data, and compiled code in a well-defined format, created to add specific functionality. Just as we created our own functions in the previous lesson, other users have created functions that they have shared with the community in the form of libraries. These packages can be installed and loaded into your Python environment to use the functions that they contain.
There are a set of standard (or base) packages which are considered part of the Python source code and automatically available as part of your Python installation. Base packages contain the basic functions that allow Python to work, and enable standard statistical and graphical functions on datasets; for example, all of the functions that we have been using so far in our examples.
The directories in Python where the packages are stored are called the libraries. The terms package and library are sometimes used synonymously and there has been discussion amongst the community to resolve this.
Channels for Python Libraries
There are many different channels for Python libraries. You can think of these as different places where Python libraries are stored and can be accessed from. Some of the most common channels include:
| channel | description |
|---|---|
| bioconda | Conda channel for bioinformatics tools and libraries.scanpy, anndata, etc. |
| conda-forge | Community conda channel with many packages for science, data, and general Python use.numpy, pandas, etc. |
| PyPI | Main online index for Python packages, used with pip to install and manage libraries.scipy, matplotlib, etc. |
Importing Libraries
Now that we have installed the libraries we want to use, we need to import them into our Python environment in order to use the functions that they contain. To import a library, we can use the import statement followed by the name of the library. For example, to import the numpy library, we would use the following code:
We will know that the library has been imported successfully if we do not get any error messages when we run this code. Once we have imported the library, we can use the functions that it contains by prefixing the function name with the library name and a dot. For example, to use the array() function from the numpy library, we would use the following code:
my_array = numpy.array([1, 2, 3, 4, 5])
print(my_array)[1 2 3 4 5]
Library Aliases
It is common practice to import libraries with an alias, which is a shorter name that we can use to refer to the library. This can make our code more concise and easier to read. To import a library with an alias, we can use the as keyword followed by the alias we want to use. For example, it is common to import the numpy library with the alias np, so we would use the following code:
import numpy as npNow we can try creating my_array again using the alias instead of the full library name:
my_array = np.array([1, 2, 3, 4, 5])
print(my_array)[1 2 3 4 5]
It does not really matter what you set this alias to be. For example, you could import numpy as meow_math and it would work just fine.
import numpy as meow_math
my_array = meow_math.array([1, 2, 3, 4, 5])
print(my_array)[1 2 3 4 5]
However, it is good practice to use the commonly accepted aliases for libraries. For example, numpy is commonly imported as np, pandas is commonly imported as pd, and matplotlib.pyplot is commonly imported as plt. Using these common aliases can make your code more readable and easier for others to understand.
- To ensure that you have successfully installed the libraries from the previous exercise, try running the import commands :
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns