Python: determine if running inside a .app bundle or not? - python

I'm working with py2app on Python 2, and was wondering if there's a way to detect from code whether my app is running as part of a compiled .app file, or as a standalone script.
This is important because some of my code dynamically loads a .dylib file. When running natively as a script, this code runs fine; however, when running inside the .app, I get something like:
Traceback (most recent call last):
File ".../python/game/dist/main.app/Contents/Resources/__boot__.py", line 316, in <module>
_run()
File ".../python/game/dist/main.app/Contents/Resources/__boot__.py", line 311, in _run
exec(compile(source, path, 'exec'), globals(), globals())
File ".../python/game/dist/main.app/Contents/Resources/main.py", line 1, in <module>
import game
File "game.pyc", line 10, in <module>
File "gui.pyc", line 5, in <module>
File "audio/__init__.pyc", line 2, in <module>
File "audio/pybass.pyc", line 65, in <module>
File "ctypes/__init__.pyc", line 365, in __init__
OSError: dlopen(...python/game/dist/main.app/Contents/Resources/lib/python2.7/site-packages.zip/audio/libbass.dylib, 6): no suitable image found. Did find:
.../python/game/dist/main.app/Contents/Resources/lib/python2.7/site-packages.zip/audio/libbass.dylib: stat() failed with errno=20
2013-07-15 02:50:21.146 main[10040:707] main Error
I'm thinking the best way would be to move the .dylib file outside of the library and into the Resources, then dynamically load it from there - but in order to do this I need to detect in code whether I'm running in the .app or not, and if so, look in a different location for the library (namely, the Resources folder of the app bundle)
Help appreciated!

Py2app sets sys.frozen to "macosx_app" (for application bundles).
A way to avoid your problem is to use the "packages" option:
setup(
...
options={
'py2app': {
'packages': ['audio'],
}
}
)
This includes the entire "audio" package in the application bundle as a directory instead of adding it to the site-packages zipfile. That way you don't have to detect whether or not you're in a application bundle.

Related

PyInstaller "You may load I/O plugins with the `skimage.io.use_plugin`"

useing Pyinstaller packages a python script
Pyinstaller version 3.2
OS:Ubuntu
Traceback (most recent call last):
File "<string>", line 57, in <module>
File "<string>", line 29, in feature_extract
File "caffe/io.py", line 295, in load_image
File "skimage/io/_io.py", line 100, in imread
File "skimage/io/manage_plugins.py", line 194, in call_plugin
RuntimeError: No suitable plugin registered for imread.
You may load I/O plugins with the `skimage.io.use_plugin` command. A list of all available plugins can be found using `skimage.io.plugins()`.
file_test returned -1
I have been getting above error. Could some one please tell me how would i fix it?
The problem seems to be related to this github issue, essentially the skimage.io._plugins submodule is making life hard for Pyinstaller.
To make sure everything you need is packaged you should have a hook file that contains
from PyInstaller.utils.hooks import collect_data_files, collect_submodules
datas = collect_data_files("skimage.io._plugins")
hiddenimports = collect_submodules('skimage.io._plugins')
(or if you already have a hook file with these, extend the current datas and hiddenimports).

How do I install PDFNet python module?

