Unable to create a import module

Viewed 53

I was having trouble with circular dependencies, only to find some advice online that I'm better off importing an entire module rather than specific items within a module. Prior to the circular dependency issue, I was having no issue with imports.

So with the blog's advice, I setup a __init__.py in a folder containing a few models, imported each there, and now when I try to import from that module, I'm being told that module doesn't have an attribute I'm looking for.

So I have this file structure:

└── root
    ├── models
    │   ├── __init__.py
    │   ├── a.py
    │   └── b.py
    └── c.py

In __init__.py, I have

from models.a import A
from models.b import B

And in c.py

import models

# code

models.A.func()

But I get the error module 'models' has no attribute 'A'. As A and B both reference each other, I was getting circular imports at first, but now that they're resolved, I can't get the models to show up at all.

2 Answers

You either need to add your project's directory to PYTHONPATH or you need to do

from . import models

your __init__.py file doesn't require any change. I think in c.py file, type as follows :

modele_name.function_name()

If u have imported that function in your __init__.py file, this will help you, I suppose. For more details, please edit your question and add the git link of your repo

Related