An example of a package developed with pyenv, built with build using setuptools, uploaded to PyPI using twine, and distributed via pip.
Project description
Python Package Example
This package was created by generally following the Packaging Python Projects with the addition of some pipenv setup to manage virtual environments.
How this package was created
- Install pipenv, build, and twine if not already installed.
- create directory structure for the package like that below, where
examplepackagefb1258
is replaced with your package's name. This name must be uniquely yours when uploaded to PyPI. Better to avoid hyphens or underline characters (-
or_
) in the package name, as these can create problems when importing. The parent directory name (the repository name) -python-package-example
in this case - is not relevant to the package name.
python-package-example/
|____README.md
|____LICENSE
|____pyproject.toml
|____tests
|____src
|____examplepackagefb1258
|______init__.py
|______main__.py
|____wisdom.py
- Make
__init__.py
an empty file. - Enter the text of a copyright license of your choosing into
LICENSE
. - Add settings in
pyproject.toml
suitable for asetuptools
-based build and add metadata fields to this file - see the example in this repository. - Put your own custom module code into
src
/examplepackagefb1258
/wisdom.py
or whatever filename(s) you choose for the module(s) that live within your package. - Optionally add a
__main__.py
file to the package directory, if you want to be able to run the package as a script from the command line, e.g.python -m examplepackagefb1258
. - Build the project by running
python -m build
from the same directory where thepyproject.toml
file is located. - Verify that the built
.tar
archive has the files you expect your package to have (including any important non-code files) by running the command:tar --list -f dist/examplepackagefb1258-0.0.7.tar.gz
, whereexamplepackagefb1258-0.0.7
is replaced with your own package name and version. - Create an account on TestPyPI where one can upload to a test repository instead of the production PyPI repo.
- Create a new API token on TestPyPI with the "Scope" set to “Entire account”. Save a copy of the token somewhere safe.
- Upload your package to the TestPyPI repository using twine, e.g.
twine upload -r testpypi dist/*
- twine will output the URL of your package on the PyPI website - load that URL in your web browser to see your packaged published - make sure the
README.md
file looks nice on the web site.
Every time you change the code in your package, you will need to rebuild and reupload it to PyPI. You will need to build from a clean slate and update the version number to achieve this:
- delete the autogenerated
dist
directory - delete the autogenerated
src/*.egg-info
directory - update the version number in
pyproject.toml
and anywhere else it is mentioned (do a find/replace) - build the package again with
python -m build
- upload the package again with
twine upload -r testpypi dist/*
Repeat as many times as necessary until the package works as expected. Once complete, upload to the real PyPI instead of the TestPyPI repository.
If updating version numbers is tedious, you may consider using bumpver - a tool that can automate some parts of updating version numbers.
How to install and use this package
Try installing and using your package in a separate Python project:
- Create a
pipenv
-managed virtual environment and install the latest version of your package installed:pipenv install -i https://test.pypi.org/simple/ examplepackagefb1258==0.0.7
. (Note that if you've previously created apipenv
virtual environment in the same directory, you may have to delete the old one first. Find out where it is located with thepipenv --venv
command.) - Activate the virtual environment:
pipenv shell
. - Create a Python program file that imports your package and uses it, e.g.
from examplepackagefb1258 import wisdom
and thenprint(wisdom.get())
(replacewisdom
andget()
with any module name and function that exists in your package) . - Run the program:
python3 my_program_filename.py
. - Exit the virtual environment:
exit
.
Try running the package directly:
- Create and activate up the
pipenv
virtual environment as before. - Run the package directly from the command line:
python3 -m examplepackagefb1258
. This should run the code in the__main__.py
file. - Exit the virtual environment.
How to run unit tests
Simple example unit tests are included within the tests
directory. To run these tests...
- Install pytest in a virtual environment.
- Run the tests from the main project directory:
python3 -m pytest
. - Tests should never fail. Any failed tests indicate that the production code is behaving differently from the behavior the tests expect.
Pro tip
While working on the package code, and verifying it behaves as expected, it can be helpful to install the package in "editable" mode so that changes to the package are immediately updated in the virtual environment.
- To do this, run
pipenv install -e .
from the main project directory.
Continuous integration
This project has a continuous integration workflow that builds and runs unit tests automatically with every push of the code to GitHub.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file biglm-0.0.1.tar.gz
.
File metadata
- Download URL: biglm-0.0.1.tar.gz
- Upload date:
- Size: 44.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.8.8
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7a7137934fc44430ac070990ce229b36f72a83f3d82f0569873f8abdff0f5f66 |
|
MD5 | a8426a5b98aa3d4c3a77e974d4550f5c |
|
BLAKE2b-256 | 0d1e94bf3f35602dc2700a9b89ba5adae1588e1610bbe671b33d51e7ef7dd4f0 |
File details
Details for the file biglm-0.0.1-py3-none-any.whl
.
File metadata
- Download URL: biglm-0.0.1-py3-none-any.whl
- Upload date:
- Size: 29.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.8.8
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | f8c30f0699143617b4f8401887e2c66a0d2746d3d0d0e66364e551e0933347ff |
|
MD5 | e73625c8d4ae3a3ad5b947c3e8bbb252 |
|
BLAKE2b-256 | 7df43035714d71eb390e6ed849d7f60f0ebc7e1b861765aa1ca4be17f174b40b |