Search code examples
pythonpippypi

Uploading different versions (python 2.7 vs 3.5) to PyPI


I have a package I'm uploading to PyPI with two different versions of the code: one for Python 2.7 and one for Python 3.5.

What is the standard for uploading this to PyPI? Do I use two separate setup.py files?

When users run pip install mypackage will it automatically download the correct version?


Solution

  • TL;DR: add python_requires on setup.py. Use twine to upload the package to PyPI.

    Like IPython, its 6.0.0+ support Python 3.3+ only and the 5.x still support Python 2.x. If you install it using pip >= 9.0.1, pip install ipython will select latest 5.x for Python 2 and latest 6.x for Python 3.

    Modify setup.py

    First, you need set Requires-Python (PEP 440) meta-data in setup.py, by putting the python_requires argument on setup() function.

    For example, the following is the setup.py for the Python 2.7 version:

    setup(
        name='some-package',
        version='2.3.3',
        ...,
        python_requires='==2.7.*'
    )
    

    For Python 3.5+, just change it to python_requires='>=3.5'.

    Of course, two packages must have different version numbers. otherwise PyPI will reject one.

    You can use two separate setup.py files to do this, or just use one file and set python_requires argument dynamically.

    Upload to PyPI

    python setup.py sdist upload seems to not upload package meta-data (which contains Requires-Python) to PyPI.

    I found the easiest way to do it correctly is using twine.

    1. Create some distributions in the normal way:

      $ python setup.py sdist bdist_wheel
      
    2. Register your project (if necessary)

    3. Upload with twine

      $ twine upload dist/*
      

    Repeat step 1 & 3 for both Python 2 and 3 version.