Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Refactoring python module configuration to avoid relative imports

This is related to a previous question of mine.

I understand how to store and read configuration files. There are choices such as ConfigParser and ConfigObj.

Consider this structure for a hypothetical 'eggs' module:

eggs/
  common/
    __init__.py
    config.py
  foo/
    __init__.py
    a.py

'eggs.foo.a' needs some configuration information. What I am currently doing is, in 'a',

import eggs.common.config
. One problem with this is that if 'a' is moved to a deeper level in the module tree, the relative imports break. Absolute imports don't, but they require your module to be on your PYTHONPATH.

A possible alternative to the above absolute import is a relative import. Thus, in 'a',

import .common.config

Without debating the merits of relative vs absolute imports, I was wondering about other possible solutions?

edit- Removed the VCS context

like image 278
saffsd Avatar asked May 13 '26 10:05

saffsd


1 Answers

"imports ... require your module to be on your PYTHONPATH"

Right.

So, what's wrong with setting PYTHONPATH?

like image 166
S.Lott Avatar answered May 15 '26 00:05

S.Lott



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!