如何打包python应用程序使其可点子安装?

问题:如何打包python应用程序使其可点子安装?

我在业余时间正在编写django应用程序,以参加我们正在上班的小费竞赛。我认为我会明智地使用这段时间,并逐步了解virtualenv,pip,打包,django 1.3以及如何编写易于重新分发的应用程序。到目前为止,一切都很好。

我要负责包装部分。例如,GitHub上的许多django应用程序大多(大致)以相同的方式捆绑在一起。我将以django-uni-forms为例。

我所做的一个假设是,MANIFEST.insetup.py是pip完成工作所需的唯一必需的部分。那是对的吗?如果我的假设是错误的,还需要哪些其他组件?

是否通常生成了所需的包装文件,或者是手工制作的?可以描述依赖项然后再安装吗?我的应用程序依赖django-uni-forms,并且在requirements.txt我用来安装依赖项的应用程序中的文件中列出了它;但这是包装系统可以解决的问题吗?

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

I’m writing a django application in my spare time for a footy-tipping competition we’re running at work. I figured I’d use this time wisely, and get up to speed on virtualenv, pip, packaging, django 1.3, and how to write an easily redistributable application. So far, so good.

I’m up to the packaging part. A lot of the django apps on GitHub for instance are mostly bundled (roughly) the same way. I’ll use django-uni-forms as an example.

An assumption I’m making is that the MANIFEST.in and setup.py are the only required pieces that pip needs to do its job. Is that correct? What other components are necessary if my assumption is wrong?

Are the required packaging files generally generated, or are they crafted by hand? Can dependencies be described and then installed also? My application depends on django-uni-forms, and I have it listed in a requirements.txt file within my app which I used to install the dependency; but is that something that the packaging system can take care of?

What are the steps I need to follow to package my application in such a way that pip will be able to install it and any dependencies?


回答 0

是的,MANIFEST.in而且setup.py应该是足够的。

这篇博客文章确实具有有关此主题的一些很好的信息: 打包Django可重用应用程序

这是另一个很好的详细概述,对我有很大帮助: Python打包用户指南

尤其是包含静态文件(模板)的提示很重要,因为起初可能并不明显。

是的,您可以在setup.py其中指定所需的软件包,这些软件包在安装应用程序时会自动获取。

例如:

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

显然,现在我们在两个地方定义了依赖项,但这并不一定意味着这些信息是重复的:setup.py与requirements.txt

通过此设置,您的软件包应可通过安装pip


正如Pierre在评论中指出的那样,Django的官方文档中现在还有一个相关的部分: 打包您的应用

然后是这个“完全不完整”的指南,它确实很好地概述了打包并将程序包上载到PyPI:分享您的爱的劳动:PyPI的快速与肮脏

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