The 6 AI Engineering Patterns, come build with Greg live:Β Starts Jan 6th, 2025
Leverage
Glossary

Import

Import libraries in Python

In Python,you’ll often want to bring in other pieces of code. This could be code that you wrote, or someone else wrote. The most basic case is bringing in other libraries. In order to do this, you’ll need to import.

pandas.DataFrame()
>>>NameError: name 'pandas' is not defined
import pandas
pandas.DataFrame()
>>>Nice!

Python Import

Once you import a library, its name will be a part of your python namespace. These are the keywords that python will recognize and execute.

However, if your library name is long, like matplotlib, then you may want to have a shorthand way to reference the code. This is where the keyword as comes in.

If you follow your import statement with as you’ll have a new, shorter name for your library.

Let’s look at a quick code sample.

Link to code

On this page