Looking for a tutorial about the use of import with from

I’m fairly new to Python, and I’m struggling to understand some of the aspects of import and from. I am sure there are great tutorials about it, but I’m struggling to find them because I don’t know the terms to search.

I’ve noticed that sometimes I need to use both import and from. For instance, I need to use:

import pathlib
from pathlib import Path

If I omit the import pathlib statement, it fails with:

NameError: name 'pathlib' is not defined

On the other hand, I can use this:

from openpyxl import Workbook

Or this:

import openpyxl
from openpyxl import Workbook

My gut instinct is that this has something to do with namespaces, but I’m not sure what. Can anyone point me to some good resources or tutorials that explain this in a newbie-friendly way? I’ve looked a bit at this page on the official docs for import but I’m finding it difficult to understand.

I suggest to read this from documentation: 6. Modules / 6.1. More on Modules

Can’t reproduce. from pathlib import Path works fine in a fresh REPL.

I’m fairly new to Python, and I’m struggling to understand some of the
aspects of import and from. I am sure there are great tutorials
about it, but I’m struggling to find them because I don’t know the
terms to search.

I’ve noticed that sometimes I need to use both import and from. For instance, I need to use:

import pathlib
from pathlib import Path

If I omit the import pathlib statement, it fails with:

NameError: name 'pathlib' is not defined

As mentioned, others cannot reproduce this, and it is not expected.

Do you get that NameError from exactly those 2 lines of code? Or do
you get it from a larger piece of code?

If the latter, note that it is best to produce a complete example which
exhibits the problem: it is very common that people supply only the
lines they think are the source of the problem; we all do this by
inclination.

My personal suspicion is that this fails in a larger piece of code, and
that larger code has something like this:

 P = pathlib.something(....)

in it. That line tries to use the name pathlib and if that hasn’t been
imported, it will be unknown. Reciting the full traceback with the
NameError is important here - it should include the very line which
raised the error.

Here’s an interactive example with no import pathlib line:

 >>> from pathlib import Path
 >>> P = Path('/foo')
 >>> P = pathlib.Path('/foo')
 Traceback (most recent call last):
   File "<stdin>", line 1, in <module>
 NameError: name 'pathlib' is not defined
 >>>

See that the NameError is because of the expression pathlib.Path,
which inherently needs the name pathlib to be defined in order to
work.

Imports tend to come in 2 basic forms: importing a module:

 import pathlib

and importing some names from a module:

 from pathlib import Path

The essential things to realise are (a) both form cause the pathlib
module to be loaded into the Python interpreter and (b) imports are a
type of assignment statement.

The former import assigns a reference to the pathlib module to the
local name pathlib in your code. The latter assigns a reference to the
Path factory function from the pathlib module to the local name
Path in your code.

So one defines the name pathlib locally and the other defines the name
Path locally.

Which you use depends on your personal style and on names you want to
use.

When you want just a few functions/classes etc from a module, it is
common to import just those names, eg:

 from pathlib import Path, PosixPath

This defines just Path and PosixPath in your code - the name
pathlib is not a local name.

If you expect to a lot of top level names from a module you might not
import each name individually, and instead import the module itself as a
local name, eg:

 import pathlib

This defines the local name pathlib as a reference to the module, and
you can use expressions like pathlib.Path to access things inside it.
A common example is the numerical package numpy, which has many top
level names. It is common to go:

 import numpy as np

which defined the local name np as a reference to the numpy module
and then one accesses things from numpy like these example lines:

 longitudes = np.linspace(0, 2 * pi, num=steps * 2 + 1)
 latitudes = np.linspace(-pi / 2, pi / 2, num=steps + 1)
 f = np.nan_to_num(f_left) + np.nan_to_num(f_right)
 return np.array(self[start:stop], self.np_type)

Each of these accesses things from the module via the nd local name as
nd.linspace or nd.nan_to_num and so forth.

Cheers,
Cameron Simpson cs@cskk.id.au

@abessman and @cameron: You’re right about the pathlib issue–I did have a reference to pathlib.Path later on, so it was failing without the import pathlib statement.

@cameron: For the rest of it, thank you that’s EXTREMELY helpful. Because I didn’t know all the terms, I was struggling to find out why some tutorials might just have you import the whole module, some might have you just import parts, and some might import it with an as statement. I appreciate the time you spent typing that up.