Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

python

pip

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 
like image 533
Cas Avatar asked Mar 23 '15 23:03

Cas


People also ask

Can pip install requirements txt?

txt. If you are managing Python packages (libraries) with pip, you can use the configuration file requirements. txt to install the specified packages with the specified version.

Where is requirements txt Python stored?

Typically the requirements. txt file is located in the root directory of your project. Notice we have a line for each package, then a version number. This is important because as you start developing your python applications, you will develop the application with specific versions of the packages in mind.

How can I get requirements for txt Jupyter?

Simply open a terminal and navigate to the folder you want your requirements file to be in. You can then activate a virtual environment using venv or conda. It will take every package you have installed on that environment.


1 Answers

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.

like image 67
Tony G Avatar answered Sep 21 '22 08:09

Tony G