gpt4 book ai didi

python - 使用 pip install -e 在 setup.py 中安装 data_files

转载 作者:行者123 更新时间:2023-12-03 17:14:09 24 4
gpt4 key购买 nike

我正在尝试为我的 CLI 工具提供一个用 Python 编写的 bash 完成脚本。根据Python Packaging Authority , data_files在 setup.py 中正是我需要的:

Although configuring package_data is sufficient for most needs, in some cases you may need to place data files outside of your packages. The data_files directive allows you to do that. It is mostly useful if you need to install files which are used by other programs, which may be unaware of Python packages.



所以我添加了这样的完成文件:
data_files=[
('/usr/share/bash-completion/completions', ['completion/dotenv']),
],

并尝试使用以下方法对其进行测试:
pip install -e .
在我的虚拟环境中。但是,未安装完成脚本。是我忘记了什么还是 pip splinter 的?完整的项目可以在 here 中找到

最佳答案

我遇到了同样的问题,我已经实现了一个解决方法。

在我看来 python setup.py develop或 ( pip install -e . ) 不运行与 python setup.py install 相同的函数.
事实上,我通过查看源代码注意到python setup.py install运行 build_py :

https://github.com/python/cpython/blob/master/Lib/distutils/command/build_py.py#L134
https://github.com/pypa/setuptools/blob/master/setuptools/command/build_py.py

经过几次挖掘,我选择覆盖 develop命令如下。以下代码为python3.6:

""" SetupTool Entry Point """
import sys
from pathlib import Path
from shutil import copy2

from setuptools import find_packages, setup
from setuptools.command.develop import develop

# create os_data_files that will be used by the default install command
os_data_files = [
(
f"{sys.prefix}/config", # providing absolute path, sys.prefix will be different in venv
[
"src/my_package/config/properties.env",
],
),
]


def build_package_data():
""" implement the necessary function for develop """
for dest_dir, filenames in os_data_files:
for filename in filenames:
print(
"CUSTOM SETUP.PY (build_package_data): copy %s to %s"
% (filename, dest_dir)
)
copy2(filename, dest_dir)


def make_dirstruct():
""" Set the the logging path """
for subdir in ["config"]:
print("CUSTOM SETUP.PY (make_dirstruct): creating %s" % subdir)
(Path(BASE_DIR) / subdir).mkdir(parents=True, exist_ok=True)


class CustomDevelopCommand(develop):
""" Customized setuptools install command """

def run(self):
develop.run(self)
make_dirstruct()
build_package_data()

# provide the relevant information for stackoverflow
setup(
package_dir={"": "src"},
packages=find_packages("src"),
data_files=os_data_files,
cmdclass={"develop": CustomDevelopCommand},
)

关于python - 使用 pip install -e 在 setup.py 中安装 data_files,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55208309/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com