Publish Python package to pypi

Official documentation

Packaging Python projects

start

Create project

Take the code from the previous article as an example

The directory structure is as follows

src 存放代码的目录
	scrapy-redis-bf 需要上传的包
tests/ 存放一些测试文件,可以为空
LICENSE 开源许可证
pyproject.toml 项目设置
README.md 

The main concern is that pyproject.tomlif you want a license, https://choosealicense.com/you can just get one.

build-system

First, you need to choose a build tool. There are four official ones listed: hatchling, setuptools, Flit and PDM.

hatchling

[build-system]
requires = ["hatchling"]
build-backend = "hatchling.build"

setuptools

[build-system]
requires = ["setuptools>=61.0"]
build-backend = "setuptools.build_meta"

Whichever one you choose, write the configuration into pyproject.toml, any one is fine

pyproject.toml

Fill in some package information

[build-system]
requires = ["hatchling"]
build-backend = "hatchling.build"

[project]
name = "scrapy-redis-bf"
version = "0.0.8"
authors = [
  { name="kanade", email="[email protected]" },
]
description = "scrapy_redis use bloomfilter"
readme = "README.md"
license = { file = "LICENSE" }
requires-python = ">=3.7"
classifiers = [
    "Programming Language :: Python :: 3",
    "License :: OSI Approved :: MIT License",
    "Operating System :: OS Independent",
]
dependencies = [
    "scrapy-redis >= 0.6.8",
]

[project.urls]
"Homepage" = "https://github.com/kanadeblisst/scrapy_redis_bf"
"Bug Tracker" = "https://github.com/kanadeblisst/scrapy_redis_bf/issues"

You should know the others at a glance. dependenciesThey are dependencies that need to be installed for the package you uploaded.

Then make the changes and README.mdstart building.

Build whl

Install first:pip install build

Then pyproject.tomlrun it in the directory where it is located python -m buildand two files will be generated in the dist directory. The whl file can be installed directly with pip.

Create a pypi account

There are two websites: https://test.pypi.organd https://pypi.org, the former is probably used for testing

Taking the former as an example, open the website, register an account->verify email->create API tokens

https://pypi.org/manage/account/Scroll all the way Add API tokento fill in a name, then select scope and click Add token. You will get a string starting with pypi-, which is token.

Then create a file in the user directory .pypirc, for example, Windows is C:\Users\Administratorcreated under:, Administrator is the login user name. The content is as follows, just modify the token to the end of the password. Note: The accounts and tokens of the two websites pypi and testpypi are different.

[distutils]
index-servers =
    pypi
    testpypi

[pypi]
repository = https://upload.pypi.org/legacy/
username = __token__
password = pypi-xxxxx

[testpypi]
repository = https://test.pypi.org/legacy/
username = __token__
password = pypi-xxxxx
Upload to pypi

Installation package:pip install twine

Upload: twine upload --repository testpypi dist/*The testpypi here is the .pypircone in the above file, you can also write pypi

Network errors may occur when uploading. You need to set up a foreign proxy first and execute the command line:set HTTPS_PROXY=127.0.0.1:10809

Installation package

testpypi: pip install --index-url https://test.pypi.org/simple/ scrapy-redis-bf
pypi:pip install --index-url https://pypi.org/simple/ scrapy-redis-bf

If it is from other sources, it may take some time to synchronize.

Guess you like

Origin blog.csdn.net/Qwertyuiop2016/article/details/127110359