在 distutils/setuptools 之前和之后访问数据文件
我正在做一个独立于平台的 PyQt 应用程序。 我打算使用 setuptools 编写 setup.py 文件。 到目前为止,我已经成功地解除了平台的技术,例如根据平台加载 setup() 的特定选项,以便在 Windows 上使用 py2exe...等等...
但是,通过我的应用程序,我正在分发一些主题、HTML 和图像,我需要在运行时在应用程序中加载这些图像。 到目前为止,它们存储在应用程序的主题/目录中。
我一直在阅读有关 setuptools 和 distutils 的文档,并发现如果我为 setup() 提供 data_files 选项,其中包含主题/目录中要安装在“share/MyApp/themes/”中的所有文件,它将被安装/usr/ 前缀,或平台上的任何 sys.prefix。 我假设无论我在什么平台上,我都会使用 os.path.join(sys.prefix, "share", "MyApp", "themes") 找到数据文件,对吧?
但是,我也希望能够在开发过程中访问数据文件,它们位于相对于应用程序源的主题/目录中。 我该怎么做呢? 有没有一些聪明的方法来确定应用程序是否已安装? 或者是一个映射到数据文件的实用程序,无论它们目前位于何处?
我真的很讨厌添加各种丑陋的黑客来查看是否有与源相关的主题,或者在 sys.prefix/share...等等...在开发过程中如何找到数据文件? 并在任意平台上安装后?
I'm doing a platform independent PyQt application. I intend to use write a setup.py files using setuptools. So far I've managed to detech platform, e.g. load specific options for setup() depending on platform in order to use py2exe on Windows... etc...
However, with my application I'm distributing some themes, HTML and images, I need to load these images in the application at runtime. So far they are stored in the themes/ directory of the application.
I've been reading documentation on setuptools and distutils, and figured out that if I gave setup() the data_files options with all the files in the themes/ directory to be installed in "share/MyApp/themes/" it would be installed with a /usr/ prefix, or whatever sys.prefix is on the platform.
I assume that I would find the data files using os.path.join(sys.prefix, "share", "MyApp", "themes") nomatter what platform I'm on, right?
However, I want to be able to access the data files during development too, where they reside in the themes/ directory relative to application source. How do I do this?
Is there some smart way to figure out whether the application has been installed? Or a utility that maps to the data files regardless of where they are, at the moment?
I would really hate to add all sorts of ugly hacks to see if there are themes relative to the source, or in sys.prefix/share... etc... How do I find there data files during development? and after install on arbitrary platform ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以尝试
pkg_resources
:You could try
pkg_resources
:我使用了一个名为 data_file 的实用程序方法:
我将其放入项目中的 init.py 文件中,然后从包中的任何位置调用它以获取与包相关的文件。
setuptools提供了类似的功能,但这不需要setuptools。
I've used a utility method called data_file:
I put this in the init.py file in my project, and then call it from anywhere in my package to get a file relative to the package.
Setuptools offers a similar function, but this doesn't need setuptools.
您应该使用 pkgutil/pkg_resources 模块加载数据文件。 它甚至可以在压缩鸡蛋中发挥作用。
You should use the pkgutil/pkg_resources module to load the data files. It even works from within ziped eggs.