4 ways to import a module in Python

Transcript

Let's talk about the four ways to import something from a module in Python.

Importing the whole module object

The first way is to use the import statement, using the syntax import module_name:

>>> import math

We've just imported the math module. We can use the things within this module by looking up attributes on the math module object. For example we can get the square root of a number by calling math.sqrt:

>>> math.sqrt(25)
5.0

Or we can access the number π with math.pi:

>>> math.pi
3.141592653589793

But we can't access just pi or sqrt (without that math. prefix):

>>> pi
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'pi' is not defined
>>> sqrt(25)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'sqrt' is not defined
>>>

The reason is, when we import a module using that import module_name syntax, we get just one thing: a variable that points to a module object.

>>> import math
>>> math
<module 'math' (built-in)>

Everything in that module is accessible as an attribute on that module object:

>>> math.pi
3.141592653589793

Importing specific things from a module

If you wanted to just type pi instead of math.pi, you could use the from syntax for importing:

>>> from math import pi
>>> pi
3.141592653589793

So now we have pi, but not sqrt (because we didn't import it):

>>> sqrt(25)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'sqrt' is not defined

And we don't have access to the whole math module:

>>> math
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'math' is not defined

If you want to import multiple things from a module using this from syntax, you could put commas each thing you'd like to import:

>>> from math import pi, sqrt
>>> pi
3.141592653589793
>>> sqrt(25)
5.0

That from math import pi, sqrt line plucks out pi and sqrt from the math module, but nothing else.

Avoiding name collision when importing

If you use the from syntax to grab specific things from a module and you grab something of the same name from two different modules, you're in for trouble.

Here we're importing sqrt from the math module and sqrt from the cmath module (cmath is for complex numbers):

>>> from math import sqrt
>>> from cmath import sqrt

The sqrt function we end up with came from the cmath module:

>>> sqrt(25)
(5+0j)

We imported two different functions named sqrt and the last one won. We first got sqrt from the math module and then we overwrote our local sqrt variable with the sqrt function from the cmath module.

To fix this we could rename sqrt from the cmath module as we import it, by using the as syntax:

>>> from math import sqrt
>>> from cmath import sqrt as csqrt

So, sqrt is now the sqrt function from the math module and csqrt is the one from the cmath module:

>>> sqrt(25)
5.0
>>> csqrt(25)
(5+0j)

If you look at the the function the csqrt variable points to, you'll even see that it's actually called sqrt:

>>> csqrt
<built-in function sqrt>

We pointed our local variable csqrt to the sqrt function within the cmath module.

Importing a module under a different name

You can also use the as syntax when importing a whole module.

>>> import math as m

The variable m now points to the math module object which means we can't say math.pi anymore:

>>> math.pi
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'math' is not defined

Instead we can use m.pi or m.sqrt:

>>> m.pi
3.141592653589793
>>> m.sqrt(25)
5.0

You likely won't see this convention used often except in specific Python communities where it's commonplace.

For example, in the Pandas world people often import pandas as pd:

>> import pandas as pd

And in the NumPy world, people often import numpy as np:

import numpy as np

It's not very common to see module names shortened using the as syntax, except in areas where it's conventional (like Pandas and NumPy).

Trey says: If I saw m.pi in your code, I'd probably find it a little bit odd.

The 4 ways to import a module

So there's four different ways to import:

  1. Import the whole module using its original name: pycon import random
  2. Import specific things from the module: pycon from random import choice, randint
  3. Import the whole module and rename it, usually using a shorter variable name: pycon import pandas as pd
  4. Import specific things from the module and rename them as you're importing them: pycon from os.path import join as join_path

That last one is usually done to avoid a name collision or sometimes to make a more descriptive name (though that's not very common).

Trey's recommendations

  1. Use from for short and descriptive variable names I tend to use the from syntax most (number 2 above) because I prefer short and descriptive variable names.

  2. Import the whole module if needed to avoid ambiguity. If there's a name like choice that isn't as clear as random.choice, then I prefer to import the whole module for a more descriptive name

  3. Avoid renaming imports. I very rarely use the as syntax (unless I'm in the pandas or numpy worlds, where it's common convention). And I almost never use the as syntax with from unless I'm avoiding a name collision.