I have been running in circles trying to figure out how to get my Django app to recognise the trial version of PDFNet:
https://pypi.python.org/pypi/PDFTron-PDFNet-SDK-for-Python/5.7
I tried adding the files to my ~/usr/bin directory, I tried dropping them into my virtualenv's bin directory. Neither has worked. I have read all the documentation i can find. I am too new of a Python developer to look at this package and know how to install it and utilize it in my project.
Please help!
update I attempted to create an app folder, and list this component as an app in the app list, but when I run the code to start the application, I get the following error:
Library not loaded: #rpath/libPDFNetC.dylib
I placed all of the lib files into this folder within my project:
__init.py (empty)
_PDFNetPython.so
libPDFNetC.dylib
PDFNetPython.py
PDFNetRuby.bundle
I used the following import code at the top of the py file I was attempting to use the component on:
import site
site.addsitedir("../pdfnetc")
from pdfnetc.PDFNetPython import *
I put the lib files into a app folder named pdfnetc. once I had this, the import statements were no longer listed as unfound by pycharm.
here is the stack trace:
Traceback (most recent call last):
File "/Applications/PyCharm.app/Contents/helpers/pydev/pydevd.py", line 2217, in <module>
globals = debugger.run(setup['file'], None, None)
File "/Applications/PyCharm.app/Contents/helpers/pydev/pydevd.py", line 1643, in run
pydev_imports.execfile(file, globals, locals) # execute the script
File "/Users/ntregillus/myapp/manage.py", line 12, in <module>
execute_from_command_line(sys.argv)
File "/Users/ntregillus/Envs/myapp/lib/python2.7/site-packages/django/core/management/__init__.py", line 385, in execute_from_command_line
utility.execute()
File "/Users/ntregillus/Envs/myapp/lib/python2.7/site-packages/django/core/management/__init__.py", line 354, in execute
django.setup()
File "/Users/ntregillus/Envs/myapp/lib/python2.7/site-packages/django/__init__.py", line 21, in setup
apps.populate(settings.INSTALLED_APPS)
File "/Users/ntregillus/Envs/myapp/lib/python2.7/site-packages/django/apps/registry.py", line 108, in populate
app_config.import_models(all_models)
File "/Users/ntregillus/Envs/myapp/lib/python2.7/site-packages/django/apps/config.py", line 202, in import_models
self.models_module = import_module(models_module_name)
File "/usr/local/Cellar/python/2.7.9/Frameworks/Python.framework/Versions/2.7/lib/python2.7/importlib/__init__.py", line 37, in import_module
__import__(name)
File "/Users/ntregillus/myapp/statements/models.py", line 12, in <module>
from statements.managers import StatementTemplateManager, StatementManager
File "/Users/ntregillus/myapp/statements/managers.py", line 8, in <module>
from statements.utils import render_to_pdf, StatementContextBuilder
File "/Users/ntregillus/myapp/statements/utils.py", line 23, in <module>
from pdfnetc.PDFNetPython import *
File "/Users/ntregillus/myapp/pdfnetc/PDFNetPython.py", line 28, in <module>
_PDFNetPython = swig_import_helper()
File "/Users/ntregillus/myapp/pdfnetc/PDFNetPython.py", line 24, in swig_import_helper
_mod = imp.load_module('_PDFNetPython', fp, pathname, description)
ImportError: dlopen(/Users/ntregillus/myapp/pdfnetc/_PDFNetPython.so, 2): Library not loaded: #rpath/libPDFNetC.dylib
Referenced from: /Users/ntregillus/myapp/pdfnetc/_PDFNetPython.so
Reason: image not found
python -m pip install PDFNetPython3
home page link here
Below are steps to install the PDFNet module:
Download the Python and Ruby prebuilt binaries. Make sure you download the right architecture for your Python interpreter.
Extract the downloaded zip file and navigate to it.
Navigate to the /PDFNetC/Lib directory of the Python SDK download and execute:
> chmod a+x fix_rpaths.sh
./fix_rpaths.sh
I am not knowledgable of django, but take a close look at the sample py files that are in the SDK. For example, take a look at PDFNetC/Samples/AddImageTest/PYTHON folder. In there is a .py file, and look at the first 4 lines of code, they indicate what needs to load.
Please note that you need all the files that are in PDFNetC/Lib folder, including the PDFNetC.dll file.
If this does not help, please post the exact (first) error message you get.
It just works fine for me when i try to run the shell script, btw iam using the python as my programming language for pdfnet,
Try to use these commands to run the pdfn
#!/bin/sh
TEST_NAME=PDFATest
export LD_LIBRARY_PATH=../../../PDFNetC/Lib
python -u $TEST_NAME.py
The PDFATest specifies the python code, and then the export will get the required packages such as the PDFNet which is in
../../Lib
folder so make sure you use that file in the lib and then run your code and you will not have any issues with PDFNet, btw i dont answer this with respect to django but sure this will help you out.

App Engine Python won't start

