Versun

对待生命,不妨大胆一点,因为我们终将失去它

参考文档:

https://packaging.python.org/en/latest/tutorials/packaging-projects/

https://setuptools.pypa.io/en/latest/userguide/quickstart.html
https://docs.djangoproject.com/zh-hans/5.0/intro/reusable-apps/

准备工作

安装相关工具

python3 -m pip install --upgrade pip

python3 -m pip install --upgrade twine

python3 -m pip install --upgrade setuptools

python3 -m pip install --upgrade build

注册pypi账号

正式环境:https://pypi.org

测试环境:https://test.pypi.org

文件结构

my-project/
└── src/
    └── my_package/
        ├── __init__.py
        └── example.py

my_package是你的包名

创建打包的相关文件

按照下方的结构创建对应文件:

my-project/
├── LICENSE
├── pyproject.toml
├── README.md
├── setup.cfg
├── setup.py
├── MANIFEST.in
├── src/
│   └── my_package/
│       ├── __init__.py
│       └── example.py
└── tests/

各文件内容参考如下:

pyproject.toml

[build-system]
requires = ['setuptools>=69.1.0']
build-backend = 'setuptools.build_meta'

setup.cfg

[metadata]
name = django-text-translator
version = 2024.02.28
description = A Django application that supports adding multiple third-party engines for text translation.
long_description_content_type = text/markdown
long_description = file: README.md
url = https://github.com/versun/django-text-translator
author = versun
author_email = [email protected]
license = MIT
classifiers =
    Environment :: Web Environment
    Framework :: Django
    Framework :: Django :: 5.0
    Intended Audience :: Developers
    License :: OSI Approved :: MIT License
    Operating System :: OS Independent
    Programming Language :: Python
    Programming Language :: Python :: 3
    Programming Language :: Python :: 3 :: Only
    Programming Language :: Python :: 3.10
    Programming Language :: Python :: 3.11
    Programming Language :: Python :: 3.12
    Topic :: Internet :: WWW/HTTP
    Topic :: Internet :: WWW/HTTP :: Dynamic Content
    Topic :: Software Development :: Libraries :: Application Frameworks
    Topic :: Software Development :: Libraries :: Python Modules

[options]
include_package_data = true
packages = find:
python_requires = >=3.10
install_requires =
    django >= 5.0

License可参考https://choosealicense.com/

setup.py

from setuptools import setup
setup()

MANIFEST.in

include LICENSE
include README.md
recursive-include docs *
recursive-include django_text_translator/static *
recursive-include django_text_translator/templates *

recursive-include表示循环包含后面的文件夹和子文件夹

打包

使用python自带的打包工具setuptools,如果没有,可使用pip安装它

然后在my-project文件夹下运行:python -m buildpython setup.py sdist

以上命令均会在dist文件夹下生成一个tar.gz源代码文件包

上传

可以先上传到test pypi进行测试。

首先需要在 https://test.pypi.org/manage/account/#api-tokens 创建一个token,将 "范围 "设置为 "整个账户",并保存复制token,该token只显示一次。

运行命令python3 -m twine upload --repository testpypi dist/*,输入token上传就行了。

如果需要上传到正式环境,则运行python3 -m twine upload dist/*,并使用正式环境的token即可

测试安装包

python3 -m pip install --index-url [https://test.pypi.org/simple/](https://test.pypi.org/simple/) --no-deps my-package

也可以直接通过git安装

pip install git+https://github.com/versun/django-text-translator

完成

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注