How to submit a package to PyPI

How to submit a package to PyPI



The other month a coworker of mine wanted to distribute a small wrapper he‘d written for the Locu API. Instead of forcing developers to clone his repository, he wanted them to be able install with a single command: pip install locu. He wasn‘t sure how to go about this so I wrote up a quick guide, which I‘m publishing below because I haven‘t found any other guides for this particular use case (python library hosted on github).

What is PyPI?

From the official website:

PyPI — the Python Package Index

The Python Package Index is a repository of software for the Python programming language.

Written something cool? Want others to be able to install it with easy_install or pip? Put your code on PyPI. It‘s a big list of python packages that you absolutely must submit your package to for it to be easily one-line installable.

The good news is that submitting to PyPI is simple in theory: just sign up and upload your code, all for free. The bad news is that in practice it‘s a little bit more complicated than that. The other good news is that I‘ve written this guide, and that if you‘re stuck, you can always refer to the official documentation.

I‘ve written this guide with the following assumptions:

  • The module/library/package that you‘re submitting is called mypackage.
  • mypackage is hosted on github.

Create your accounts

On PyPI Live and also on PyPI Test. You must create an account in order to be able to upload your code. I recommend using the same email/password for both accounts, just to make your life easier when it comes time to push.

Create a .pypirc configuration file

This file holds your information for authenticating with PyPI, both the live and the test versions.

