Search code examples
pythonpip

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


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 Windows is not compatible with Linux. Fortunately there is another package that provides the same functionality on Linux. All other dependencies are compatible in both platforms.

I know I could have 2 separate requirement files to address both platform dependencies separately. Something like win_requirements.txt and linux_requirements.txt, however this approach doesn't feel like the best way to do it.

I wonder if there is a way I can have only one requirements.txt file so any user can use pip install -r requirements.txt to install all the dependencies regardless of what platform they are?

Maybe something like??:

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
if platform.system() == 'Linux':
    wmi-client-wrapper>=0.0.12
else if platform.system() == 'Windows':
    WMI>=1.4.9

Solution

  • 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