如何将setup.py / install_requires中的值与Pipfile / packages同步

时间:2018-03-26 17:11:31

标签: python pipenv pipfile

如果您处理同时使用setup.pyPipfile的项目,则通常会在Pipfile/[packages]setup.py/install_requires中找到相同的值。

有谁知道我如何告诉Pipfile使用setup.py/install_requires [packages]的值?

1 个答案:

答案 0 :(得分:0)

在您的setup.py内:

  1. 定义一个读取节的函数:

    def locked_requirements(section):
    """Look through the 'Pipfile.lock' to fetch requirements by section."""
        with open('Pipfile.lock') as pip_file:
            pipfile_json = json.load(pip_file)
    
        if section not in pipfile_json:
            print("{0} section missing from Pipfile.lock".format(section))
            return []
    
        return [package + detail.get('version', "")
                for package, detail in pipfile_json[section].items()]
    
  2. setup函数中,从default部分返回列表:

    setup(
        # ...snip...
        install_requires=locked_requirements('default'),
        # ...snip...
    )
    

重要提示 :将Pipfile.lock包含在MANIFEST.in中,例如:

include Pipfile.lock