Is there a way to have a conditional requirements.txt file for my Python application based on platform?

前端 未结 4 1582
陌清茗
陌清茗 2020-12-02 15:06

I have a python application that I wrote to be compatible with both, Linux and Windows platforms. However there is one problem... One of the python packages I need for Windo

相关标签:
4条回答
  • 2020-12-02 15:41

    You can add additional requirements to any package after a semicolon. You may limit any package with multi-condition by and, or. more conditions: https://www.python.org/dev/peps/pep-0508/#environment-markers

    examples:

    futures>=3.0.5; python_version < '3.0'
    futures>=3.0.5; python_version == '2.6' or python_version=='2.7'
    futures>3 ; python_version >= "3.6" and sys_platform == "linux"
    futures>3.3 ; python_version >= "3.6" and sys_platform == "darwin"
    
    0 讨论(0)
  • 2020-12-02 15:42

    Use this in the requirements.txt file

    uwsgi==2.0.18; platform_system != "Windows"
    

    in this case pip will install uwsgi if not running on Windows

    0 讨论(0)
  • 2020-12-02 16:03

    You can add certain conditional requirements after a semi-colon particularly useful for sys_platform and python_version.

    Examples:

    atomac==1.1.0; sys_platform == 'darwin'
    futures>=3.0.5; python_version < '3.0'
    futures>=3.0.5; python_version == '2.6' or python_version=='2.7'
    

    Apparently you can also exclude particular versions of a library:

    futures>=3.0,!=3.0.5
    

    They are defined in PEP 508 and PEP 0345 (Environment Markers) but the syntax appears to follow the draft PEP 0496.

    0 讨论(0)
  • 2020-12-02 16:04

    You could create an install.py script and call pip by script.

    import pip
    
    _all_ = [
        "SOAPpy>=0.12.22",
        "pycrypto>=2.6.1",
        "suds>=0.4",
        "Python-ldap>=2.4.19",
        "paramiko>=1.15.2",
        "nose>=1.3.4",
        "selenium>=2.44.0",
        "bottle>=0.12.8",
        "CherryPy>=3.6.0",
        "pika>=0.9.14",
    ]
    
    windows = ["wmi-client-wrapper>=0.0.12",]
    
    linux = ["WMI>=1.4.9",]
    
    darwin = []
    
    def install(packages):
        for package in packages:
            pip.main(['install', package])
    
    if __name__ == '__main__':
    
        from sys import platform
    
        install(_all_) 
        if platform == 'windows':
            install(windows)
        if platform.startswith('linux'):
            install(linux)
        if platform == 'darwin': # MacOS
            install(darwin)
    

    Another way to resolve this issue using only requirements files should be using inheritance of requirements

    requirements.txt

    SOAPpy>=0.12.22
    pycrypto>=2.6.1
    suds>=0.4
    Python-ldap>=2.4.19
    paramiko>=1.15.2
    nose>=1.3.4
    selenium>=2.44.0
    bottle>=0.12.8
    CherryPy>=3.6.0
    

    windows.txt

    -r requirements.txt
    WMI>=1.4.9
    

    linux.txt

    -r requirements.txt
    WMI>=1.4.9
    

    Then you can call just the requirement equivalent to platform.

    pip install -r windows.txt
    pip install -r linux.txt
    
    0 讨论(0)
提交回复
热议问题