I don't know what changed, but suddenly none of my Python App Engine projects will start on my PC. They start on my MacBook just fine, so there's something about the installation on my PC that's messed up. I've tried going back to 1.8.8, but that doesn't work. I've also tried using a fresh Python installation, but that didn't work. I've even tried using port 8080 (I usually use port 80, as I run the app launcher as admin).
This is what shows up in the error log when I click start on one of my projects:
2014-02-05 10:42:24 Running command: "['c:\\users\\jonathan\\appdata\\local\\enthought\\canopy\\user\\scripts\\pythonw.exe', 'C:\\Program Files (x86)\\Google\\google_appengine\\dev_appserver.py', '--skip_sdk_update_check=yes', '--port=80', '--admin_port=8000', 'C:\\Users\\jonathan\\My Repositories\\my-project']"
Traceback (most recent call last):
File "C:\Program Files (x86)\Google\google_appengine\dev_appserver.py", line 197, in <module>
_run_file(__file__, globals())
File "C:\Program Files (x86)\Google\google_appengine\dev_appserver.py", line 193, in _run_file
execfile(script_path, globals_)
File "C:\Program Files (x86)\Google\google_appengine\google\appengine\tools\devappserver2\devappserver2.py", line 32, in <module>
from google.appengine.tools.devappserver2.admin import admin_server
File "C:\Program Files (x86)\Google\google_appengine\google\appengine\tools\devappserver2\admin\admin_server.py", line 29, in <module>
from google.appengine.tools.devappserver2.admin import console
File "C:\Program Files (x86)\Google\google_appengine\google\appengine\tools\devappserver2\admin\console.py", line 22, in <module>
from google.appengine.tools.devappserver2 import module
File "C:\Program Files (x86)\Google\google_appengine\google\appengine\tools\devappserver2\module.py", line 69, in <module>
from google.appengine.tools.devappserver2 import wsgi_server
File "C:\Program Files (x86)\Google\google_appengine\google\appengine\tools\devappserver2\wsgi_server.py", line 31, in <module>
from cherrypy import wsgiserver
File "C:\Program Files (x86)\Google\google_appengine\lib\cherrypy\cherrypy\__init__.py", line 278, in <module>
_pydoc._builtin_resolve = _pydoc.resolve
AttributeError: 'module' object has no attribute 'resolve'
2014-02-05 10:42:32 (Process exited with code 1)
Also, I'm using Windows 7 x64. My original installation of Python is from Enthought x64, but I did try using the basic x64 Python as well. As mentioned, I downgraded from 1.8.9 to 1.8.8, to no avail.
From a quick Google search, other users are suggesting it's a bug in pyvenv. Since not very many people are still using CherryPy, and it seems to only manifest itself in whatever CherryPy is doing there, that's probably it.
Bug the gist is that somewhere, only on Windows, you have a pydoc.py file which is being imported instead of the standard library's pydoc file.
So, find the offending pydoc.py file in your import path, and rename it. You can most likely find it by doing something like this:
import pydoc
print pydoc.__file__

Trouble running an application made with PyInstaller, importing GTK--searching nonexistent directories

