身為軟體工程師,我們經常發現自己在不同的模組和專案中重複使用程式碼。但讓我們面對現實吧,這種重複帶來了一個挑戰:當我們需要調整或修復程式碼時,我們必須在多個地方進行相同的更改。對於我們這些重視效率和自動化的人來說,解決方案很明確 - 創建一個可以在我們的專案中安裝和使用的單獨包。
然而,在處理機密程式碼時,我們不能簡單地將套件發佈到像 PyPI 這樣的公共儲存庫上。相反,我們需要將其部署到私人儲存庫,例如 GitHub 或 GitLab。這種方法使我們能夠保持安全性,同時仍然受益於可重複使用套件的便利性。
在本教學中,我們將引導您完成以下過程:
透過執行這些步驟,您將能夠減少程式碼重複並簡化專案中共享程式碼的維護。
注意:DRY 不僅僅代表「Don’t Repeat Yourself」——它也是一種生活方式的選擇。
首先,讓我們為 Python 套件設定一個基本的專案結構:
my-package/ ├── my_package/ │ ├── __init__.py │ └── module1.py ├── setup.py ├── build.pipeline.yml ├── requirements.txt ├── .gitignore ├── README.md ├── MANIFEST.in └── LICENSE
讓我們來剖析一下我們的私有 Python 套件。每個檔案和目錄在使我們的套件正常運作和可安裝方面發揮著至關重要的作用:
讓我們在套件中建立一個簡單的模組。在 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
在 my_package/__init__.py 中,我們將導入我們的模組:
class Hello: def __init__(self, name): self.name = name def greet(self): return f"Hello, {self.name}!"
setup.py 檔案對於打包我們的專案至關重要。這是一個基本範例:
from .module1 import Hello
在我們的requirements.txt 檔案中,我們包含了建置和分發套件所需的依賴項:
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}", } )
安裝要求。為了簡單起見,我們將使用 Python 虛擬環境。
setuptools==69.2.0 wheel twine
建造我們的包:
python -m venv env source env/bin/activate # for linux and mac ./env/Scripts/activate # for windows pip install -r requirements.txt
要在本地安裝我們的軟體包以進行測試:
python setup.py sdist bdist_wheel
您可以使用 .gitignore 檔案提交您的工作並忽略資料夾:
https://github.com/github/gitignore/blob/main/Python.gitignore
要發佈包,請先在專案 my-package/ 的根目錄下建立一個 build.pipeline.yml 檔案並提交。部署將使用 twine 完成,這是我們之前安裝的函式庫:
my-package/ ├── my_package/ │ ├── __init__.py │ └── module1.py ├── setup.py ├── build.pipeline.yml ├── requirements.txt ├── .gitignore ├── README.md ├── MANIFEST.in └── LICENSE
如果您需要在模組安裝中包含非 Python 文件,可以使用 MANIFEST.in 檔案。此文件指定您的軟體包分發中應包含哪些附加文件。
class Hello: def __init__(self, name): self.name = name def greet(self): return f"Hello, {self.name}!"
然後上傳套件:
from .module1 import Hello
建立存取權杖:
獲得令牌後,請確保其安全,因為您將需要它來安裝軟體包。
在您的電腦上,您可以使用以下範本安裝您的私有套件:
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}", } )
幹得好,你現在知道如何在 GitHub 上使用 Python 建立和部署自己的私有套件了。
Github 儲存庫連結:https://github.com/ABDELLAH-Hallou/Private-Python-Package-Deployment
以上是在 GitHub 上建立並發布私有 Python 套件的詳細內容。更多資訊請關注PHP中文網其他相關文章!