Kurt Peek
Kurt Peek

Reputation: 57791

How do I import a module from the same directory explicitly in Python 2?

I'm looking for a clarification of the following statement in PEP 8:

Implicit relative imports should never be used and have been removed in Python 3.

Suppose that in Python 2 I have the following directory structure (in a directory called test):

.
├── test_recurring_interval.py
└── test_utils.py

In the file test_recurring_interval.py, I have a line

import test_utils

Is this an implicit relative import? If so, how should I change it to make it explicit?

Upvotes: 2

Views: 139

Answers (2)

user2357112
user2357112

Reputation: 282158

Whether this is an implicit relative import depends on whether the module containing the import statement is part of a package. This is trickier than it might at first seem, since whether a module is part of a package depends on how Python was executed and how __package__ and sys.path are set.

It doesn't look like there are any packages involved here. This is an absolute import, then, not a relative import.

Upvotes: 2

Stephen Rauch
Stephen Rauch

Reputation: 49862

An explicit relative import is explicitly relative when it starts with a ..

Implicitily relative:

import test_utils

Explicitily relative:

import .test_utils

Upvotes: 1

Related Questions