Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

379
Views
What is Python implicit relative import

In PEP 8 -- Style Guide for Python Code

Explicit relative imports are an acceptable alternative to absolute imports

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

What Is Python Implicit Relative Import?

Implicit import is a algorithm

Search up from current package directory until the ultimate package parent gets hit.
-- From https://www.python.org/dev/peps/pep-0328/#rationale-for-relative-imports

Can someone explain it in detail?

Removed In Python3?

python2 -c 'import csv; print(csv)'
<module 'csv' from '/usr/local/Cellar/python/2.7.13/Frameworks/Python.framework/Versions/2.7/lib/python2.7/csv.pyc'>

$ touch csv.py

$ python2 -c 'import csv; print(csv)'
<module 'csv' from 'csv.pyc'>

# In python3 still search from current package
$ python3 -c 'import csv; print(csv)'
<module 'csv' from '/path_to/csv.py'>

Why does pep-0008 suggest never use it?

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

When you say:

import foo

Python 2 would look first in the caller's directory. Python 3 will not do that, and will only find foo in the usual places like sys.path (PYTHONPATH, site-packages, etc.).

This means if you're writing a package that supports Python 3, you should say this inside your package:

import mypkg.foo

Or use an explicit relative import:

from . import foo
over 4 years ago · Santiago Trujillo Report

0

in python3, your code only works because python3 adds the parent directory of the python module you call to the sys.path

it won't work if you use submodule and use implicit relative import from submodule.

mkdir -p smod
touch smod/csv.py
echo 'import csv; print(csv)' > smod/__init__.py
python3 -c 'import smod; print(smod)'
<module 'csv' from '</path/to>/lib/python3.9/csv.py'>
<module 'smod' from '/<home>/test/smod/__init__.py'>

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!