python3 pip loading egg is deprecated 警告的解决方法

解决 egg is deprecation 警告

python3 -m pip install some_package 时发现 exploitable 有警告:

DEPRECATION: Loading egg at exploitable-1.32-py3.12.egg is deprecated. pip 24.3 will enforce this behaviour change. A possible replacement is to use pip for package installation.
Discussion can be found at https://github.com/pypa/pip/issues/12330

pip 24.3 将废弃 egg 形式的 python package, 根据 github issue [1] 的信息 pip 24.3 将在 October 2024 发布。可以使用下面的方法,要去掉上面的警告信息。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

cd exploitable_src_dir

# 删除老的 build 结果
rm -rf build dist htmlcov *.egg-info

# 升级 pip
python3 -m pip install --upgrade build

# 重新 build
python3 -m build --wheel

# 使用 wheel 的方式安装
python3 -m pip install . --user

一般情况下,不用重新写 pyproject.toml ,执行上面命令就可以了。

在 Wheel 中添加二进制文件的方法

根据python 官方的文档,MANIFEST.in 只对 source distributions (sdist) 起作用。

MANIFEST.in does not affect binary distributions such as wheels.

要想给 wheel 添加非纯 python 的文件,有一些困难,主要是一些使用 Extension 的 Python Package 会遇上这个问题。通过研究发现,可以控制 setup.py bdist_wheel 的参数达到打包so 文件的效果。

执行 python3 setup.py bdist_wheel --help ,可以看到详细的参数。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
Options for 'bdist_wheel' command:
--bdist-dir (-b) temporary directory for creating the distribution
--plat-name (-p) platform name to embed in generated filenames (default:
linux_x86_64)
--keep-temp (-k) keep the pseudo-installation tree around after creating
the distribution archive
--dist-dir (-d) directory to put final built distributions in
--skip-build skip rebuilding everything (for testing/debugging)
--relative build the archive using relative paths (default: false)
--owner (-u) Owner name used when creating a tar file [default: current
user]
--group (-g) Group name used when creating a tar file [default: current
group]
--universal make a universal wheel (default: false)
--compression zipfile compression (one of: stored, deflated) (default:
'deflated')
--python-tag Python implementation compatibility tag (default: 'py3')
--build-number Build number for this particular version. As specified in
PEP-0427, this must start with a digit. [default: None]
--py-limited-api Python tag (cp32|cp33|cpNN) for abi3 wheel tag (default:
false)

利用--bdist-dir 参数,修改生成wheel distributions 的目录可以达到目的。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
from setuptools import setup, Extension, find_packages

lib_name = Extension()
setup(name='package_name',
packages=find_packages(),
package_data={'': ['*.so']},
include_package_data=True,
...,
ext_modules=[lib_name],
options={
'bdist_wheel': {'python_tag': 'cp30', 'py_limited_api': 'cp32', 'bdist_dir': 'build'},
'build_ext': {'build_lib': 'build/package_name'},
}
)

修改 setup.py 后,重新执行 python3 -m build --wheel 即可。

参考信息

[1] Remove support for installed .egg distributions
[2] Packaging Python Projects
[3] Writing the Setup Script
[4] Packaging and distributing projects
[5] wheel Documentation


python3 pip loading egg is deprecated 警告的解决方法
http://usmacd.com/cn/egg-is-deprecated/
Author
henices
Posted on
January 17, 2024
Licensed under