diff --git a/pytest-virtualenv/pytest_virtualenv.py b/pytest-virtualenv/pytest_virtualenv.py index dbaf45b4..b0524a1c 100644 --- a/pytest-virtualenv/pytest_virtualenv.py +++ b/pytest-virtualenv/pytest_virtualenv.py @@ -2,10 +2,9 @@ """ import os import sys -from distutils import sysconfig +import importlib_metadata as metadata from pytest import yield_fixture -from pkg_resources import working_set try: from path import Path except ImportError: @@ -178,8 +177,12 @@ def install_package(self, pkg_name, installer='easy_install', build_egg=None): False: Runs 'python setup.py develop' None (default): installs the egg if available in dist/, otherwise develops it """ - installed = [p for p in working_set if p.project_name == pkg_name] - if not installed or installed[0].location.endswith('.egg'): + def location(dist): + return dist.locate_file('') + + installed = [ + dist for dist in metadata.distributions() if dist.name == pkg_name] + if not installed or location(installed[0]).endswith('.egg'): if sys.platform == 'win32': # In virtualenv on windows "Scripts" folder is used instead of "bin". installer = str(self.virtualenv / 'Scripts' / installer + '.exe') @@ -191,16 +194,17 @@ def install_package(self, pkg_name, installer='easy_install', build_egg=None): # This is to circumvent #! line length limits :( cmd = '%s %s %s' % (self.python, installer, pkg_name) else: - pkg = installed[0] + dist = installed[0] d = {'python': self.python, 'easy_install': self.easy_install, - 'src_dir': pkg.location, - 'name': pkg.project_name, - 'version': pkg.version, - 'pyversion': sysconfig.get_python_version(), + 'src_dir': location(dist), + 'name': dist.name, + 'version': dist.version, + 'pyversion': '{sys.version_info[0]}.{sys.version_info[1]}' + .format(**globals()), } - d['egg_file'] = Path(pkg.location) / 'dist' / ('%(name)s-%(version)s-py%(pyversion)s.egg' % d) + d['egg_file'] = Path(location(dist)) / 'dist' / ('%(name)s-%(version)s-py%(pyversion)s.egg' % d) if build_egg and not d['egg_file'].isfile(): self.run('cd %(src_dir)s; %(python)s setup.py -q bdist_egg' % d, capture=True) @@ -222,8 +226,8 @@ def installed_packages(self, package_type=None): raise ValueError('invalid package_type parameter (%s)' % str(package_type)) res = {} - code = "from pkg_resources import working_set\n"\ - "for i in working_set: print(i.project_name + ' ' + i.version + ' ' + i.location)" + code = "import importlib_metadata as metadata\n"\ + "for i in metadata.distributions(): print(i.name + ' ' + i.version + ' ' + i.locate_file(''))" lines = self.run([self.python, "-c", code], capture=True).split('\n') for line in [i.strip() for i in lines if i.strip()]: name, version, location = line.split() diff --git a/pytest-virtualenv/setup.py b/pytest-virtualenv/setup.py index ff63719e..4ea205d7 100644 --- a/pytest-virtualenv/setup.py +++ b/pytest-virtualenv/setup.py @@ -26,6 +26,7 @@ 'pytest-shutil', 'pytest', 'virtualenv', + 'importlib-metadata', ] tests_require = [