返回介绍

4.5 和世界分享你的成果

发布于 2024-01-23 21:41:46 字数 4536 浏览 0 评论 0 收藏 0

一旦有了合适的setup.py文件,很容易生成一个用来分发的源代码tarball。只需要使用sdist命令即可,如示例4.3所示。

示例4.3 使用setup.py sdist

$ python setup.py sdist
running sdist
[pbr] Writing ChangeLog
[pbr] Generating AUTHORS
running egg_info
writing requirements to ceilometer.egg-info/requires.txt
writing ceilometer.egg-info/PKG-INFO
writing top-level names to ceilometer.egg-info/top_level.txt
writing dependency_links to ceilometer.egg-info/dependency_links.txt
writing entry points to ceilometer.egg-info/entry_points.txt
[pbr] Processing SOURCES.txt
[pbr] In git context, generating filelist from git
warning: no previously-included files matching '*.pyc' found anywhere in 
distribution
writing manifest file 'ceilometer.egg-info/SOURCES.txt'
running check
copying setup.cfg -> ceilometer-2014.1.a6.g772e7
Writing ceilometer-2014.1.a6.g772e7/setup.cfg

[...]

Creating tar archive
removing 'ceilometer-2014.1.a6.g772e7' (and everything under it)

这会在你的源代码树的dist目录下创建一个tarball,这可以用来安装你的软件。正如在4.3节中提到的,可以使用bdist_wheel命令构建Wheel归档文件。

最后一步是要让最终用户在通过pip命令安装你的包时能够知道包在哪里。这意味你需要将你的项目发布到PyPI(http://pypi.python.org)。

如果是第一次,你很可能会犯错,最好能在一个安全的沙箱中测试发布流程而不是在生产服务器中。可以使用PyPI预付费服务器(PyPI staging server,https://testpypi.python.org/pypi)实现,它复制了主索引的全部功能,但是只用于测试目的。

第一步就是在测试服务器上注册你的项目。打开你的~/.pypirc文件并加入下列行:

[distutils]
index-servers =
  testpypi

[testpypi]
username = <your username>
password = <your password>
repository = https://testpypi.python.org/pypi

现在就可以在索引中注册你的项目了:

$ python setup.py register -r testpypi
running register
running egg_info
writing requirements to ceilometer.egg-info/requires.txt
writing ceilometer.egg-info/PKG-INFO
writing top-level names to ceilometer.egg-info/top_level.txt
writing dependency_links to ceilometer.egg-info/dependency_links.txt
writing entry points to ceilometer.egg-info/entry_points.txt
[pbr] Reusing existing SOURCES.txt
running check
Registering ceilometer to https://testpypi.python.org/pypi
Server response (200): OK

最后,可以上传一个源代码分发tarball:

% python setup.py sdist upload -r testpypi
running sdist
[pbr] Writing ChangeLog
[pbr] Generating AUTHORS
running egg_info
writing requirements to ceilometer.egg-info/requires.txt
writing ceilometer.egg-info/PKG-INFO
writing top-level names to ceilometer.egg-info/top_level.txt
writing dependency_links to ceilometer.egg-info/dependency_links.txt
writing entry points to ceilometer.egg-info/entry_points.txt
[pbr] Processing SOURCES.txt
[pbr] In git context, generating filelist from git
warning: no previously-included files matching '*.pyc' found anywhere in 
distribution
writing manifest file 'ceilometer.egg-info/SOURCES.txt'
running check
creating ceilometer-2014.1.a6.g772e7

[...]

copying setup.cfg -> ceilometer-2014.1.a6.g772e7
Writing ceilometer-2014.1.a6.g772e7/setup.cfg
Creating tar archive
removing 'ceilometer-2014.1.a6.g772e7' (and everything under it)
running upload
Submitting dist/ceilometer-2014.1.a6.g772e7.tar.gz to https://testpypi. 
python.org/pypi
Server response (200): OK

以及一个Wheel归档文件:

$ python setup.py bdist_wheel upload -r testpypi
running bdist_wheel
running build
running build_py
running egg_info
writing requirements to ceilometer.egg-info/requires.txt
writing ceilometer.egg-info/PKG-INFO
writing top-level names to ceilometer.egg-info/top_level.txt
writing dependency_links to ceilometer.egg-info/dependency_links.txt
writing entry points to ceilometer.egg-info/entry_points.txt
[pbr] Reusing existing SOURCES.txt
installing to build/bdist.linux-x86_64/wheel
running install
running install_lib
creating build/bdist.linux-x86_64/wheel

[...]

creating build/bdist.linux-x86_64/wheel/ceilometer-2014.1.a6.g772e7.
  dist-info/WHEEL
running upload
Submitting /home/jd/Source/ceilometer/dist/ceilometer-2014.1.a6.g772e7- 
  py27-none-any.whl to https://testpypi.python.org/pypi
Server response (200): OK

现在应该可以在PyPi预付费服务器中搜索你的包,确认是否上传成功了。也可以试着用pip安装它,可以通过-i选项指定测试服务器:

$ pip install -i https://testpypi.python.org/pypi ceilometer

如果一切就绪,就可以继续下一步了:上传项目到PyPI主服务器。只需要将身份信息和服务器的具体信息添加到你的~/.pypirc文件中:

[distutils]
index-servers =
  pypi
  testpypi

[pypi]
username = <your username>
password = <your password>

[testpypi]
repository = https://testpypi.python.org/pypi
username = <your username>
password = <your password>

分别运行register和upload并配合参数-r pypi就能正确地将你的包上传到PyPI服务器了。

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文