I am trying to create an executable of my Python application that uses PyGTK to make a GUI. I have a well-established, automated build process using Pyinstaller that has worked for me for a previous application. Suffice it to say that it calls the usual Makespec.py and Build.py with 32-bit Python 2.7, with Pyinstaller configured for 32 bits. The resulting 32-bit application works fine on my machine and another machine running Windows 7 64-bit, but fails on 32-bit Windows XP with this error:
C:\OutNav_0_64\OutNav_0.64>outnav
Traceback (most recent call last):
File "<string>", line 23, in <module>
File "C:\Pyinstaller-1.5\iu.py", line 436, in importHook
File "C:\Pyinstaller-1.5\iu.py", line 521, in doimport
File "C:\Users\462974\Documents\Local Sandbox\tools\utilities\Oni\build\pyi.win32\OutNav\outPYZ1.pyz/gtk", line 40, in
<module>
File "C:\Pyinstaller-1.5\iu.py", line 477, in importHook
File "C:\Pyinstaller-1.5\iu.py", line 495, in doimport
File "C:\Pyinstaller-1.5\iu.py", line 297, in getmod
File "C:\Pyinstaller-1.5\archive.py", line 468, in getmod
File "C:\Pyinstaller-1.5\iu.py", line 109, in getmod
ImportError: DLL load failed: The specified procedure could not be found.
The strange part is, there is no C:\Pyinstaller-1.5 directory on my machine or the one experiencing the error. I have no idea why it is attempting to run code from this nonexistent directory, or what the missing DLL is. Can anyone help me fold PyGTK into my application?
NOTE: The first line of the trace, line 23 in my program, is
import gtk
UPDATE: My manager successfully ran it on 64-bit Windows XP.
UPDATE 2: He was mistaken, it was 64-bit Windows 7. It has the same problem on his Windows XP installation.
Also, on the original machine it failed on, from the directory of the executable, I did this:
>>> import imp
>>> fp = open('gtk._gtk.pyd', 'rb')
>>> mod = imp.load_module('gtk._gtk', fp, 'gtk._gtk.pyd', ('.pyd', 'rb', 3))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: DLL load failed: A dynamic link library (DLL) initialization routine failed.
As far as I can tell, the .pyd file (which is in the format of a .DLL file) generated by pyinstaller on Windows 7 is incompatible with Windows XP. My solution was to simply recreate my build process on a Windows XP machine, which solved it to my satisfaction.

Py2exe can't seem to find my file in library.zip

I am trying to create an exe file from my program written in Python 2.7 (using GTK+ layouts). I have consulted this guide: http://www.py2exe.org/index.cgi/Py2exeAndPyGTK and tried to set up my files accordingly. I succesfully make an .exe file, but when trying to run it, i get this error:
Traceback (most recent call last):
File "cipherka.py", line 183, in <module>
File "cipherka.py", line 18, in __init__
GError: Nelze otevřít soubor "C:\Repa\cipherka\dist\library.zip\gui.xml": No such file or directory
While I cannot find the gui.xml file in the library.zip archive, it is available on the same level as the main .exe (cipherka.exe) file. If i copy it into the .zip file on my own, it doesn't solve the problem.
Here is my setup.py file:
from distutils.core import setup
import py2exe
import glob
setup(
name="cipherka",
windows = [
{
'script': 'cipherka.py',
}
],
options = {
'py2exe': {
'packages':'encodings',
'includes': 'cairo, pango, pangocairo, atk, gobject, gio',
}
},
data_files=['gui.xml',
'README',
("modules", glob.glob("modules/*.*")),
("media", glob.glob("media/*.png"))
]
)
Any help will be wildly appreciated! And I can give you any necessary info when needed. Thx
UPDATE:
Allright, I implemented the change. However, it does weird things. When i use:
path = os.path.dirname(__file__).replace('\\library.zip','')
it works when ran from the .py file, but fails when compiled with this error:
Traceback (most recent call last):
File "cipherka.py", line 217, in <module>
File "cipherka.py", line 18, in __init__
NameError: global name '__file__' is not defined
When i use
path = os.path.dirname('gui.xml').replace('\\library.zip','')
instead - the program stops running from python and the compiled version gives me the following:
C:\Repa\cipherka\dist\cipherka.exe:188: GtkWarning: gtk_tree_path_append_index: assertion `index >= 0' failed
C:\Repa\cipherka\dist\cipherka.exe:188: GtkWarning: gtk_tree_model_get_iter: assertion `path->depth > 0' failed
Traceback (most recent call last):
File "cipherka.py", line 217, in <module>
File "cipherka.py", line 55, in __init__
File "cipherka.py", line 188, in changed_cb
IndexError: could not find tree path
Any ideas?
"gui.xml" is in "C:\Repa\cipherka\dist\", not "C:\Repa\cipherka\dist\library.zip" as your code supposes.
This should work whether your program is compiled or not.
path = os.path.dirname(__file__).replace('\\library.zip','')
xml_file = open(os.path.join(path, 'gui.xml'))
the error reported is due to scripts running under py2exe do not have a __file__ global. Detect this and use sys.argv[0] instead in your .py file.

Categories

Resources