简体   繁体   English

如何将 package 之外的文件附加到我的 Python package

[英]How can I attach a file outside the package to my Python package

I have a web application that displays the ABOUT.md file of the project.我有一个显示项目的 ABOUT.md 文件的 web 应用程序。 The project has the following file tree:该项目具有以下文件树:

project_folder/
  main_package/
    assets/icon.png
    __init__.py
    app.py
  .gitignore # And other files
  README.md
  ABOUT.md
  setup.cfg
  setup.py

In app.py I have a webserver that renders and provides the contents of the file README.md .在 app.py 中,我有一个网络服务器,它呈现并提供文件README.md的内容。 It has a code like this:它有这样的代码:

from main_package import __file__ as mpfile

# First parent is just the folder where __init__.py is located.
ABOUT_MD = Path(mpfile).parent.parent / 'ABOUT.md'

This works without building, but if I build the wheel and install it in other environment, it stops working.这无需构建即可工作,但是如果我构建轮子并将其安装在其他环境中,它将停止工作。

I modified the config in setup.cfg so it includes the ABOUT.md我修改了setup.cfg中的配置,因此它包含 ABOUT.md

[options.package_data]
main_package = 
  ../ABOUT.md
  assets/*

But this copies the file to the root of site_packages , which I think is a bit dirty.但这会将文件复制到site_packages的根目录,我认为这有点脏。

I want to keep the ABOUT.md file available in the root folder, so it remains very accessible through GitHub, but I also want to be able to build and publish my package.我想在根文件夹中保留ABOUT.md文件,因此它仍然可以通过 GitHub 访问,但我也希望能够构建和发布我的 package。

Idea:主意:

Modify the build system to copy the ABOUT.md from the root to main_package/assets/ABOUT.md while creating the wheel.修改构建系统以在创建轮子时将 ABOUT.md 从根目录复制到main_package/assets/ABOUT.md Then, add an if inside app.py that loads the correct file depending on where it is.然后,在app.py中添加一个 if 来根据文件的位置加载正确的文件。 The problem is that I don't know how to make the build system copy the file to this path.问题是我不知道如何让构建系统将文件复制到这个路径。

Update 2022-07-18: Why not linking? 2022-07-18 更新:为什么不链接?

In response to @Leander回应@Leander

If we use a Hard Link, that info is not propagated through the version control system (git), so It will appear to be two different files in every other computer, and we would have to sync the data somehow (with hooks or any kind of system), plus it would take double space in disk.如果我们使用硬链接,则该信息不会通过版本控制系统 (git) 传播,因此在每台其他计算机上它似乎是两个不同的文件,我们将不得不以某种方式同步数据(使用钩子或任何类型系统),加上它会占用双倍的磁盘空间。

If we use a Symlink, the used space is reduced, but the web view of the repositories can't follow symlinks, so the ABOUT.md ends unusable (it just displays the plaintext path of the symlink).如果我们使用符号链接,使用的空间会减少,但是存储库的 web 视图不能跟随符号链接,因此 ABOUT.md 结束不可用(它只显示符号链接的明文路径)。

Symlink符号链接

Symlinking the other way around (original in ./ABOUT.md and creating a symbolic link at main_package/assets/ABOUT.md to ../../ABOUT.md works flawlessly.以相反的方式进行符号链接(原始在./ABOUT.md中并在main_package/assets/ABOUT.md创建符号链接到../../ABOUT.md完美无缺。

  • Is true that GitHub (for example) doesn't follow symlinks, but the ABOUT.md in the root folder remains readable.确实,GitHub(例如)不遵循符号链接,但根文件夹中的 ABOUT.md 仍然可读。
  • Wheel packaging copies the file, not the symlink, so the application can be packaged and installed in any computer. Wheel 打包复制文件,而不是符号链接,因此可以在任何计算机上打包和安装应用程序。
  • No need to add an If or try/catch when importing the file, just main_package/assets/ABOUT.md .导入文件时无需添加 If 或 try/catch,只需main_package/assets/ABOUT.md

Your problem is very similar to the versioning problem of repositories.您的问题与存储库的版本控制问题非常相似。 Typically, the version is provided by the git repository as tag and potentially gets a suffix from the commit hash and a dirty flag if not yet committed.通常,该版本由 git 存储库作为标记提供,并且可能从提交 hash 和未提交的脏标志中获取后缀。 A very light weight solution to this problem is provided by https://github.com/jbweston/miniver so I would recommend you to follow the idea. https://github.com/jbweston/miniver为这个问题提供了一个非常轻量级的解决方案,所以我建议你遵循这个想法。 It basically checks the version string in a file and if the placeholder is found, it uses git to compose the version string, otherwise the given value.它基本上检查文件中的版本字符串,如果找到占位符,则使用 git 组成版本字符串,否则使用给定值。 This value is only set during the build process, meaning it is overwritten when executing the setup.py and therefore only appears in installed instances of the software.此值仅在构建过程中设置,这意味着它在执行setup.py时被覆盖,因此仅出现在已安装的软件实例中。

In your case, just replace the version with the ABOUT.md content during the build process.在您的情况下,只需在构建过程中将版本替换为ABOUT.md内容。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python:如何使用包含在包外的修改版本覆盖包中的一个模块? - Python: How can I override one module in a package with a modified version that lives outside the package? 如何为ROS软件包(脚本文件夹中的python代码)生成deb可安装文件? - How can I generate deb installable file for my ROS package(python code in the script folder)? 如何执行位于我的Python软件包安装目录中的二进制文件(没有sudo) - How can I execute a binary file located in the installation directory of my Python package (without sudo) 如何跟踪python软件包的用户群? - How can I track my python package's user base? 如何在python中导入我自己的包? - How can I import my own package in python? 如何为单个Python文件创建包? - How can I create a Package for a single Python file? 如何使python文件可作为脚本运行并且可作为包导入? - How can I make a python file runnable as a script and importable as a package? 在数据块上,我无法从文件中读取:/databricks/python/lib/python3.7/site-package/my_package - On databricks I can't read from file:/databricks/python/lib/python3.7/site-package/my_package 如何构建我的Python包? - How do I structure my Python package? 如何将python模块及其依赖项(例如Numpy / Scipy)打包到一个文件中,例如dll以进行C#调用? - How can I package my python module and its dependencies such as Numpy/Scipy into one file like dll for C# calls?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM