python 3.7.6 pip setup.py test with ContextualVersionConflict

Viewed 950

my setup.py is like below:

from setuptools import find_packages, setup

tests_require = ['pytest-env', 'pytest-mock', 'pytest-cov', 'pytest-xdist', 'pytest', 'mock', 'moto<=1.3.10']

setup(
    name='repo_name',
    version='0.1.0',
    description='repo_name',
    keywords=['?'],
    packages=find_packages('src', exclude=['tests', 'venv']),
    package_dir={'': 'src'},
    package_data={'': ['schema/*.yaml']},
    install_requires=[
        'boto3<=1.10.19',
        'requests<=2.22.0',
        'jsonschema<=3.0.1',
        'objectpath<=0.6.1',
        'pyyaml<=5.1.1',
        'sqlalchemy<=1.3.5',
        'psycopg2-binary<=2.8.3',
        'auth-client<=1.0.23',   # internal package
        'policy_client<=1.0.9',  # internal package
        'audit-client<=1.1.20',  # internal package
        'flask<=1.1.1',
        'click<=7.0',
        'Werkzeug<=0.15.5',
        'itsdangerous<=1.1.0',
        'Jinja2<=2.10.1',
        'MarkupSafe<=1.1.1',
        'structlog<=19.2.0',
        'python-rapidjson<=0.9.1'
    ],
    setup_requires=['pytest-runner'],
    tests_require=tests_require,
    extras_require={'test': tests_require},
    include_package_data=True,
    zip_safe=False
)

When I run python setup.py test, it will always reinstall the most of the packages that I already installed into .eggs folder, which I understand. While it's installing the package with different versions that I provided in the install_requires section, which results in below error:

pkg_resources.ContextualVersionConflict: (urllib3 1.25.10 (/Users/***/Desktop/repo/.eggs/urllib3-1.25.10-py3.7.egg), Requirement.parse('urllib3<1.25,>=1.21.1'), {'requests'})

I have to pin the requests version to 2.22.0 which requires urllib3 version no larger than 1.25, which caused my issue. So, is there anyway I can pin the urllib3 version in somewhere so when it runs, it will only install the provided version? I have tried to add the urllib3 version in install_requires, tests_require and in requirements.txt, but no luck so far.

2 Answers

Probably not what you want to hear but... setup_requires and tests_require are deprecated. Although they (probably) still work (mostly) fine, you might want to look for alternative solutions. As far as I know the most common alternative solutions are to use Tox instead of tests_require and PEP 517 to replace setup_requires.

In your case, it seems that your usage of setup_requires is only a consequence of your usage of tests_require. So by switching to something like Tox you get rid of both tests_require and setup_requires.

I got it resolve to simply replace python setup.py test to be pip instll -e . [test], and then pytest directly.

It will still install all the tests_require packages and then run pytest directly. Instead of going through all the list packages and find the best match versions for all packages.

UPDATES:

The real problem is I did not remove the old .eggs/ and venv/ folder when I made the packages version change. So the solution is updating the requests version to be 2.21.0 in the setup.py file, then remove the .eggs/ and venv/ folder and rerun everything.

Related