praiskup / argparse-manpage

Automatically build man-pages for your Python project
Apache License 2.0
40 stars 21 forks source link

relative module import #73

Closed chapmanjacobd closed 1 year ago

chapmanjacobd commented 1 year ago

It would nice to be able to import relative modules. or rather argparse-manpage has trouble seeing from the current working directory for absolute module loading

~/ $ python -m xklb.lb
# python loads from site-packages

~/python_project/ $ python -m xklb.lb
# python loads from CWD

~/python_project/ $ argparse-manpage --module xklb.lb ...
# argparse-manpage loads from site-packages instead of CWD

~/python_project/ $ argparse-manpage --module .xklb.lb ...

  File "/home/xk/.local/lib/python3.11/site-packages/argparse_manpage/tooling.py", line 66, in get_parser
    return get_parser_from_module(import_from, objname, objtype, prog=prog)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/xk/.local/lib/python3.11/site-packages/argparse_manpage/tooling.py", line 28, in get_parser_from_module
    mod = importlib.import_module(module)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib64/python3.11/importlib/__init__.py", line 121, in import_module
    raise TypeError(msg.format(name))
TypeError: the 'package' argument is required to perform a relative import for '.xklb.lb'
praiskup commented 1 year ago

Thank you for the report! Seems like this would be useful, though I'm not really sure how to implement correctly. In particular, I'm not able to let bin/argparse-manpage behave the same way setup.py use-case would behave. Seems like we'd have to modify sys.path directly in bin/argparse-manpage (which is a generated file). Ideas? Patches are welcome!

ssbarnea commented 1 year ago

TBH, I am against that proposal. Relative module imports are known to be problematic and dangerous.

praiskup commented 1 year ago

If we had a good test for the whole matrix of possible import combinations, I wouldn't be afraid of adding the support. But I agree that absolute imports have much more "expectable" effects.

I experimented with this for some time already, and haven't found a solution anyway. Closing, feel free to submit patches though.