要使用Python的setup()
函數發布包,請按照以下步驟操作:
pip install setuptools wheel
在項目根目錄下創建一個名為setup.py
的文件。這個文件將包含有關您的包的信息以及發布所需的配置。
編輯setup.py
文件,添加以下內容并根據您的包進行相應的修改:
from setuptools import setup, find_packages
setup(
name="your-package-name",
version="0.1",
packages=find_packages(),
install_requires=[
# 添加您的包所需的依賴項,例如:
"numpy",
"pandas",
],
entry_points={
# 如果您的包包含可執行文件或命令行接口,請在此處定義它們,例如:
"console_scripts": [
"your-command-name = your_package_name.main:main_function",
],
},
author="Your Name",
author_email="your.email@example.com",
description="A short description of your package",
long_description="A longer description of your package",
long_description_content_type="text/markdown",
url="https://github.com/yourusername/your-package-name",
classifiers=[
"Programming Language :: Python :: 3",
"License :: OSI Approved :: MIT License",
"Operating System :: OS Independent",
],
)
在項目根目錄下創建一個名為.gitignore
的文件,以排除不必要的文件,例如__pycache__
、.pyc
和*.pyo
等。
初始化Git倉庫并提交更改:
git init
git add .
git commit -m "Initial commit"
創建一個PyPI帳戶并登錄:https://pypi.org/account/register/
安裝twine庫,以便將包發布到PyPI:
pip install twine
git config --global user.name "Your Name"
git config --global user.email "your.email@example.com"
python setup.py sdist bdist_wheel
twine upload dist/*
現在,您的包已成功發布到PyPI,可以通過以下命令安裝和使用:
pip install your-package-name