Installing and Importing Libraries
Learn how to bring powerful tools into your notebooks by importing Python libraries, and when you might need to install them outside Google Colab.
Skill Level: Beginner
Prerequisites: Introduction to Pandas
Estimated Time: 20 minutes
Story Time
You have just learned about Pandas and seen how it can help you work with data.
Now imagine you want to add more tools: one for numerical calculations, one for charts, and one for interactive visualizations. You could try to write everything yourself, but that would be slow and error‑prone.
Instead, Python lets you import libraries that already solve these problems. In Google Colab, most of these libraries are already installed, and you simply need to import them into your notebook.
What You’ll Learn
By the end of this lesson, you will be able to:
- Explain what a Python library is.
- Import libraries into a Python program.
- Understand aliases such as
pdandnp. - Identify commonly used libraries in data science.
- Know when
pip installis needed outside Colab.
Why This Topic Matters
Python becomes much more powerful when you use libraries.
Rather than writing every feature from scratch, you can reuse code that other developers have packaged into tools for tasks like:
- Reading CSV files
- Performing mathematical calculations
- Creating charts
- Building machine learning models
Throughout this guide, you will rely on these libraries to work with data efficiently. In Colab, your main job is to import them correctly and understand what each one does.
What Is a Library?
A library is a collection of pre-written code designed to perform specific tasks.
Instead of writing everything yourself, you can import a library and use its functions in your program.
You can think of a library as a toolbox:
- The toolbox contains many tools.
- Each tool is designed for a particular job.
- You take the tool you need and use it when required.
Common Python Libraries
Here are some libraries you will use often in data work:
| Library | Purpose |
|---|---|
| Pandas | Data analysis |
| NumPy | Numerical computing |
| Matplotlib | Static visualizations |
| Seaborn | Statistical visualizations |
| Plotly | Interactive visualizations |
Together, these libraries form the foundation of many data science workflows.
Libraries in Google Colab
Google Colab already includes many popular libraries, including:
- Pandas
- NumPy
- Matplotlib
- Seaborn
- Plotly
In most cases, you do not need to install them manually. You only need to import them at the top of your notebook.
You will still see pip install commands in tutorials and documentation, but those are mainly needed when:
- You run Python on your own machine.
- You use a library that Colab does not include by default.
Importing Libraries
Importing a library allows you to use it in your current Python program.
Example:
import pandas
After importing, you can call functions from the library in your code.
Using Aliases
Many libraries are imported using standard aliases to keep code short and readable.
import pandas as pd
Here:
pandasis the library name.pdis the alias used in your code.
Instead of writing:
pandas.read_csv()
you can write:
pd.read_csv()
This alias is a widely used convention in Python data analysis.
Common Import Statements
You will often see imports like these in data projects:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
import plotly.express as px
Over time, these names will become familiar, and you will know which library to use for each task.
Importing Specific Functions
Sometimes, you only need a single function from a library.
Example:
from math import sqrt
print(sqrt(25))
Output:
5.0
This pattern lets you use sqrt directly without writing math.sqrt.
When Do You Use pip install?
Outside Colab, or when working on your own computer, you may need to install libraries before importing them.
Most Python libraries can be installed using pip, which is Python’s package manager.
Example (run in a terminal, not inside a Python cell):
pip install pandas
To install several libraries at once:
pip install pandas numpy matplotlib seaborn plotly
In Google Colab:
-
For the core libraries used in this guide, you usually do not need
pip install. -
If you ever use a library that is not available, you can install it in a code cell using:
!pip install some-library-name
But for this course, you can safely focus on importing rather than installing.
Worked Example
import pandas as pd
import numpy as np
print("Pandas Version:", pd.__version__)
print("NumPy Version:", np.__version__)
This simple program checks that both libraries are available and shows which versions are installed.
Practice in the Notebook
Run the following code in Google Colab:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
import plotly.express as px
print("Pandas:", pd.__version__)
print("NumPy:", np.__version__)
If no errors appear, the libraries have been imported successfully.
Then answer:
- What is the difference between installing a library and importing a library?
- Why do most Python programs use aliases such as
pdandnp? - Which library would you use to create an interactive chart?
You can write your answers directly in the notebook.
Independent Exercise
Research one Python library that is not covered in this guide.
Find out:
- What problem does it solve?
- Who commonly uses it?
- One example application.
Summarize your findings in two or three sentences.
Common Mistakes
Forgetting to Import a Library
Incorrect:
df = pd.read_csv("students.csv")
This will produce an error because pd has not been defined.
Correct:
import pandas as pd
df = pd.read_csv("students.csv")
Confusing Installation and Importing
Installing (usually outside Colab):
pip install pandas
Importing (inside your Python code or notebook):
import pandas as pd
Installing is usually done once per environment. Importing is done in every Python program or notebook that uses the library.
Using a Non-Standard Alias
Incorrect:
import pandas as my_library
This is valid Python, but it makes your code harder for others to read. Using common aliases such as pd and np helps keep code consistent.
Key Takeaways
In this lesson, you learned that:
- A library is a collection of pre-written code for specific tasks.
- In Google Colab, core data libraries (Pandas, NumPy, Matplotlib, Seaborn, Plotly) are already installed; you mainly need to import them.
- Aliases like
pd,np,plt,sns, andpxare common in data science code. pip installis used to install libraries, mainly when working outside Colab or with less common libraries.
Self Evaluation
Check Your Understanding
1. What is a Python library?
2. Which command is used to install a library like Pandas?
3. What is the difference between installing and importing a library?
4. Why do many programs use aliases like pd and np?
5. Which library would you typically use to create an interactive chart?
Continue Your Journey
You are now ready to start working with real datasets.
The next topic is Reading CSV Files, where you will learn how to load tabular data like the StudentsPerformance dataset into Pandas DataFrames.