How to directly access a resource in a Py2app (or Py2exe) program?

2024/9/8 8:23:04

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

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.

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.

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.)

Thanks

Answer

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

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

https://en.xdnf.cn/q/72964.html

Related Q&A

Workflow for Python with Docker + IDE for non-web applications

I am currently trying to insert Docker in my Python development workflow of non-web applications.What are the current best practices in Python development using Docker and an IDE? I need the possibili…

Django Deserialization Error Problem installing Fixture

Traceback (most recent call last):File "/Users/sparshkedia/Desktop/task/venv/lib/python3.6/site-packages/django/core/serializers/json.py", line 69, in Deserializeryield from PythonDeserialize…

Python HTTP Exception Handling

Im running a program that downloads files from a web sit. Ive introduced one exception handling urllib.error.HTTPError, but now Im getting from time to time additional errors that Im not sure how to ca…

Weird lambda behaviour in loops [duplicate]

This question already has answers here:What do lambda function closures capture?(8 answers)Closed 10 years ago.I stumbled upon a behaviour in python that I have a hard time understanding. This is the…

Why does inspect return different line for class inheriting from superclass?

While trying to figure out if a function is called with the @decorator syntax, we realized that inspect has a different behaviour when looking at a decorated class that inherits from a superclass.The f…

Sorting a list of tuples with multiple conditions

I am currently trying to sort the following list:list_ = [(1, 0101), (1, 1010), (1, 101), (2, 01), (2, 010), (2, 10)]These are the steps I want to take in order to sort it:Sort the list by the value of…

Tensorboard error: Tensor object has no attribute value

My goal: Add arbitrary text to tensorboard.My code:text = "muh teeeext" summary = tf.summary.text("Muh taaaag", tf.convert_to_tensor(text)) writer.add_summary(summary)My error:File …

How to embed Google Speech to Text API in Python program? [closed]

Closed. This question is seeking recommendations for books, tools, software libraries, and more. It does not meet Stack Overflow guidelines. It is not currently accepting answers.We don’t allow questi…

shell script remote execution using python

Is there a way that I can use Python on Windows to execute shell scripts which are located on a remote Unix machine?P.S: Sorry about the late edit. I do know of Paramiko, but I wanted to know if there…

Shutdown for socketserver based Python 3 server hangs

I am working on a "simple" server using a threaded SocketServer in Python 3.I am going through a lot of trouble implementing shutdown for this. The code below I found on the internet and shut…