Skip to content

imp module is deprecated in v3 so in place of it using importlib - #96

Merged
grisha merged 1 commit into
grisha:masterfrom
subrahmanya-rao:master
Oct 19, 2020
Merged

grisha merged 1 commit into
grisha:masterfrom
subrahmanya-rao:master

Conversation

@subrahmanya-rao

Copy link
Copy Markdown
Contributor

There are two issues in the existing code.

  1. imp module has been deprecated since python v3.4
  2. Behavior of imp module has changed between 2.7 and 3.5

Here is the issue as mentioned in (2) above with imp module in v3.5.
There is package named "foo" and it contains a module "common". When common module is compiled into .pyc file then finding followed by loading using imp module does not work. The same works with importlib module. Here is the output of both the scenarios.

f, p, d = imp.find_module("foo")
print (f, p)
None /usr/local/lib/python3.5/dist-packages/foo
print (f, p, d)
None /usr/local/lib/python3.5/dist-packages/foo ('', '', 5)
imp.load_module("common", f, p , d)
Traceback (most recent call last):
File "", line 1, in
File "/usr/lib/python3.5/imp.py", line 244, in load_module
return load_package(name, filename)
File "/usr/lib/python3.5/imp.py", line 210, in load_package
raise ValueError('{!r} is not a package'.format(path))
ValueError: '/usr/local/lib/python3.5/dist-packages/foo/init.py/init.pyc' is not a package

import importlib
importlib.load_module("foo.common");
Traceback (most recent call last):
File "", line 1, in
AttributeError: module 'importlib' has no attribute 'load_module'
importlib.import_module("foo.common");
<module 'foo.common' from '/usr/local/lib/python3.5/dist-packages/foo/common.pyc'>

The changes here is to just use the new module importlib and not use imp.
These changes are tested with version 3.5.

@grisha
grisha merged commit 88e4444 into grisha:master Oct 19, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants