python setup.py install 不会保留 data_files

发布于 2025-01-18 17:44:37 字数 1961 浏览 2 评论 0原文

我目前对安装我自己的Python包感到非常困惑...在setup.py、sdist和wheel之间,无论怎样我所做的,我似乎无法实现我想要的 - 即安装一堆非 .py 数据文件并将其保存在虚拟环境中,其结构与我的结构相同安装后的项目文件夹。

我阅读了各种文档,并创建了一个 setup.py 文件,其中包含一个 data_files 字段,其中包含我的安装中所需的所有数据文件。

我有以下结构:

.
|__ requirements.txt
    setup.py
    hr_datapool
    |__ __init__.py
    |__ data_needed
        |__ needed_folder
           |__ needed_file.notpy
    |__ one_module
        |__ __init__.py
            |__ misc_tools.py
            |__tests
                |__ test_tools.py
    |__ other_module
            ...

并且 data_needed 包含 misc_tools.py 所需的非 .py 数据文件(因此 tests .py) 来运行。

因此,我在 setup.py 中添加了一个 data_files ,其中包含我需要的所有文件夹和文件。我确认了这一点,一切都该有的样子。

然而,如果我执行 pip install .python setup.py install 等任何变体,则 data_file 会被完全忽略,和/或放置在错误的目录中,并且不会出现在创建的 builddist 文件夹中的任何位置。因此,显然我所有的测试都失败了,因为它们无法加载不存在的文件。当我有时成功复制它们时,它们也不存储在 venv 上的安装文件夹中,而是存储在 venv 的根目录中。

有趣的是,文件是在安装时处理的,在使用python setup.py install安装时我不断获得控制台输出,如下所示:

正在复制 data_needed/needed_folder/needed_file.notpy-> /Users/.../venv/hr_datapool/data_needed/needed_folder/

如果我使用python setup.py install,(而不是在使用pip 安装。)。

根据文档

目录应该是相对路径。它被解释为相对于安装前缀(Python 的 sys.prefix 用于系统 装置; site.USER_BASE 用于用户安装)。迪斯图尔斯 允许目录是绝对安装路径,但这是 不鼓励,因为它与轮封装格式不兼容。 不使用文件中的目录信息来确定最终的 安装文件的位置;仅使用文件名。

注意亮点。但是,在我的示例中,它不会相对于包含包的目录进行安装,而是安装到虚拟环境根目录中自己的文件夹中,这使得它实际上无法从我的代码中访问。我确保我在 setup.py 中设置了相对路径,但这种情况仍然发生。

如何确保所需的 data_files 安装在模块的目标目录中,而不是单独安装到虚拟环境的根目录中?

I am currently so confused about installation of my own Python packages... Between setup.pys, sdists and wheels, no matter what I do, I can't seem to achieve what I want - which is to have a bunch of non-.py data files installed and kept in the virtual environment with the same structure I have in my project folder after the installation.

I've read all kinds of documentations, and created a setup.py file that has a data_files field that contains all the data files I need in my installation.

I have the following structure:

.
|__ requirements.txt
    setup.py
    hr_datapool
    |__ __init__.py
    |__ data_needed
        |__ needed_folder
           |__ needed_file.notpy
    |__ one_module
        |__ __init__.py
            |__ misc_tools.py
            |__tests
                |__ test_tools.py
    |__ other_module
            ...

And data_needed contains non-.py data files that are needed for misc_tools.py (and thus tests.py) to run.

Because of this, I added a data_files into my setup.py that contains all the folders and files I need. This I confirmed, everything is there what should be.

And yet, if I do any variation of pip install ., python setup.py install or the likes, the data_files are completely ignored, and/or placed in the wrong directory and don't appear anywhere in the created build or dist folders. And because of this, obviously all my tests fail, since they can't load files that are not there. Neither are they stored in the installation folder on the venv when I sometimes do succeed in copying them, but rather in the root of the venv.

The funny thing is, the files are handled while installing, I keep getting console output when installing with python setup.py install like:

copying data_needed/needed_folder/needed_file.notpy-> /Users/.../venv/hr_datapool/data_needed/needed_folder/

but only if I use python setup.py install, (not when using pip install .).

According to the documentation:

The directory should be a relative path. It is interpreted relative to the installation prefix (Python’s sys.prefix for system
installations; site.USER_BASE for user installations). Distutils
allows directory to be an absolute installation path, but this is
discouraged since it is incompatible with the wheel packaging format.
No directory information from files is used to determine the final
location of the installed file; only the name of the file is used.

Notice the highlights. However, in my example, it doesn't install relative to the directory containing the package, but it installs into its own folder in the root of the virtual environment, making it practically unreachable from within my code. I made sure I se relative paths in my setup.py, but still this happens.

How can I make sure the required data_files install within the target directory of the module, and not separately into the root of the virtual environment?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文