Skip to content
SEO & Data Specialist | Arthur Camberlein
TwitterFRBlog FR

How to import Python libraries

Python, Data1 min read

First you will have to verify if the library/the package is installed on you environement (or your machine or server).

Note: you might found different terminologies for libraries, it could also be called packages or modules.

Libraries in Python like real libraries - Arthur Camberlein

This import will work if you are using any version of Python (meaning Python 2 or Python 3).

How to import a library

To import a library, you will have to use import + {the name of your library} So you could do this to import libraries one by one:

1import library1
2import library2
3import library3
4import library4

Or you could use import and split libraries with a comma on the same line.

1import library1, library2, library3, library4

import libraries as aliases

Sometime and to simplify your code and how you will call some python fuction, you will be able to define some aliases for your libraries. It works that way: import + {the name of your library} + as + {alias}

Let's take a generic example (and we will see some examples below):

1import library1 as lib1

Note: you can definitely use any alias to call later a library on your scripts ... but there are some conventions.

How to import famous libraries in Python

Let's see together some example of "famous" libraries and how to import them.

Import pandas

1import pandas as pd

pd is usually the alias of pandas, this is one of the convention.

Import numpy

1import numpy as np

The convention for numpy is to use np as an alias.

Import re

1import re

Here there is not need to use an alias because re is only two letters and that is short enough.

Import json

1import json

No alias in this example.

© 2023 by SEO & Data Specialist | Arthur Camberlein. All rights reserved.
Theme by LekoArts