Python packages distribution

Currently python options for code distribution  are bit messy as described here.   I spent some time to find right way how to create setup script, which will install external dependencies,   so here is the recipe for python3:

Create virtual virtual environment for your project:

virtualenv -p /usr/bin/python3 .

Then create file – two important things:

    from setuptools import setup
except ImportError:
    from distutils.core import setup

Import setup function from setuptools,   this l enables to specify dependencies, which should be installed  automatically by pip or easy_setup.

      description='Sample package',
      author='Your name',
      author_email='your email',
      requires= ['six (>=1.4.1)', 'bitarray (>=0.8.1)'],
      install_requires=['six>=1.4.1', 'bitarray>=0.8.1'],

It is important that install_requires keyword is  included. Also note different syntax of version requirements  – requires vs install_requires . (requires and provides are probably not necessary ).

Test files,  readme, license  files etc.   specify them in file in root of distribution – something like this:

recursive-include tests *.txt *.py
exclude tests/

Once all code is ready you can create distribution with command:

python sdist

This will create packed distribution in dist subdirectory  and then you can try to install in another virtual environment by:

pip install path/to/dist/packageX-1.0.0.tar.gz


Leave a Reply

Your email address will not be published. Required fields are marked *