Creating your first Python package can be intimidating, but it doesn’t have to be! Below are the basic instructions, with up-to-date links to examples for each step, and links to best practices. Steps 5 and 6 show options to make your package pip installable, whether your package is public or private.
- Add a setup.py script in the project root.
- Official pypa example setup.py: https://github.com/pypa/sampleproject/blob/master/setup.py
- More thorough explanations: https://packaging.python.org/tutorials/distributing-packages/
- Note: To include tests, add the following:
- In setup.pyPython12345678from setuptools import setupsetup(#...,setup_requires=['pytest-runner', ...],tests_require=['pytest', ...],#...,)In setup.cfg (to set up alias)Python12[aliases]test=pytestFrom the terminalShell1python setup.py test
- Optional: Add setup.cfg file.
- Official pypa example: https://github.com/pypa/sampleproject/blob/master/setup.cfg
- Optional: Package using
python setup.py sdist
- This will create a dist folder containing all of your distributions
- Push ( git push ) to remote (public or private) git repository
- Install from remote repository
- Full Options: https://pip.pypa.io/en/stable/reference/pip_install/#git
- Private repo example: pip install git+ssh://firstname.lastname@example.org:port/path/repo.git@branch_name
- Public repo example: pip install git+ https://github.com/github_user/repo.git@branch_name
- Optional: Upload to PyPi
- Register your package: python setup.py register
- Upload your package: python setup.py sdist upload
- What to do if you get a message like:
error: Upload failed (403): Invalid or non-existent authentication information. : Create a
~/.pypirc file with the following contents:Python12345678[distutils]index-servers =pypi[pypi]repository: https://pypi.python.org/pypiusername: <username>password: <pass>