[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

This is just to make your life easier, so that when it comes time to upload you don‘t have to type/remember your username and password. Make sure to put this file in your home folder – its path should be ~/.pypirc.

Notes on passwords / usernames

Michiel Sikma has reported that in Python 3 if your password includes a raw %, it needs to be escaped by doubling – the .pypirc config parser interpolates strings. For example, if your password is hello%world:

[pypi]
repository=https://pypi.python.org/pypi
username=myusername
password=hello%%world

I‘ve never run into this issue, but if you‘re having trouble this might help.

Andrew Farrell points out that if your password includes spaces, make sure not to quote it. For example, if your password is correct horse battery staple:

[pypi]
repository=https://pypi.python.org/pypi
username=myusername
password=correct horse battery staple

Thanks to Michiel, Andrew, and Charlie Hack for their help with this section.

Prepare your package

Every package on PyPI needs to have a file called setup.py at the root of the directory. If your‘e using a markdown-formatted read me file you‘ll also need a setup.cfg file. Also, you‘ll want a LICENSE.txt file describing what can be done with your code. So if I‘ve been working on a library called mypackage, my directory structure would look like this:

root-dir/   # arbitrary working directory name
  setup.py
  setup.cfg
  LICENSE.txt
  README.md
  mypackage/
    __init__.py
    foo.py
    bar.py
    baz.py

Here‘s a breakdown of what goes in which file:

setup.py

This is metadata about your library.

from distutils.core import setup
setup(
  name = ‘mypackage‘,
  packages = [‘mypackage‘], # this must be the same as the name above
  version = ‘0.1‘,
  description = ‘A random test lib‘,
  author = ‘Peter Downs‘,
  author_email = ‘[email protected]‘,
  url = ‘https://github.com/peterldowns/mypackage‘, # use the URL to the github repo
  download_url = ‘https://github.com/peterldowns/mypackage/tarball/0.1‘, # I‘ll explain this in a second
  keywords = [‘testing‘, ‘logging‘, ‘example‘], # arbitrary keywords
  classifiers = [],
)

The download_url is a link to a hosted file with your repository‘s code. Github will host this for you, but only if you create a git tag. In your repository, type: git tag 0.1 -m "Adds a tag so that we can put this on PyPI.". Then, type git tag to show a list of tags — you should see 0.1 in the list. Type git push --tags origin master to update your code on Github with the latest tag information. Github creates tarballs for download athttps://github.com/{username}/{module_name}/tarball/{tag}.

setup.cfg

This tells PyPI where your README file is.

[metadata]
description-file = README.md

This is necessary if you‘re using a markdown readme file. At upload time, you may still get some errors about the lack of a readme — don‘t worry about it. If you don‘t have to use a markdown README file, I would recommend usingreStructuredText (REST) instead.

LICENSE.txt

This file will contain whichver license you want your code to have. I tend to use the MIT license.

Upload your package to PyPI Test

Run:

python setup.py register -r pypitest

This will attempt to register your package against PyPI‘s test server, just to make sure you‘ve set up everything correctly.

Then, run:

python setup.py sdist upload -r pypitest

You should get no errors, and should also now be able to see your library in the test PyPI repository.

Upload to PyPI Live

Once you‘ve successfully uploaded to PyPI Test, perform the same steps but point to the live PyPI server instead. To register, run:

python setup.py register -r pypi

Then, run:

python setup.py sdist upload -r pypi

and you‘re done! Congratulations on successfully publishing your first package!

时间: 2024-12-24 23:37:22

How to submit a package to PyPI的相关文章

openstack devstack Bad md5 hash for package

不管用unbunt 14还是ubuntu 15,或者Centos 7.1,运行stack.sh脚本生成devstack时,每次都会在类似下面的步骤中报错返回,只不过不同版本,每次的package不一样2015-12-10 22:38:18.381 | Collecting requests===2.8.1 (from -c /opt/stack/requirements/upper-constraints.txt (line 292))2015-12-10 22:38:18.769 |   Us

Bad md5 hash for package

[root@master conn]# pip install chardet==2.2.1 Collecting chardet==2.2.1 /usr/lib/python2.6/site-packages/pip/_vendor/requests/packages/urllib3/util/ssl_.py:79: InsecurePlatformWarning: A true SSLContext object is not available. This prevents urllib3

python urllib模块

1.urllib.urlopen(url[,data[,proxies]]) 打开一个url的方法,返回一个文件对象,然后可以进行类似文件对象的操作.本例试着打开google 1 #coding:UTF8 2 3 import urllib 4 response = urllib.urlopen("http://www.baidu.com") 5 f = response.readline() #读取html第一行 6 print f 代码 返回结果 <!DOCTYPE html

Python著名的lib和开发框架(均为转载)

第一,https://github.com/vinta/awesome-python Awesome Python A curated list of awesome Python frameworks, libraries, software and resources. Inspired by awesome-php. Awesome Python Admin Panels Algorithms and Design Patterns Anti-spam Asset Management A

Learn Python the Hard Way--Exercise 46

0. 缘起 <Learn Python the Hard Way>Exercise 46 要求安装四个python package pip, distribute, nose, virtualenv,(原书作者特别提醒: Do not just donwload these packages and install them by hand. Instead see how other people recommend you install these packages and use th

Machine and Deep Learning with Python

Machine and Deep Learning with Python Education Tutorials and courses Supervised learning superstitions cheat sheet Introduction to Deep Learning with Python How to implement a neural network How to build and run your first deep learning network Neur

python的项目结构

项目结构 知识点 创建项目,编写 __init__ 文件 使用 setuptools 模块,编写 setup.py 和 MANIFEST.in 文件 创建源文件的发布版本 项目注册&上传到 PyPI 实验步骤 本实验阐述了一个完整的 Python 项目结构,你可以使用什么样的目录布局以及怎样发布软件到网络上. 我们的实验项目名为 factorial. $ mkdir factorial $ cd factorial/ 1. 主代码 我们给将要创建的 Python 模块取名为 myfact,因此我

Awesome Python

Awesome Python  A curated list of awesome Python frameworks, libraries, software and resources. Inspired by awesome-php. Awesome Python Environment Management Package Management Package Repositories Distribution Build Tools Interactive Interpreter Fi

django-celery动态添加定时任务

为了使用celery替代crontab并做到实时添加定时任务的效果,需要使用django-celery,效果如下图, 来自:https://www.caktusgroup.com/blog/2014/06/23/scheduling-tasks-celery/ 要使用django-celery,需要安装python的以下包:django,celery,django-celery.其中django安装比较麻烦,首先它和python版本相关,django1.7.9和1.8.3都是支持python 2