I'm having trouble importing a variable from my config/__init__.py
file.
My directory looks like:
.
├── src
│ ├── config
│ │ ├── __init__.py
│ │ ├── base_config.py
│ │ ├── log_config.py
│ │ └── output_config.py
│ ├── script.py
In __init__.py
I have from config.output_config import output_data
. This file also contains a line BUCKET = ...
In my output_config.py
file I have from config import BUCKET
, which is them used in the function output_data()
.
In script.py
I have import config
and a call to config.output_data()
When I run script.py, I get the following error:
Traceback (most recent call last):
File ".../src/config/output_config.py", line 15, in <module>
from config import BUCKET
File ".../src/config/__init__.py", line 7, in <module>
from config.output_config import output_data
File ".../src/config/output_config.py", line 15, in <module>
from config import BUCKET
ImportError: cannot import name 'BUCKET'
It seems like my issue is probably polluting the name space (..and making a circular reference..?), but I'm not sure how to resolve the issue. It's my understanding that the __init__.py
file is best used to import other config files, but then I'm not sure how to get my variable BUCKET
into the output_config.py
file where I need it. Would appreciate help resolving the issue and understanding best practices for structuring projects as it relates to importing. Thanks!
I would say get that variable BUCKET to a different python file or a settings.py if it is a settings or constant as you are incurring in circular imports because of it.
Update from comments:
Create a settings.py
or constants.py
file and put the settings/constants there, and try to avoid any imports in __init__.py
unless it is a standard library import to avoid any potential circular reference on imports.
One thing to remember is that anything you put in __init__.py
is loaded whenever you import any module in that package so you need to be careful what you put there and try to keep it short.