如何打包一个 python 应用程序使其可安装 pip?

我在业余时间写一个 Django 应用程序参加我们公司举办的足球小费比赛。我认为我应该明智地利用这段时间,并加快处理 viralenv、 pip、打包、 django 1.3以及如何编写一个容易重新发布的应用程序。目前为止,一切顺利。

我来负责包装。例如,GitHub 上的很多 django 应用程序基本上都是以相同的方式打包的。我将使用 Django-uni- 表格作为示例。

我的一个假设是,MANIFEST.insetup.py是 pip 完成其工作所需要的唯一部分。是这样吗?如果我的假设是错误的,还需要什么其他组件?

所需的包装文件是通常生成的,还是手工制作的?是否可以描述依赖项并安装它们?我的应用程序依赖于 django-uni-forms,我把它列在我的应用程序的 requirements.txt文件中,我用它来安装依赖项; 但是这是打包系统可以处理的事情吗?

我需要遵循哪些步骤来打包我的应用程序,使 pip 能够安装它和任何依赖项?

38895 次浏览

Yes, MANIFEST.in and setup.py should be sufficient.

This blog post really has some good information on this topic: Packaging a Django reusable app

And here's another good, detailed overview that helped me a lot: Python Packaging User Guide

Especially the tips to get your static files (templates) included are important as this might not be obvious at first.

And yes, you can specify required packages in your setup.py which are automatically fetched when installing your app.

For example:

    install_requires = [
'django-profiles',
'django-uni-forms',
],

Obviously now we have two places where dependencies are defined, but that doesn't necessarily mean that these information are duplicated: setup.py vs requirements.txt

With this setup your package should be installable via pip.


As Pierre noted in the comments, there's now also a relevant section in Django's official documentation: Packaging your app

And then there is this "completely incomplete" guide, which really gives a great overview over packaging and uploading a package to PyPI: Sharing Your Labor of Love: PyPI Quick And Dirty