This project explores how to build a Python package using industry-standard project organization for distributable code and how to distribute code to PyPI.
Follows: https://blog.ionelmc.ro/2014/05/25/python-packaging/#the-structure
- Ensure you are inside a virtual environment.
- Install the package. The easiest method is to follow package documentation on TestPyPi or PyPi, which will provide the exact
pip
command. For ease of use, the commands are repeated in this document. Note that TestPyPi is periodically purged, so the distribution may no longer be available there.$ pip install pythontemplate-rnelson5 # from PyPi
$ pip install -i https://test.pypi.org/simple/ pythontemplate-rnelson5==0.1.0 # from TestPyPi
$ pip install -e . # from local files (if downloaded)
- Ensure you are inside the same virtual environment in which you installed the package.
- Use an entry point from the distribution's
setup.py
file, if there is one, or execute the script from the command line in the usual way.$ run-pythontemplate # entry point
$ python pythontemplatepackage/pythontemplatemodule.py # usual way
- Ensure you are inside the same virtual environment in which you installed the package.
- Start your interpreter, then import the package or functions from the package to use.
$ python >>> from pythontemplatepackage.pythontemplatemodule import get_id >>> get_id('John') 123
- Ensure you are inside the same virtual environment in which you installed the package.
- Import the package or functions from the package to use.
```python ```python from src.pythontemplatepackage.pythontemplatemodule import get_id ``` ```
- Ensure you are not inside a virtual environment.
- Assemble a distribution using
wheel
$ python3 setup.py sdist bdist_wheel
- Upload the distribution using
twine
$ python3 -m twine upload --repository testpypi dist/* Uploading distributions to https://test.pypi.org/legacy/ Enter your username: __token__ Enter your password: <TestPyPi API key> : View at: https://test.pypi.org/project/pythontemplate-rnelson5/0.1.0/
The project is designed to be as teachable as possible:
- Components in the project are uniquely named to help users clearly identify package components which might otherwise be confusing due to naming standards (i.e. git repository, Python package, Python module, and virtual environment are typically given the same name).
- The output of the
main()
method of thepythontemplatemodule
module allows users to explore the concept of virtual environments. Users can maintain different versions of python and therequests
library in their regular environment versus a virtual environment, and see how output changes depending on where code is executed. - The
__init__.py
files print out statements whenever they are imported. This helps users understand when packages and modules are imported.