How to Publish Your Code as a Pip Module

简介: Last week, I have made a python cli tool. To make it more convenient to use, I want to publish it as a pip module, so I have made some research and mistakes, and finally succeeded.

Last week, I have made a python cli tool. To make it more convenient to use, I want to publish it as a pip module, so I have made some research and mistakes, and finally succeeded.

Prerequisites

  1. Register a pypi account in the official website
  2. Apply a token in the pypi management page
  3. Make sure your module name will be unique, you can check it in the pypi search page

Check your code

  1. Make sure your code is in a "package" folder (Must have __init__.py file)
  2. Create a README.md
  3. Create a LICENSE
  4. Create the pyproject.toml file in the root folder of the package, you can use the following content as a template:
[build-system]
requires = ["hatchling"]
build-backend = "hatchling.build"

[project]
name = "biliupload" # make sure your module name is unique
version = "0.0.1"
authors = [
  { name="timerring"},
]
description = "Login and upload videos to bilibili"
readme = "README.md"
license = { file="LICENSE" }
requires-python = ">=3.10"
classifiers = [
    "Programming Language :: Python :: 3",
    "License :: OSI Approved :: MIT License",
    "Operating System :: OS Independent",
]
dependencies = [
  "PyYAML==6.0.2",
  "qrcode==8.0",
  "Requests==2.32.3",
  "requests_html==0.10.0",
]

[project.urls]
"Homepage" = "https://github.com/timerring/biliupload"

The below is the basic template, if you are making a cli tool, you also need to add the project.scripts section.

For more details, you can refer to the official documentation

Build the package

  1. Latest version of PyPA’s build is recommended

    python3 -m pip install --upgrade build
    
  2. Build the package(run this command from the same directory where pyproject.toml is located)

    python3 -m build
    

You will see the following output in dist/ folder:

  • .whl file: the built distribution
  • .tar.gz file: the source distribution

Upload distribution archive for TestPyPI

The TestPyPI is a isolated website of PyPI, you can upload your package to it first to check if it works.

  1. Register on TestPyPI
  2. Apply a token in the TestPyPI management page
  3. install the twine package python3 -m pip install --upgrade twine
  4. upload the distribution packages via python3 -m twine upload --repository testpypi dist/* and you can check your package.
  5. Test your package in a new environment python3 -m pip install --index-url https://test.pypi.org/simple/ biliupload

That maybe have some problems, you can refer to the Conflict problem section to fix it.

Upload distribution archive formally

  1. Make sure your production version for build is ready
  2. Upload the distribution archive via python3 -m twine upload dist/*
  3. Then you can pip it formally pip install biliupload

Your can also refer to the official documentation if you want more details.

Additionally, you may also want to make it into a executable file, you can just use the pyinstaller package command pyinstaller -F biliupload/cli.py to do it.

Note: pyinstaller is not a cross-platform compiler, you need to compile it on the platform you want to run it on via docker or some other tools.
And if you want to Using Data Files from a Module, don't forget to include it via pyinstaller --add-data "bilitool/authenticate/config.json:bilitool/authenticate" -F bilitool/cli.py.

Conflict problem

You may encounter the following error:

The conflict is caused by:
    biliupload 0.1.1 depends on pyyaml==6.0.2
    biliupload 0.1.0 depends on pyyaml==6.0.2

To fix this you could try to:
1. loosen the range of package versions you've specified
2. remove package versions to allow pip to attempt to solve the dependency conflict

That is because you are using the pyyaml package which is not exist in the testpypi, so you can refer to this stackoverflow to fix it via pip install -i https://test.pypi.org/simple/ --extra-index-url https://pypi.org/simple biliupload==0.1.0, which the --extra-index-url pointer to the pypi.org.

This article is also posted on my own blog, feel free to check the latest revision: How to Publish Your Code as a Pip Module.

目录
相关文章
|
Python
python2 pip2 No module named web
python2 pip2 No module named web
64 0
|
JavaScript 前端开发 Python
PIP常用命令-No module named ‘lxml‘
PIP常用命令-No module named ‘lxml‘
181 0
使用pip时报错:No module named ‘chardet‘ 的解决办法
使用pip时报错:No module named ‘chardet‘ 的解决办法
1943 0
使用pip时报错:No module named ‘chardet‘ 的解决办法
|
Ubuntu Python
pip3 执行错误 No module named 'distutils.cmd'
pip3 执行错误 No module named 'distutils.cmd'
1807 0
|
5月前
|
缓存 Python
Command "python setup.py egg_info" failed with error code 1 in /tmp/pip-build-npf9报错
Command "python setup.py egg_info" failed with error code 1 in /tmp/pip-build-npf9报错
|
7月前
|
Linux 网络安全 开发者
【Python】已解决:WARNING: pip is configured with locations that require TLS/SSL, however the ssl module i
【Python】已解决:WARNING: pip is configured with locations that require TLS/SSL, however the ssl module i
2213 3
|
Python
python2 pip2 pip install web.py No module named wsgiserver
python2 pip2 pip install web.py No module named wsgiserver
70 0
pip安装skbuild报错:ModuleNotFoundError: No module named ‘skbuild’解决方法
pip安装skbuild报错:ModuleNotFoundError: No module named ‘skbuild’解决方法,换源
796 0
|
TensorFlow 算法框架/工具
pip安装后仍有ImportError No module named XX问题解决
pip安装后仍有ImportError No module named XX问题解决
398 2

热门文章

最新文章