Proper way to parse requirements file after pip upgrade to pip 10.x.x?

前端 未结 5 1557
-上瘾入骨i
-上瘾入骨i 2020-12-16 17:17

So today I did found out that with the release of pip 10.x.x the req package changed its directory and can now be found under pip._internal.r

5条回答
  •  时光取名叫无心
    2020-12-16 18:11

    I don't agree with the accepted answer. The setup.py file can get ugly real fast if you have a large project with a lot of dependencies. It is always good practice to keep your requirements in a separate .txt file. I would do something like this -

    try:  # for pip >= 10
        from pip._internal.req import parse_requirements
        from pip._internal.download import PipSession
    except ImportError:  # for pip <= 9.0.3
        from pip.req import parse_requirements
        from pip.download import PipSession
    
    requirements = parse_requirements(os.path.join(os.path.dirname(__file__), 'requirements.txt'), session=PipSession())
    
    if __name__ == '__main__':
        setup(
            ...
            install_requires=[str(requirement.requirement) for requirement in requirements],
            ...
        )
    

    Throw in all your requirements in requirements.txt under project root directory.

提交回复
热议问题