This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Come up with a good way to handle module aliasing
Type: enhancement Stage: test needed
Components: Library (Lib) Versions: Python 3.6
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: barry, brett.cannon
Priority: normal Keywords:

Created on 2015-11-17 17:07 by brett.cannon, last changed 2022-04-11 14:58 by admin.

Messages (2)
msg254805 - (view) Author: Brett Cannon (brett.cannon) * (Python committer) Date: 2015-11-17 17:07
If you look at things like https://github.com/kennethreitz/requests/issues/2870 and https://pythonhosted.org/six/#module-six.moves you start to notice that users do on occasion have a need to alias module names. It might be worth thinking through what that might entail and provide a solution.

Historically it has been done with a meta path importer which catches the import for some old module name, imports the real name, and then sets sys.modules under the alternative name before returning that the import was successful. It's a little tricky to get right, though, since you essentially have to pause an import while another one is going on, make sure you wait to the last minute to do the sys.modules aliasing to make sure no object swap handled from underneath you. I also have not thought through the best way to do this with module specs., but maybe there is a better way. There's also making sure locking is handled properly.

The reason this might be useful to have in importlib is it would help facilitate module renamings. If you set it up so that it isn't hard to turn on with mappings of old name to new along with whether you want to raise an ImportWarning about the aliasing then others may find it useful.
msg254806 - (view) Author: Barry A. Warsaw (barry) * (Python committer) Date: 2015-11-17 17:15
Another example is the six.moves module, though I haven't looked at its implementation approach.
History
Date User Action Args
2022-04-11 14:58:23adminsetgithub: 69835
2015-11-17 17:15:47barrysetmessages: + msg254806
2015-11-17 17:14:31barrysetnosy: + barry
2015-11-17 17:07:40brett.cannoncreate