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:
import library1 import library2 import library3 import library4
Or you could use import
and split libraries with a comma on the same line.
import library1, library2, library3, library4
import libraries as aliases
Sometimes, to simplify your code and how you will call some Python functions, you can define aliases for your libraries. It works like this: import
+ {the name of your library}
+ as
+ {alias}
Let's take a generic example (and we will see some examples below):
import library1 as lib1
Note: you can definitely use any alias to call a library later in your scripts, but there are some conventions.
How to import famous libraries in Python
Let's see together some examples of "famous" libraries and how to import them.
Import pandas
import pandas as pd
pd
is usually the alias for pandas
, this is one of the conventions.
First, you will have to verify if the library or package is installed on your environment (or your machine or server).
Note: you might find different terminologies for libraries, it could also be called packages or modules.
Import numpy
import numpy as np
The convention for numpy
is to use np
as an alias.
Import re
import re
Here, there is no need to use an alias because re
is only two letters and that is short enough.
Import json
import json
No alias is needed in this example.