Packaging your Project to have it installable from a Package Index like PyPI ¶
Initial setup
Create a Python package, for example:
follow these instructions:
https://docs.djangoproject.com/en/2.1/intro/reusable-apps/
then add versioning:
file setup.cfg:
[bumpversion] current_version = 0.0.1 commit = True tag = True [bumpversion:file:MYAPP/__init__.py] [wheel] universal = 1
where:
file MYAPP/__init__.py:
__version__ = '0.0.1'
and in setup.py add:
import os import re def get_version(*file_paths): """Retrieves the version from django_task/__init__.py""" filename = os.path.join(os.path.dirname(__file__), *file_paths) version_file = open(filename).read() version_match = re.search(r"^__version__ = ['\"]([^'\"]*)['\"]", version_file, re.M) if version_match: return version_match.group(1) raise RuntimeError('Unable to find version string.') version = get_version("MYAPP", "__init__.py") readme = open('README.rst').read() history = open('HISTORY.rst').read().replace('.. :changelog:', '') setup( name='MYAPP', version=version, description='...', long_description=readme + '\n\n' + history, ...
where:
file HISTORY.rst:
.. :changelog: History ======= v0.0.1 ------ * Initial setup
Remember to add a .gitignore file:
# Compiled python modules. *~ *.py[cod] __pycache__ # Setuptools distribution folder. /dist/ /build/ # Python egg metadata, regenerated from source files by setuptools. /*.egg-info
or:
pip install cookiecutter cookiecutter https://github.com/pydanny/cookiecutter-djangopackage.git
then:
(1) Select master branch and Update changelog
git checkout master git merge develop vim CHANGELOG.rst
(2) create a Distribution (aka “Package” ) for your project
Universal Wheels are wheels that are pure python (i.e. contains no compiled extensions) and support Python 2 and 3. This is a wheel that can be installed anywhere by pip.
To build the wheel:
python setup.py bdist_wheel --universal
(3) Make sure that README.rst is valid
twine check dist/*
or
python setup.py check -r -s
(4) Bump package version
bumpversion PART --verbose
where PART = major, minor or patch
(5) Re-create package and upload your distribution
Once you have an account you can upload your distributions to PyPI using twine
rm -fr dist/ && rm -fr build/ python setup.py bdist_wheel --universal twine upload dist/*
(6) cleanup
rm -fr dist/ && rm -fr build/
See also:
Debugging a Package locally
Strategy:
- add package to project's requirements as usual
- clone package sources in an external local folder
- add this statement to local settings:
import sys sys.path.insert(0, <PATH_TO_PACKAGE_SOURCES_PARENT_FOLDER>)
or:
LOCAL_MODULES = [ '/Users/morlandi/src2/github_public/django-query-tracer', '/Users/morlandi/src2/github_public/django-task', ] import sys for local_module in LOCAL_MODULES: sys.path.insert(0, local_module) sys.stderr.write('\x1b[1;36;40m Using local module: "%s" \x1b[0m' % local_module)
Alternative:
- install package as follows:
pip install -e <PATH_TO_PACKAGE_SOURCES_PARENT_FOLDER>