django-configurations icon indicating copy to clipboard operation
django-configurations copied to clipboard

DeprecationWarning on `import imp`

Open peterbe opened this issue 6 years ago • 6 comments

/Users/peterbe/virtualenvs/recipe-server/lib/python3.6/site-packages/configurations/importer.py:1: DeprecationWarning: the imp module is deprecated in favour of importlib; see the module's documentation for alternative uses
  import imp

peterbe avatar Mar 07 '18 20:03 peterbe

Thanks for the report. Please consider providing a PR to fix it, please.

blueyed avatar Mar 12 '18 05:03 blueyed

Interesting, as of today, there is no such warning anymore:

$ python
Python 3.6.7 (default, Oct 22 2018, 11:32:17)
[GCC 8.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import configurations.importer
>>> 

Though, the imp module is officially deprecated, hence it makes sense to replace the import. Unfortunately, the feature set of importlib is different from Python 2.7 to Python 3 (no find_module and load_module in Python 2), which requires a version check for the import. :unamused:

PR #212 is related to this issue, but it doesn't seem to be a real fix.

bittner avatar Dec 09 '18 16:12 bittner

@bittner You have to enable the warning.

% python -W error
Python 3.7.1 (default, Oct 22 2018, 10:41:28) 
[GCC 8.2.1 20180831] on linux
Type "help", "copyright", "credits" or "license" for more information.
Using histfile /home/user/.python_history (via pythonrc)
>>> import configurations.importer
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "…/Vcs/django-configurations/configurations/importer.py", line 1, in <module>
    import imp
  File "/usr/lib/python3.7/imp.py", line 33, in <module>
    DeprecationWarning, stacklevel=2)
DeprecationWarning: the imp module is deprecated in favour of importlib; see the module's documentation for alternative uses

blueyed avatar Dec 09 '18 17:12 blueyed

Replacing imp is fairly tricky. Too much has changed across all Python versions. Also, I'm not totally sure what ConfigurationImporter.find_module and ConfigurationLoader.load_module are doing. Whether it needs to be that complicated.

I found a related issue, though, where it's possible to simplify the current code. (PR #222)

bittner avatar Dec 09 '18 23:12 bittner

Looks like it might be possible to take some inspiration from how importhook works and/or take it as a dependency perhaps.

PeterJCLaw avatar Jun 16 '22 18:06 PeterJCLaw

This has been resolved in #365

gump avatar Sep 27 '23 11:09 gump