Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow loading extensions via relative paths #2465

Closed
chrisbouchard opened this issue Dec 5, 2019 · 0 comments · Fixed by #6445
Closed

Allow loading extensions via relative paths #2465

chrisbouchard opened this issue Dec 5, 2019 · 0 comments · Fixed by #6445
Labels
feature request This is a feature request. help appreciated Good issue to work on suggestion A suggestion

Comments

@chrisbouchard
Copy link

chrisbouchard commented Dec 5, 2019

The Problem

I would like to use relative import paths to describe my extensions, e.g., .extensions.foo rather than requiring an absolute path like my_bot.extensions.foo. I use relative paths for package-internal imports, so it seems logical that extensions would follow the same pattern.

This does not currently work. I get the following error:

Traceback (most recent call last):
  File "/usr/lib64/python3.7/runpy.py", line 193, in _run_module_as_main
    "__main__", mod_spec)
  File "/usr/lib64/python3.7/runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "/home/chris/Projects/discord-fate-bot/discord_fate_bot/__main__.py", line 15, in <module>
    bot.load_extension('.extensions.error_handling')
  File "/home/chris/.local/share/virtualenvs/discord-fate-bot-MUSnTVTS/lib/python3.7/site-packages/discord/ext/commands/bot.py", line 621, in load_extension
    lib = importlib.import_module(name)
  File "/home/chris/.local/share/virtualenvs/discord-fate-bot-MUSnTVTS/lib64/python3.7/importlib/__init__.py", line 122, in import_module
    raise TypeError(msg.format(name))
TypeError: the 'package' argument is required to perform a relative import for '.extensions.error_handling'

It would seam this library is using importlib internally, which can handle relative paths, but is not passing enough parameters.

The Ideal Solution

I would like Bot.load_extension to take an optional second parameter, which would specify the package to resolve against. This follows the example of importlib.import_module, which takes a package parameter in the same way. If no package is provided, the path is looked up as if from a top-level script (the current behavior).

This would allow us to say

bot.load_extension('.extensions.foo', __package__)

which would load the extension relative to the current package.

The Current Solution

Currently the solution is to only use absolute paths. I use importlib.utils.resolve_name to resolve my relative paths to absolute ones:

from importlib.util import resolve_name
# ...
bot.load_extension(resolve_name('.extensions.foo', __package__))

Summary

Please allow us to load extensions using relative paths via an optional package parameter in Bot.load_extension.

This feature request looks similar to #1703, but that request wants something similar old Python 2.x-style relative paths (just concatenating package and name with a dot). I would like something compatible with Python 3 and importlib.

I would be willing to work on a merge request. I just wanted to make an issue first in case there was a conscious decision not to support relative paths. (I would still ask you to reconsider.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature request This is a feature request. help appreciated Good issue to work on suggestion A suggestion
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants