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

639
Views
How to import from the __init__.py in the same directory?

Suppose I have a module rules with the following structure:

rules
├── conditions.py
├── __init__.py

In the script conditions.py, I'd like to import a class called RuleParserError defined in __init__.py. However, I haven't been able to figure out how to do this. (Following Python: How to import from an __init__.py file? I've tried

from . import RuleParserError

but this leads to an ImportError: cannot import name RuleParserError when trying to run conditions.py as __main__).

over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

I see 'import from parent module' as an anti-pattern in Python. Imports should be the other way around. Importing from modules's __init__.py is especially problematic. As you noticed, importing module foo.bar from foo/bar.py involves importing foo/__init__.py first, and you may end up with a circular dependency. Adding a print("Importing", __name__) to your init files helps see the sequence and understand the problem.

I'd suggest that you moved the code you want to import in conditions.py from __init__.py to a separate lower-level module, and just import some names from that module in __init__.py to expose it at higher level.

Let's suppose that you had some class Bar in your __init__.py. I'd reorganize it the following way.

__init__.py:

from bar import Bar  # exposed at the higher level, as it used to be.

bar.py:

class Bar(object): ...

conditions.py:

from . import Bar  # Now it works.

Ideally an __init__.py should contain nothing but imports from lower-level modules, or nothing at all.

over 4 years ago · Santiago Trujillo Report

0

If you get

ImportError: attempted relative import with no known parent package

when you do like

import . from something

especially from the script executed, just try

from __init__ import something

Even though it could be problematic when there are many __init__.pys in sys.path, it would be helpful in some simple situaions.

over 4 years ago · Santiago Trujillo Report

0

adding __package__ on top the script

__package__ = "pkg.dir.dir"

resolving the issue

in this case

__package__ = "rules"
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!