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 = 'peterldowns@gmail.com',
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!

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

  1. 如何上传package到pypi

    首先访问 pypi 创建一个帐号,并且需要验证一个邮箱,注意网易163邮箱收不到验证的邮件. 安装上传工具 pip install --user twine 执行上传命令 python setup.p ...

  2. How to using PyPI publish a Python package

    How to using PyPI publish a Python package PyPI & Python package https://pypi.org/ main make a f ...

  3. 使用pypi-server搭建简单的PyPI源

    pypiserver 是一个最基本的PyPI服务器实现, 可以用来上传和维护python包. 本文介绍 pypiserver 在ubuntu上的基本安装, 配置和使用. 1. 基本安装和使用 1.1 ...

  4. Creating a NuGet Package in 7 easy steps - Plus using NuGet to integrate ASP.NET MVC 3 into existing Web Forms applications

    UPDATE: Check out my follow up post where I remove the need for editing the Global.asax.cs and show ...

  5. 如何将Python项目发布到PyPI

    The Python Package Index (PyPI) is a repository of software for the Python programming language. 如何打 ...

  6. python urllib模块

    1.urllib.urlopen(url[,data[,proxies]]) urllib.urlopen(url[, data[, proxies]]) :创建一个表示远程url的类文件对象,然后像 ...

  7. python中对 函数 闭包 的理解

    最近学到 函数 闭包的时候,似懂非懂.迷迷糊糊的样子,很是头疼,今天就特意查了下关于闭包的知识,现将我自己的理解分享如下! 一.python 闭包定义 首先,关于闭包,百度百科是这样解释的: 闭包是指 ...

  8. 系统学习爬虫_2_urllib

    什么是urllib urlopen urllib.request.urlopen(url, data=None, [timeout, ]*, cafile=None, capath=None, cad ...

  9. “全能”选手—Django 1.10文档中文版Part4

    第一部分传送门 第二部分传送门 第三部分传送门 3.2 模型和数据库Models and databases 3.2.2 查询操作making queries 3.3.8 会话sessions 2.1 ...

随机推荐

  1. c++——this指针

    实验1:若类成员函数的形参 和 类的属性,名字相同,通过this指针来解决. 实验2:类的成员函数可通过const修饰,请问const修饰的是谁 #include <iostream> u ...

  2. java 输出流 outputstream

    一:输入和输出概念 输入流(inputstream):对于java程序来说,从程序写入文件叫做输出. 输出流(outputstream):对于java程序来说,从文件读取数据,到java程序叫做输入. ...

  3. N个数中第k个最大者

    #include<stdio.h> #include<stdlib.h> void bubbleSort(int arr[],int n) { int i,j; ;i>; ...

  4. javascript 深入理解 继承(转)

    通过对继承的深入理解,更了解javascript.本人还不太写会博客,转自汤姆大叔,链接http://www.cnblogs.com/TomXu/archive/2012/01/05/2305453. ...

  5. mysql8.0版本skip-grant-tables出现的新问题

    MySQL 初始化 mysqld --initialize 的时候会有密码,就这个样子, 可是毕竟总有人跟我一样,不熟悉安装过程,没有注意这一密码这一项,导致你现在不知道密码的尴尬处境,或者说你是正常 ...

  6. plupload分片上传视频文件源码展示

    plupload分片上传视频文件目录结构如下: |- images//视频上传小图片 |-js// plupload js文件 |-uploads//视频文件存放文件夹 里面是按日期存放 |-ajax ...

  7. 关于DE2-115 FPGA开发板无法烧写程序的解决方法

    友晶科技推出的DE2-115 FPGA开发板,有时候莫名奇妙,无法烧写或者固化程序.利用JTAG 向DE2-115开发板烧写.sof文件失败,并提示以下错误,如图1和图2所示 图1 图2 解决方法:只 ...

  8. innodb_flush_log_at_trx_commit

    innodb_flush_log_at_trx_commit   innodb_buffer_pool_size如 果用Innodb,那么这是一个重要变量.相对于MyISAM来说,Innodb对于bu ...

  9. Openstack入门篇(十二)之neutron服务(计算节点)的部署与测试

    1.Neutron组件安装 [root@linux-node2 ~]# yum install -y openstack-neutron-linuxbridge ebtables ipset 2.配置 ...

  10. idea中xml打开方式变成file,改回来

    原文:https://blog.csdn.net/u012903926/article/details/80682885 创建了一个test文件,用的是普通text打开方式,然后你修改文件为test. ...