为一个 python 项目构建一个轮子/egg 和所有依赖项

为了在我们公司内部实施 python 项目,我需要制作一个可安装的发行版。

这应包括:

  • 给我的项目一个鸡蛋或者一个人
  • 对于项目的每个依赖项,使用 egg 或 whl
  • (可选)生成一个列出此版本的所有可安装组件的 Requments.txt 文件

是否有一个简单的插件(例如 bdist _ Wheal 的替代品)不仅可以编译一个轮子,而且还可以编译项目的组件?

显然,我可以编写这个脚本,但是我希望有一个捷径,可以用更少的步骤构建包 + 依赖项。

这需要在 Windows + Linux 上的 Python 2.7上工作。

130473 次浏览

You will need to create a setup.py file for your package. Make sure you have the latest setuptools and pip installed. Then run the following:

python setup.py bdist_wheel

This will create a wheel file for your package. This assumes you don't have C/C++ headers, DLLs, etc. If you do, then you'll probably have a lot more work to do.

To get dependencies, you will want to create a requirements.txt file and run the following:

pip wheel -r requirements.txt

If your package isn't on PyPI, then you'll have to manually copy your package's wheel file into the wheel folder that this command creates. For more information see the following excellent article:

With the latest pip and wheel, you can simply run

pip wheel .

within your project folder, even if your application isn't on PyPi. All wheels will be stored in the current directory (.).

To change the output directory (to for example, ./wheels), you may use the -w / --wheel-dir option:

pip wheel . -w wheels

All the options available are listed at the pip documentation.

With poetry you can define your dependencies and metadata about your project in a file in the root of your project, called pyproject.toml:

[tool.poetry]
name = "my-project"
version = "0.1.0"
description = "some longer description"
authors = ["Some Author <some@author.io>"]


[tool.poetry.dependencies]
python = "*"


[tool.poetry.dev-dependencies]
pytest = "^3.4"

To build your project as a wheel, execute poetry build

$ poetry build


Building my-project (0.1.0)
- Building sdist
- Built my-project-0.1.0.tar.gz


- Building wheel
- Built my-project-0.1.0-py3-none-any.whl

a dist/ folder is created with a wheel for your project.