So you made a package. Now you will want to share it. What next?
Developer
Goal - make a distribution (also called a "package") to share
Preamble
You are now packaging your package and wish to distribute it. There are two main kinds of packages:
- an application: deploy a source to a server, github, website e.g. CLI
- a library: publish a source distribution (sdist) or binary (e.g. wheel) usually to PyPI via
twine
Traditional Ways
Several files may be included in a distribution, but here are the main ones:
- source: your code
setup.py
: specify metadata, and dependencies required to make an sdist.
requirements.txt
: a list of dependencies
Contemporary Ways
Use pyproject.toml
to specify which tool to use in creating your sdist or binary:
Modern tools to create + deploy/publish a package include:
pipenv
: makes a package and substitutes requirements.txt
(recommended by PyPA)
- develop:
> pipenv install <dependency>
, > pipenv install
- publish:
> pipenv -e .
+ twine
poetry
: makes a package and publishes to PyPI
- develop
> poetry add <dependency>
, > poetry install
- publish:
> poetry publish
flit
: makes a package and publishes to PyPI
- develop:
> flit install
- publish:
> flit publish
The first two options have features to make clean virtual environments and safely install dependencies using lock files. I would encourage exploring these newer options later on as they clear up a lot of packaging headaches by obviating setup.py
and setuptools
.
User
Goal - get a package and install it with dependencies
Traditional Ways
Applications have a variety of deployment methods, e.g. uploading your app to a hosting service e.g. heroku, DigitalOcean, etc. The user may indirectly interface with your app through a website, CLI or more.
Libraries are often uploaded to PyPI. From here the user can usually install a package using pip
independent from how they are made:
> pip install <package>
(recommended)
> pip install <packatge> -r requirements
(explicit, optional)
These commandline invocations will fetch the distribution from PyPI, install the package and specified dependencies.
Contemporary Ways
Here are some alternatives to pip
:
pipx
: "safely" install packages in isolated environments
See Also
- Official docs on publishing packages by PyPA
- Tutorial on How To Package Your Python Code
- Podcast interview with B. Cannon on
pyproject.toml
and modern packaging tools