Coder Social home page Coder Social logo

Comments (2)

bswck avatar bswck commented on June 8, 2024

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/.

  1. Change the 8th line in repro/module/conf.py to Test.wrap_module("repro.my_config").
    Execute PYTHONPATH=. && python3 repro/module/conf.py. There should be no error.
  2. Change the 8th line in repro/module/conf.py to Test.wrap_module("my_config").
    Execute PYTHONPATH=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:

  1. Change the 8th line in repro/module/conf.py to the desired Test.wrap_module("..my_config").
  2. Add main.py (or any other name) module in the parent package (parent-parent to repro/module/conf.py), i.e. repro/. Your tree should look like this now:
    .
    ├── module
    │   └── conf.py
    ├── main.py
    └── my_config.py
    
  3. In main.py, place
    from repro.module import conf
    
    from repro import my_config  # Enjoy your wrapped module!
  4. 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.

bswck avatar bswck commented on June 8, 2024

I'm closing this issue for now, but we may still chat here.

from configzen.

Related Issues (16)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.