26

Is it possible to have OS specific requirements in pip's requirements.txt file?

For example: I have a dependency on readline, therefore, if installing on windows (or OSX), then pyreadline is a requirement. If it's linux, then I don't want to force an install.

3
  • 1
    Nope. I don't think so. This should be dealt with some deployment. Also, if this were a project you want to distribute to people to use, never distribute requiremnts.txt. Commented Apr 15, 2013 at 9:07
  • 1
    @CppLearner never distribute requirements.txt ? isn't it its purpose ? Commented Apr 15, 2013 at 9:32
  • 1
    Well that's what I said "if this were a project". If you are writing a project, use setup.py. Commented Apr 15, 2013 at 16:53

2 Answers 2

35

You can do this with "Environment Markers" as specified in PEP-508:

Here's an example of using such a marker inside a requirements.txt:

pyreadline==2.1; platform_system == "Windows"

Similarly, in a setup.py:

setup(
    ...
    install_requires=['pyreadline; platform_system == "Windows"'],
)
Sign up to request clarification or add additional context in comments.

3 Comments

With Python 3.7, I had to use a semi-colon in the setup.py file install_requires, like this: install_requires=["pywin32 > 1.0 ; sys.platform == 'win32'"],
PEP 496 is Status: Rejected. I have updated your post with the appropriate replacement, which is PEP 508. This also accounts for the confusion @electromechanick mentioned.
FYI Mac system_platform is "Darwin"
3

In the end, adding the OS check in the setup.py is what I've found other people using. Ex:

install_requires = [
        "parsedatetime >= 1.1.2",
        "colorama >= 0.2.5",
        "pycrypto >= 2.6"
        ] + ["pyreadline >= 2.0"] if "win" in sys.platform else [],

link to full setup.py with example code

2 Comments

Don't use this. This used to be fine when posted (back in the days when the setup.py was executed during installation), but the modern way this works is that a wheel is build that contains everything to install the package.
wow, this is old....I'll just go ahead and make the other answer the accepted one......

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.