# Import the numpy library
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 functionality 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
Much of the power and appeal 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. 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 packages of functions they have shared with the community in the form of libraries. These packages can be installed and loaded into your Python environment so you can 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 are basic functions.
Libraries are directories where packages for Python are stored. Note that the terms package and library are sometimes used interchangably and there has been some 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 commonly used 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 access their functions. To do so, 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 the library 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:
# Use the array function from numpy to create an array from a list called my_array
my_array = numpy.array([1, 2, 3, 4, 5])
# Print my_array
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. We can import a library with an alias, by using 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. We can do the same with the following code:
# Import numpy with the alias, np
import numpy as npNow we can try creating my_array again with alias instead of the full library name:
# Use the array function from numpy, with alias np, to create an array from a list called my_array
my_array = np.array([1, 2, 3, 4, 5])
# Print my_array
print(my_array)[1 2 3 4 5]
It does not really matter what you set this alias to be as long as it does not conflict with existing names. For example, you could import numpy as meow_math and it would work just fine.
#Import numpy using the alias, meow_math
import numpy as meow_math
# Create a numpy array with the meow_math alias
my_array = meow_math.array([1, 2, 3, 4, 5])
# Print my numpy array
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