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