As software engineers, we often find ourselves reusing code across different modules and projects. But let's face it, this repetition creates a challenge: when we need to adjust or fix that code, we have to make the same changes in multiple places. For those of us who value efficiency and automation, the solution is clear - create a separate package that can be installed and used across our projects.
However, when dealing with confidential code, we can't simply publish our package on public repositories like PyPI. Instead, we need to deploy it to a private repository such as GitHub or GitLab. This approach allows us to maintain security while still benefiting from the convenience of a reusable package.
In this tutorial, we’ll guide you through the process of:
By following these steps, you'll be able to reduce code duplication and simplify maintenance of shared code across your projects.
Note: DRY doesn't just stand for "Don't Repeat Yourself" - it's also a lifestyle choice.
First, let's set up a basic project structure for our Python package:
my-package/ ├── my_package/ │ ├── __init__.py │ └── module1.py ├── setup.py ├── build.pipeline.yml ├── requirements.txt ├── .gitignore ├── README.md ├── MANIFEST.in └── LICENSE
Let's break down the anatomy of our private Python package. Each file and directory plays a crucial role in making our package functional and installable:
Let's create a simple module within our package. In my_package/module1.py:
my-package/ ├── my_package/ │ ├── __init__.py │ └── module1.py ├── setup.py ├── build.pipeline.yml ├── requirements.txt ├── .gitignore ├── README.md ├── MANIFEST.in └── LICENSE
In my_package/__init__.py, we'll import our module:
class Hello: def __init__(self, name): self.name = name def greet(self): return f"Hello, {self.name}!"
The setup.py file is crucial for packaging our project. Here's a basic example:
from .module1 import Hello
In our requirements.txt file, we include the necessary dependencies for building and distributing our package:
from setuptools import setup, find_packages with open('requirements.txt') as f: requirements = f.read().splitlines() setup( name="my_package", version="0.1", include_package_data=True, python_requires='>=3.8', packages=find_packages(), setup_requires=['setuptools-git-versioning'], install_requires=requirements, author="Abdellah HALLOU", author_email="abdeallahhallou33@gmail.com", description="A short description of your package", long_description=open('README.md').read(), long_description_content_type="text/markdown", classifiers=[ "Programming Language :: Python :: 3.8", "License :: OSI Approved :: MIT License", "Operating System :: OS Independent", ], version_config={ "dirty_template": "{tag}", } )
Install the requirements. To keep things simple, we will use the Python virtual environment.
setuptools==69.2.0 wheel twine
To build our package:
python -m venv env source env/bin/activate # for linux and mac ./env/Scripts/activate # for windows pip install -r requirements.txt
To install our package locally for testing:
python setup.py sdist bdist_wheel
You can commit your work and ignore the folders using .gitignore file:
https://github.com/github/gitignore/blob/main/Python.gitignore
To publish the package, first, create a build.pipeline.yml file at the root of the project my-package/ and commit it. The deployment will be done with twine, the library that we installed before:
my-package/ ├── my_package/ │ ├── __init__.py │ └── module1.py ├── setup.py ├── build.pipeline.yml ├── requirements.txt ├── .gitignore ├── README.md ├── MANIFEST.in └── LICENSE
If you need to include non-Python files with your module installation, you can use a MANIFEST.in file. This file specifies which additional files should be included in your package distribution.
class Hello: def __init__(self, name): self.name = name def greet(self): return f"Hello, {self.name}!"
Then upload the package:
from .module1 import Hello
Create an access token:
Once you have your token, keep it secure, as you'll need it to install your package.
On your machine, you can install your private package using the following template:
from setuptools import setup, find_packages with open('requirements.txt') as f: requirements = f.read().splitlines() setup( name="my_package", version="0.1", include_package_data=True, python_requires='>=3.8', packages=find_packages(), setup_requires=['setuptools-git-versioning'], install_requires=requirements, author="Abdellah HALLOU", author_email="abdeallahhallou33@gmail.com", description="A short description of your package", long_description=open('README.md').read(), long_description_content_type="text/markdown", classifiers=[ "Programming Language :: Python :: 3.8", "License :: OSI Approved :: MIT License", "Operating System :: OS Independent", ], version_config={ "dirty_template": "{tag}", } )
Well done, you know now how to create and deploy your own private packages with Python on GitHub.
Github repository link : https://github.com/ABDELLAH-Hallou/Private-Python-Package-Deployment
The above is the detailed content of Create and Release a Private Python Package on GitHub. For more information, please follow other related articles on the PHP Chinese website!