Comments (2)
Thanks for reporting the issue.
tl;dr: You cannot perform relative imports directly from the __main__
module. Relative imports are only relative in a package. This is not a limitation nor a bug from configzen, but a natural and correct behavior of Python.
The provided example would not work even if the package argument was not None
, because the conf.py
module is not in the same directory as the my_config.py
module. You would have to import ..my_config
instead of .my_config
.
I am guessing you meant Test.wrap_module("..my_config")
and the further explanation is based on this claim.
When executing repro/module/conf.py
directly, it is impossible to relatively import repro/my_config.py
and that's just fine, because there is no parent package known to __main__
. You could probably, however, absolutely-import it:
Please cd /home/zero/Desktop/Projects/Python/
.
- Change the 8th line in
repro/module/conf.py
toTest.wrap_module("repro.my_config")
.
ExecutePYTHONPATH=. && python3 repro/module/conf.py
. There should be no error. - Change the 8th line in
repro/module/conf.py
toTest.wrap_module("my_config")
.
ExecutePYTHONPATH=repo && python3 repro/module/conf.py
. There should be no error, too.
(See PYTHONPATH
documentation here. I change its value to influence sys.path
, the reference list to resolving any imports in Python; usually people would think it's just the current working directory, but it doesn't have to be, that's why sometimes they might stumble across a seemingly impossible path-related issue which can be fixed the fastest way by adjusting PYTHONPATH
.)
If you want to wrap my_config.py
using a relative import in the described case, you must apply the rule that relative imports are only relative in a package. Hence, you cannot run repro/module/conf.py
directly, but you need to import it instead.
Follow these steps:
- Change the 8th line in
repro/module/conf.py
to the desiredTest.wrap_module("..my_config")
. - Add
main.py
(or any other name) module in the parent package (parent-parent torepro/module/conf.py
), i.e.repro/
. Your tree should look like this now:. ├── module │ └── conf.py ├── main.py └── my_config.py
- In
main.py
, placefrom repro.module import conf from repro import my_config # Enjoy your wrapped module!
cd /home/zero/Desktop/Projects/Python/ && export PYTHONPATH=. && python3 repro/main.py
.
Of course, that's not all. This structure could be contained inside thousands of directories, where the __main__
package is on top of it. And it would still work, though more readable with relative imports inside the innermost modules then.
I hope that helps. If you have any questions, let me know.
from configzen.
I'm closing this issue for now, but we may still chat here.
from configzen.
Related Issues (16)
- Configuration transclusion and query language HOT 1
- Support 3.8+
- Properly internally type-annotate binary blobs
- Add support for CLI model fields
- Support comment preservation
- Implement own async-reading utilities
- Add coverage
- CLI for config operations
- LinkedRoute
- Support for Python configuration HOT 1
- Translate camelCase to snake_case HOT 1
- Support for autosave
- Contrib: changing the configuration with paginated UIs
- Add unit tests
- Migrate to pydantic v2 HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from configzen.