How to publish your software package to a package repository?
Description
Publishing a software package to a package repository allows other developers to easily install and use it in their projects. Different programming languages have their own package repositories, such as npm for JavaScript, PyPI for Python, and Maven Central for Java. Additionally, there are general-purpose registries like GitHub Packages and GitLab Package Registry, which allow storing and distributing packages across multiple ecosystems.
This document specifically covers publishing Python packages to Python Package Index (PyPi) (Python Package Index) and GitLab Package Registry, explaining the differences between these two so developers can decide where to publish their package.
Differences Between PyPI and GitHub Packages
- PyPI:
- The official package repository for Python.
- Packages published here are accessible via pip install.
- Ideal for public distribution to the Python community.
- GitLab Package Registry:
- A registry integrated into GitLab, supporting multiple package formats including Python.
- Can be used for private package distribution within GitLab projects.
- Supports CI/CD workflows within GitLab pipelines.
Considerations
-
Provide relevant information in package configuration files (e.g., package.json, pyproject.toml).
-
Ensure all dependencies are correctly listed and compatible.
-
You can use Semantic Versioning to indicate changes clearly.
-
Include a valid open-source or commercial license and relevant documentation for users.
-
Some repositories require authentication tokens or API keys to publish packages.
-
To publish a package in a public repository, you need an account on the respective platform (e.g., npm, PyPI).
-
PyPI allows public package installations without authentication.
-
GitLab Package Registry may require authentication to install packages, depending on repository settings.
Solutions
-
Publishing a Python package to PyPI
-
Create a PyPI account at pypi.org and verify your email.
-
Create the package directory:
mkdir my_python_package && cd my_python_package
-
Add a Python script (
my_module.py
) that serves as the main module of your package. -
Create a
setup.py
file:from setuptools import setup setup( name='my_python_package', version='0.1.0', py_modules=['my_module'], install_requires=[], )
-
Build the package:
python setup.py sdist
-
Create an API token at your PyPI account settings.
-
Create a configuration file (
.pypirc
) to store your API token securely (replaceYOUR_PYPI_API_TOKEN
with the token you generated in the previous step):[pypi] username = __token__ password = YOUR_PYPI_API_TOKEN
-
Install
twine
(allows secure upload to PyPI) and upload the package:pip install twine twine upload --verbose --config-file .pypirc dist/*
-
Your package is now available on PyPI and can be installed using
pip install my_python_package
. -
Using the PyPI Test Server (TestPyPI): If you want to test your package upload process without publishing it to the official PyPI, you can use TestPyPI. First, create an account there and generate an API token. Store the token in a
.pypirc
file (replaceYOUR_PYPI_API_TOKEN
with the token you generated in the previous step):[testpypi] username = __token__ password = YOUR_PYPI_API_TOKEN
Then upload your package with:
twine upload --repository testpypi --verbose --config-file ./.pypirc dist/*
You can install the test package using:
pip install --index-url https://test.pypi.org/simple/ my_python_package
-
-
Publishing a Python package to GitLab Package Registry
-
Ensure you have a GitLab account and a project where the package will be hosted.
-
Generate a Personal Access Token in GitLab with
api
scopes (under User settings > Access Tokens). This token is used for authentication when uploading packages. -
Find your Project ID in GitLab. You can locate this in your project settings under General > Project ID. This ID is required to correctly reference the package repository URL.
- Create or update a
.pypirc
file in your home directory to store GitLab authentication:[distutils] index-servers = gitlab [gitlab] repository = https://gitlab.com/api/v4/projects/YOUR_PROJECT_ID/packages/pypi/ username = __token__ password = YOUR_GITLAB_ACCESS_TOKEN
- Build the package:
python setup.py sdist
- Install
twine
and upload the package to GitLab Package Registry:pip install twine twine upload --repository --verbose gitlab dist/*
- Your package is now available in GitLab and can be installed with:
pip install --index-url https://gitlab.com/api/v4/projects/YOUR_PROJECT_ID/packages/pypi/simple my_python_package
-
References
Tools and resources on this page
Tool or resource | Description | Related pages | Registry |
---|---|---|---|
GitLab | DevOps platform that enables teams to collaborate, plan, develop, test, and deploy software using an integrated toolset for version control, CI/CD, and project management. | Creating a 'Read the D... Releasing code Software documentation Version control Digital Software Ident... | |
Python Package Index (PyPi) | Official third-party software repository for Python packages | Reproducible virtual s... | |
Semantic Versioning | Semantic versioning (SemVer) is a widely-adopted version scheme that encodes a version of a project by a three-part version number (Major. Minor. Patch), an optional pre-release tag, and an optional build meta tag. | Releasing code |
How to cite this page
Eva Martín del Pico, "Publishing a package in a package repository". everse.software. http://everse.software/RSQKit/publish_package .