setup.py示例?

2024-03-29 08:00:57 发布

您现在位置:Python中文网/ 问答频道 /正文

学习本页后:

http://docs.python.org/distutils/builtdist.html

我希望找到一些setup.py文件来学习,以便自己制作(目标是制作fedora rpm文件)。

s.o.社区能给我举几个好例子吗?


Tags: 文件pyorghttpdocs目标htmlsetup
3条回答

您可能会发现HitchHiker's Guide to Packaging很有用,即使它不完整。我将从Quick Start tutorial开始。试着浏览一下Python Package Index上的Python包。只需下载tarball,解包,然后查看setup.py文件。或者更好的方法是,只需查看列出公共源代码存储库(如托管在GitHub或BitBucket上的存储库)的包。你一定会在头版碰到一个。

我的最后一个建议是去做一个,不要害怕失败。我真的不明白,直到我开始自己做。在PyPI上创建一个新的包很简单,删除它也很容易。所以,创建一个虚拟包并四处播放。

先读这个https://packaging.python.org/en/latest/current.html

Installation Tool Recommendations

  1. Use pip to install Python packages from PyPI.
  2. Use virtualenv, or pyvenv to isolate application specific dependencies from a shared Python installation.
  3. Use pip wheel to create a cache of wheel distributions, for the purpose of > speeding up subsequent installations.
  4. If you’re looking for management of fully integrated cross-platform software stacks, consider buildout (primarily focused on the web development community) or Hashdist, or conda (both primarily focused on the scientific community).

Packaging Tool Recommendations

  1. Use setuptools to define projects and create Source Distributions.
  2. Use the bdist_wheel setuptools extension available from the wheel project to create wheels. This is especially beneficial, if your project contains binary extensions.
  3. Use twine for uploading distributions to PyPI.

这个anwser已经过时了,而且确实有一个针对python打包世界的救援计划,叫做

轮距

我想pythonwheels.com这里:

What are wheels?

Wheels are the new standard of python distribution and are intended to replace eggs. Support is offered in pip >= 1.4 and setuptools >= 0.8.

车轮的优点

  1. 纯python和本机C扩展包的安装速度更快。
  2. 避免安装时执行任意代码。(避免setup.py)
  3. 在Windows上安装C扩展不需要编译器 或者OS X
  4. 允许更好的缓存以进行测试和连续 整合。
  5. 在安装过程中创建.pyc文件以确保 它们与使用的python解释器匹配。
  6. 跨平台和计算机的安装更加一致。

关于正确的python打包(以及关于wheels)的全部内容,请参见packaging.python.org


康达路

对于科学计算(这在packaging.python.org上也是推荐的,见上文),我会考虑使用CONDA packaging,这可以看作是在PyPI和pip工具之上构建的第三方服务。它还可以很好地设置您自己的binstar版本,因此我可以想象它可以完成复杂的定制企业包管理的技巧。

Conda可以安装到用户文件夹中(没有超级用户权限),工作方式类似于magic

conda install

以及强大的虚拟环境扩展。


鸡蛋道

此选项与python-distribute.org相关,并且已经过时(以及站点),因此让我向您介绍一个我喜欢的现成且紧凑的setup.py示例:

  • 将脚本和单个python文件混合到setup.py中的一个非常实用的示例/实现给出了here
  • 更棒的是hyperopt

这段引述摘自setup.py的状态指南,仍然适用:

  • setup.py不见了!
  • distutils不见了!
  • 分发出去!
  • 皮普和维图阿列夫留在这里!
  • 鸡蛋。。。跑了!

我(向我)再加一点

  • 车轮

我建议在尝试无意识的复制粘贴之前,先了解一下packaging-ecosystem(来自gotgenes所指的指南)。

互联网上的大多数例子都是从

from distutils.core import setup

但是这个示例不支持构建eggpython setup.py bdist_egg(以及其他一些旧的功能),这些功能在

from setuptools import setup

原因是它们已被弃用。

现在根据指南

Warning

Please use the Distribute package rather than the Setuptools package because there are problems in this package that can and will not be fixed.

不推荐使用的setuptools将被distutils2替换,后者“将成为Python 3.3中标准库的一部分”。我必须说我喜欢安装工具和鸡蛋,但还没有完全相信distutils2的便利性。它需要

pip install Distutils2

以及安装

python -m distutils2.run install

聚苯乙烯

包装从来都不是小事(一个人通过尝试开发一个新的来学习这个),所以我认为很多事情都是有原因的。我只希望这次它能正确地完成。

完成编写setup.py脚本here的演练。(举几个例子)

如果您想要一个真实的例子,我可以为您指出几个主要项目的setup.py脚本。Django's是here,pyglet's是here。您只需浏览名为setup.py的文件的其他项目的源代码即可获得更多示例。

这些并不是简单的例子;我提供的教程链接有这些。这些都比较复杂,但也比较实用。

相关问题 更多 >