Skip to content

Commit

Permalink
MAI: on Python 3, use importlib instead of imp
Browse files Browse the repository at this point in the history
because the module `imp` has been deprecated since
Python version 3.4 [1].

[1] https://docs.python.org/3.9/library/imp.html
  • Loading branch information
johnyf committed Nov 25, 2020
1 parent 7edb448 commit d26cae0
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 6 deletions.
12 changes: 9 additions & 3 deletions run_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
"""
from __future__ import absolute_import
from __future__ import print_function
import imp
# import imp # inline
# import importlib # inline
import sys
import os.path

Expand Down Expand Up @@ -81,8 +82,13 @@
except ValueError:
pass
try:
modtuple = imp.find_module("polytope", sys.path)
imp.load_module("polytope", *modtuple)
if sys.version_info.major == 2:
import imp
modtuple = imp.find_module("polytope", sys.path)
imp.load_module("polytope", *modtuple)
else:
import importlib
importlib.import_module('polytope')
except ImportError:
if require_nonlocaldir_polytope:
raise ImportError(
Expand Down
17 changes: 14 additions & 3 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@
from __future__ import print_function
from __future__ import unicode_literals

import imp
# import imp # inline
# import importlib # inline
import os
from setuptools import setup
import subprocess
import sys


classifiers = [
Expand Down Expand Up @@ -94,8 +96,17 @@ def run_setup():
f.write(commit_hash_header + '\n')
f.write(sha1 + '\n')
# Import polytope/version.py without importing polytope
version = imp.load_module('version',
*imp.find_module('version', ['polytope']))
if sys.version_info.major == 2:
import imp
version = imp.load_module('version',
*imp.find_module('version', ['polytope']))
else:
import importlib.util
spec = importlib.util.spec_from_file_location(
'version', 'polytope/version.py')
version = importlib.util.module_from_spec(spec)
sys.modules['version'] = version
spec.loader.exec_module(version)
polytope_version = version.version
setup(
name='polytope',
Expand Down

0 comments on commit d26cae0

Please sign in to comment.