Search code examples
pythonpython-3.xpython-wheelpython-packaging

Create python wheel based on platform


I am trying to package my python code into wheel file. My code dependecies differ based on the platform. For example in Windows, I need psycopg2 while in linux I need psycopg2-binary.

I have created two separate files: requirements.txt and requirements_linux.txt in my project. Below is the detail of my Setup.py

requirement_file='requirements.txt'
if sys.platform == 'linux':
    requirement_file = 'requirements_linux.txt'
here = os.path.abspath(os.path.dirname(__file__))
# setup method inside setup.py

setup(
...
...
install_requires=open(os.path.join(here,requirement_file)).readlines(),
...
)

Now I am creating wheel file using the command python setup.py bdist_wheel command from windows system.But the above code doesn't seem to work. When I run the wheel file in linux environment, it searches for psycopg2 instead of psycopg2-binary. Am I missing something?

How can I create wheel files for other platforms like linux or Mac and have separate dependencies in them?


Solution

  • Python projects distributed as wheel do not contain the setup.py file. Therefore it cannot be run at the time of the installation.

    The correct way to specify platform specific dependencies for setuptools is the following:

    setuptools.setup(
        # ...
        install_requires=[
            "LinuxOnlyDependency ; platform_system=='Linux'",
            "WindowsOnlyDependency ; platform_system=='Windows'"
        ],
        # ...
    )
    

    References: