将python包上传到PyPI
程序员文章站
2022-05-07 23:09:10
...
目标:
将自己写的python包上传到PyPI上,这样其他人就可以用pip下载了
总体文件结构:
--- Root directory (name doesn't matter)
|- your_library
| |- __init__.py
| |- actual_code_goes_here.py
|- setup.py
|- README.rst
|- LICENSE.txt
创建setup.py文件
例子:
from setuptools import setup, find_packages
setup(
name="advisorhelper",
packages=find_packages(),
version='0.9.3',
description="command line tool for auto tuner",
author="libbyandhelen",
author_email='[email protected]',
url="https://github.com/username/reponame",
download_url='https://github.com/username/reponame/archive/0.1.tar.gz',
keywords=['command', 'line', 'tool'],
classifiers=[],
entry_points={
'console_scripts': [
'command1 = advisorhelper.cmdline:execute'
'command2 = adviserserver.create_algorithm:run',
'command3 = adviserserver.run_algorithm:run'
]
},
install_requires=[
'grpcio>=1.7.0',
'numpy',
'requests',
]
)
注:
- Name: 包的名字
- find_packages(): 默认以setup.py所在路径为源路径(也可以用参数指定),递归遍历找到所有的python包(含有init.py的文件夹),支持include,exclude参数
- find_packages(exclude=[“.tests”, “.tests.“, “tests.“, “tests”])
- version:如修改后再次上传,需要使用不同版本号
- entry_points:指定包的入口
- install_requires: 指定此包的依赖
- url 和 download_url不是必须,可填写github地址
在 PyPI Live 上注册一个账户
https://pypi.python.org/pypi?%3Aaction=register_form
创建.pypirc文件,这样之后每次上传就不用登录了
[distutils]
index-servers =
pypi
pypitest
[pypi]
repository=https://pypi.python.org/pypi
username=your_username
password=your_password
[pypitest]
repository=https://testpypi.python.org/pypi
username=your_username
password=your_password
- /home/yourname/.pypirc on Mac and Linux
C:\Users\YourName.pypirc on Windows - 改变文件读写权限,因为其中含有明文的密码
chmod 600 ~/.pypirc on Mac and Linux
Windows中右键->属性->安全
检查一下setup.py的语法是否正确
python setup.py check
将包上传到PyPI Live
python3.5 setup.py sdist upload -r pypi
注意这里用的是python3.5,之前用python3.6会报[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed这个错误,python3.5就可以了,其他版本没有试过
参考资料
http://peterdowns.com/posts/first-time-with-pypi.html
http://setuptools.readthedocs.io/en/latest/setuptools.html#using-find-packages