python pypi_如何在PyPi上发布自己的Python包

python pypi

by Marco Massenzio

由Marco Massenzio

如何在PyPi上发布自己的Python包 (How to publish your own Python Package on PyPi)

Want to share your Python code with other developers? Want to make your users’ lives easier when installing your package? Then you should publish your Python packages to PyPi.

是否想与其他开发人员共享您的Python代码? 想要在安装软件包时使用户的生活更轻松吗? 然后,您应该将Python包发布到PyPi。

The good news is that it is now easier than ever. This is a short guide that will walk you through the process and point you toward relevant documentation along the way.

好消息是,它现在比以往任何时候都容易。 这是一个简短的指南,将引导您完成整个过程,并逐步向您介绍相关文档。

步骤1:建立setup.py档案 (Step 1: Create a setup.py file)

The arguments for setup() are documented here and are non-trivial. A good example to go by is my filecrypt project’s setup.py file.

setup()的参数在此处介绍,并且很简单。 一个很好的例子是我的filecrypt项目的setup.py文件。

Below is a brief excerpt. Again, be sure to read the documentation for more info on this, since it explains what all these arguments mean much better than I could, even with a full Medium article to do it:

以下是一个简短的摘录。 同样,请务必阅读文档以获取有关此内容的更多信息,因为即使其中有一篇完整的中型文章也可以解释所有这些论点的含义,但它们比我要好得多:

setup(name='crytto',      description='An OpenSSL-based file encryption                    and decryption utility',      long_description=long_description,      version='0.2.0',      url='https://github.com/massenz/filecrypt',      author='M. Massenzio',      author_email='marco@alertavert.com',      license='Apache2',      classifiers=[          'Development Status :: 4 - Beta',          'Intended Audience :: System Administrators',          'License :: OSI Approved :: Apache Software License',          'Programming Language :: Python :: 3'      ],      packages=['crytto'],      install_requires=[          'PyYAML>=3.11',          'sh>=1.11'      ],      entry_points={          'console_scripts': [              'encrypt=crytto.main:run'          ]      })

Note: Do not confuse setuptools with distutils — here’s the correct import for setup.py:

注意: 不要混淆的setuptools与distutils的-这里是为setup.py正确导入:

from setuptools import setup

The trickiest part is figuring out package names and the correct configuration for your script files. It’s probably best to decide these in advance, but you can always rectify these while crafting your setup.py.

最棘手的部分是找出程序包名称和脚本文件的正确配置 最好事先确定这些,但是在制作setup.py时,您总是可以纠正它们。

The biggest challenge is to come up with a top-level package name that does not conflict with an existing one. As far as I can tell, it’s currently mostly a process of trial-and-error.

最大的挑战是提出一个与现有软件包不冲突的顶级软件包名称。 据我所知,目前主要是反复试验的过程。

Once the setup.py is in decent shape, you can try and build a wheel:

一旦setup.py处于合适的形状,您可以尝试构建轮子:

python setup.py bdist_wheel

After doing that, it’s good practice to create a new virtualenv, and try to install the new package in that one:

之后,创建一个新的virtualenv并尝试在其中安装新软件包是一个好习惯:

virtualenv test_env./test_env/bin/activatepip install dist/my-project.whl

This is particularly useful for testing out whether the console_scripts have been correctly configured.

这对于测试console_scripts是否已正确配置特别有用。

If you use classifiers as in:

如果您按以下方式使用分类器

classifiers=[     'Development Status :: 4 - Beta',     'Intended Audience :: System Administrators',     'License :: OSI Approved :: Apache Software License',    'Programming Language :: Python :: 3']

…then make sure to consult the classifiers list, as anything else will cause an error and prevent registration.

…然后确保查阅分类列表 ,否则将导致错误并阻止注册。

注册您的项目 (Register your Project)

Note: The documentation told me to use twine for this step, but it didn’t work for me. Your mileage may vary.

注意:文档告诉我此步骤要使用麻线,但对我而言不起作用。 你的旅费可能会改变。

Unless you have already have an account on PyPi, you’ll need to create one, then log in.

除非您已经在PyPi上拥有一个帐户,否则您需要创建一个 ,然后登录。

You can then head over to the registration form and upload your PKG_INFO file. This has been created in a [prj name].egg-info/ directory. It may take a bit of back and forth, while you try to appease the Gods of PyPi to accept your configuration choices.

然后,您可以转到注册表,并上传您的PKG_INFO文件。 这是在[prj名称] .egg-info /目录中创建的。 在您尝试安抚PyPi众神以接受您的配置选择时,可能要花费一些时间来回。

In particular, coming up with a non-conflicting-yet-meaningful package name may take more attempts than you’d expect. Again, I recommend you plan, as I’ve been unable to find an easy way to list all package names. If you do know of one, be sure to leave a comment. You’ll note that, according to PyPi…

特别是,想出一个没有冲突但还没有意义的软件包名称可能会比您期望的花费更多的时间。 再次,我建议您进行计划,因为我一直无法找到列出所有软件包名称的简便方法。 如果您确实知道其中一项,请务必发表评论。 您会注意到,根据PyPi ...

There are currently 88906 packages here.

(“here” being PyPi, as of September 16, 2016).

(“此处”为PyPi,截至2016年9月16日)。

上载到PyPi (Upload to PyPi)

Once registration succeeds, the actual upload is rather easy, using twine:

注册成功后,使用麻线即可轻松进行实际上传:

twine upload dist/*

Provided you have a valid ~/.pypirc, it will just ask for your password. Then you just need to:

只要您有一个有效的〜/ .pypirc,它只会询问您的密码。 然后,您只需要:

$ cat ~/.pypirc [distutils] index-servers=pypi
[pypi] repository = https://upload.pypi.org/legacy/ username = [your username]

That's it. Enjoy building and sharing your Python packages!

而已。 乐于构建和共享您的Python包!

I originally published this on my blog at codetrips.com.

我最初将此内容发布在我的博客上codetrips.com上

翻译自: https://www.freecodecamp.org/news/how-to-publish-a-pyton-package-on-pypi-a89e9522ce24/

python pypi

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值