如何直接访问 Py2app(或 Py2exe)程序中的资源? [英] How to directly access a resource in a Py2app (or Py2exe) program?

查看:37
本文介绍了如何直接访问 Py2app(或 Py2exe)程序中的资源?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这主要用于 Py2app,但我也计划移植到 Windows,因此 Py2exe 也适用.

This is mostly for Py2app, but I plan to also port to Windows so Py2exe is also applicable.

对于 Mac:如何从 Python 代码访问我的应用程序包的 Resources 文件夹?对我来说,理想的方法是将此文件夹的路径放入一个变量中,我的类将其添加到他们需要访问的任何文件中.鉴于 OSX 应用程序包的可移植性,此资源文件夹可以移动,因此假设它始终位于/Applications/MyApp.app/Contents/Resources 显然是不可接受的.

For Mac: How can I access the Resources folder of my app bundle from Python code? The ideal way for me would be to get the path to this folder into a variable that my classes prepend to any file they need to access. Given the portable nature of OSX app bundles this Resources folder can move, so it's obviously not acceptable to assume it'll always be at /Applications/MyApp.app/Contents/Resources.

对于开发,我可以将此变量预设为./Resources-test"之类的内容,但对于最终发行版,我需要能够找到 Resources 文件夹以将其中的文件作为文件对象访问.

For development I can preset this variable to something like "./Resources-test" but for the final distribution I would need to be able to locate the Resources folder to access files therein as file objects.

对于 Windows:如果我使用 py2exe,获取应用程序运行路径的正确方法是什么?(想想便携式应用程序 - 该应用程序可能从程序文件、某人闪存驱动器上的目录或临时目录中运行!)在 Windows 上,只需知道 .exe 文件的位置并只有一个资源文件夹那里.(我打算使用 wxwidgets 制作跨平台应用.)

For Windows: If I use py2exe, what's the correct way to get the path to where the application is running from? (Think portable app - the app might be running from Program files, or a directory on someone's flash drive, or in a temp directory!) On Windows it'd be suitable to simply know where the .exe file is and just have a Resources folder there. (I plan to make cross-platform apps using wxwidgets.)

谢谢

推荐答案

默认情况下,Resource"文件夹是 py2app 启动的应用程序的当前工作目录.

By default the 'Resource' folder is current working directory for applications started by py2app.

此外环境变量RESOURCEPATH"设置为资源文件夹的绝对路径.

Futhermore the environment variable "RESOURCEPATH" is set to the absolute path of the resource folder.

这篇关于如何直接访问 Py2app(或 Py2exe)程序中的资源?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