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

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.

Related

Py2exe NotImplementedError: resource_filename() only supported for .egg, not .zip

I am trying to convert a python file into an executable and for that I'm using py2exe. When running the setup.py script to generate the dist folder and the executable file everything goes well.
What goes wrong is that when I'm running the executable the following error message gets displayed:
Traceback (most recent call last):
File "q400.py", line 16, in <module>
File "SUAVE\__init__.pyc", line 12, in <module>
File "SUAVE\Plugins\__init__.pyc", line 11, in <module>
File "SUAVE\Plugins\load_plugin.pyc", line 37, in load_plugin
File "SUAVE\Plugins\pint\__init__.pyc", line 23, in <module>
File "SUAVE\Plugins\pint\unit.pyc", line 423, in __init__
File "pkg_resources\__init__.pyc", line 1171, in resource_filename
File "pkg_resources\__init__.pyc", line 1872, in get_resource_filename
NotImplementedError: resource_filename() only supported for .egg, not .zip
When I check the file unit.pyc at line 423 the code is:
data = pkg_resources.resource_filename(__name__, 'default_en.txt')
And when I check the library.zip in the dist folder there is no default_en.txt file, however, it is present in the site-packages module.
So is it because this file is a txt file that this error shows up?
I tried to force py2exe to import it but with no success.
Also I found a similar question here but the answer can't be applied in my case because I don't have any egg nor zip files implicated in the error (at least in my opinion).
Thank you

Can't run python script converted to exe

I have created a python program that sends data to google spreadsheets using the following external libaries:
json
gspread
oauth2client
The program works as expected, however when I tried to convert it to an executable, I would get the following error every time I rune the .exe file:
Traceback (most recent call last):
File "C:\Python27\lib\site-packages\cx_Freeze\initscripts\Console.py", line 27, in <module>
exec(code, m.__dict__)
File "Demo.py", line 13, in <module>
File "C:\Python27\lib\site-packages\oauth2client\util.py", line 140, in positional_wrapper
return wrapped(*args, **kwargs)
File "C:\Python27\lib\site-packages\oauth2client\client.py", line 1630, in __init__
_RequireCryptoOrDie()
File "C:\Python27\lib\site-packages\oauth2client\client.py", line 1581, in _RequireCryptoOrDie
raise CryptoUnavailableError('No crypto library available')
oauth2client.client.CryptoUnavailableError: No crypto library available
Would any of you guys know what the problem could be, and what steps I could take to solve it?
Thanks in advance
Edit: I tried to use both cx_freeze and py2exe. I got similar errors in both
Below is the setup.py I used in cx_freeze:
from cx_Freeze import setup, Executable
setup(name = "Demo" ,
version = "1.0" ,
description = "A python Demo" ,
executables = [Executable("Demo.py")])

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

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

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.

Bizzarre issue trying to make Rpy2 2.1.9 work with R 2.12.1, using Python 2.6 under Windows xp - Rpy can't find the R.dll?

I've been having a real issue trying to make Rpy2 play nice with my R install. I first tried installing the rpy2 MSI package, and this didn't appear to work. When I ran the recommended tests, it was giving me an error saying that it couldn't find the R.dll, because the new R installs (post 2.11) install the DLLs into an i386 folder, where rpy2 can't find them because its looking in the bin folder instead of the bin/i386 folder.
Then I tried to build the install from scratch myself using the command line tools (distutils) included with python. This didn't work, because setup.py claimed to be unable to find the R_home location. But I did work out that editing an environment variable (PATH) might show the rpy2 setup where to find the R installation. I then made a couple of edits to the environment, adding the "R_home" variable pointing to the bin/i386 directory, and made a new entry under the PATH variable, pointing to the same spot.
Unfortunately, when it found the R path, I got this issue instead:
running build
running build_py
running build_ext
Traceback (most recent call last):
File "setup.py", line 372, in <module>
[os.path.join('doc', 'source', 'rpy2_logo.png')])]
File "C:\Python26\lib\distutils\core.py", line 152, in setup
dist.run_commands()
File "C:\Python26\lib\distutils\dist.py", line 975, in run_commands
self.run_command(cmd)
File "C:\Python26\lib\distutils\dist.py", line 995, in run_command
cmd_obj.run()
File "C:\Python26\lib\distutils\command\build.py", line 134, in run
self.run_command(cmd_name)
File "C:\Python26\lib\distutils\cmd.py", line 333, in run_command
self.distribution.run_command(command)
File "C:\Python26\lib\distutils\dist.py", line 994, in run_command
cmd_obj.ensure_finalized()
File "C:\Python26\lib\distutils\cmd.py", line 117, in ensure_finalized
self.finalize_options()
File "setup.py", line 111, in finalize_options
config += get_rconfig(r_home, about)
File "setup.py", line 264, in get_rconfig
rc = RConfig.from_string(rconfig)
File "setup.py", line 252, in from_string
+ '\nin string\n' + string)
ValueError: Invalid substring in string
So I went back to trying to use the premade install, thinking that maybe the new edits to the environment might work but got this issue here
Traceback (most recent call last):
File "<string>", line 245, in run_nodebug
File "C:\Documents and Settings\User\Desktop\rpy2-2.1.9\rpy\tests.py", line 3, in <module>
import rpy2.robjects.tests
File "C:\Python26\lib\site-packages\rpy2\robjects\__init__.py", line 12, in <module>
import rpy2.rinterface as rinterface
File "C:\Python26\lib\site-packages\rpy2\rinterface\__init__.py", line 56, in <module>
raise RuntimeError("Unable to locate R.dll within %s" % R_HOME)
RuntimeError: Unable to locate R.dll within C:\Program Files\R\R-2.12.1\bin\i386
This is REALLY weird, because (as anyone can check on their own install) R installs R.dll into "C:\Program Files\R\R-2.12.1\bin\i386" and I've checked and verified that its in there, and I've pointed rpy2 to this directory in the windows default PATH! I know for a fact that rpy2 is looking in the right place, but can't understand why its not seeing R.dll.
So why can't rpy2 find it? And does anyone know a way to get rpy2 working with R 2.12? Perhaps I should try the newer rpy2 2.2.0 version? Its still in development though, and 1.9 is supposed to be able to handle R 2.12 according to this website so I don't know what to do...
Thanks to anyone who can help out...
[EDIT] I've also tried these instructions over here but they return the same "can't find DLL" error... Unless you change the environment variable "R_home" to point straight at the c/program files/R/R 2.12 directory instead of into the i386 subdirectory.
If it points at the right place, you get these errors back. This looks a bit more promising... But its still pretty bad!
.......................F....................................F.FFF...F....................................................................F..................................
======================================================================
FAIL: testNewWithoutInit (rpy2.rinterface.tests.test_SexpVector.SexpVectorTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
File "C:\Python26\lib\site-packages\rpy2\rinterface\tests\test_SexpVector.py", line 43, in testNewWithoutInit
self.assertTrue(False) # worked when tested, but calling endEmbeddedR causes trouble
AssertionError
======================================================================
FAIL: testCallErrorWhenEndedR (rpy2.rinterface.tests.test_EmbeddedR.EmbeddedRTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
File "C:\Python26\lib\site-packages\rpy2\rinterface\tests\test_EmbeddedR.py", line 122, in testCallErrorWhenEndedR
self.assertTrue(False) # worked when tested, but calling endEmbeddedR causes trouble
AssertionError
======================================================================
FAIL: testReadConsoleWithError (rpy2.rinterface.tests.test_EmbeddedR.EmbeddedRTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
File "C:\Python26\lib\site-packages\rpy2\rinterface\tests\test_EmbeddedR.py", line 117, in testReadConsoleWithError
self.assertTrue(errorstring.startswith('Traceback'))
AssertionError
======================================================================
FAIL: testSetReadConsole (rpy2.rinterface.tests.test_EmbeddedR.EmbeddedRTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
File "C:\Python26\lib\site-packages\rpy2\rinterface\tests\test_EmbeddedR.py", line 97, in testSetReadConsole
self.assertEquals(yes.strip(), res[0])
AssertionError: 'yes' != ''
======================================================================
FAIL: testSetWriteConsole (rpy2.rinterface.tests.test_EmbeddedR.EmbeddedRTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
File "C:\Python26\lib\site-packages\rpy2\rinterface\tests\test_EmbeddedR.py", line 36, in testSetWriteConsole
self.assertEquals('[1] "3"\n', str.join('', buf))
AssertionError: '[1] "3"\n' != ''
======================================================================
FAIL: testWriteConsoleWithError (rpy2.rinterface.tests.test_EmbeddedR.EmbeddedRTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
File "C:\Python26\lib\site-packages\rpy2\rinterface\tests\test_EmbeddedR.py", line 55, in testWriteConsoleWithError
self.assertTrue(errorstring.startswith('Traceback'))
AssertionError
======================================================================
FAIL: testVectorUnicodeCharacter (rpy2.robjects.tests.testNumpyConversions.NumpyConversionsTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
File "C:\Python26\lib\site-packages\rpy2\robjects\tests\testNumpyConversions.py", line 54, in testVectorUnicodeCharacter
self.assertTrue(False) # arrays of unicode characters causing segfault
AssertionError
----------------------------------------------------------------------
Ran 172 tests in 0.407s
FAILED (failures=7)
Exit code: True
After many hours of searching on the web and trying out many different things, amongst others encountering the same issues as above, I finally got Rpy2 working on my windows 7 computer!
Basically, the crucial help came from this thread: http://www.mail-archive.com/rpy-list#lists.sourceforge.net/msg03348.html.
Summarized, these were the steps to get rpy2 up and running on Windows7:
Install rpy2 from this link: https://bitbucket.org/breisfeld/rpy2_w32_fix/issue/1/binary-installer-for-win32
Add the path to R.dll to the environment variable PATH (C:\Program Files\R\R-2.12.1\bin\i386 in my case)
Add an environment variable R_HOME (C:\Program Files\R\R-2.12.1 in my case)
Add an environment variable R_USER (simply my username in Windows)
Restart your Python IDE (otherwise the environment variables are not reloaded!)
RPy2 isn't tested on Windows. You can try using an older version (2.0.8) with a Windows installer, but that may have trouble with newer versions of R.
The author doesn't use Windows. If you've got the knowledge to get a newer version working on Windows, I'm sure he'd welcome contributions.
I'm not sure where to write this, as I can't comment (no reputation points), but feel it is useful info on this problem. The reason for the aggravating "Unable to locate R.dll within..." error message, even when you know that R.dll is located exactly where it says, is that the program isn't actually looking in that directory. I think the relevant action happens in rpy2's "init.py" module in these lines:
import win32api
os.environ['PATH'] += ';' + os.path.join(R_HOME, 'bin', _win_bindir)
os.environ['PATH'] += ';' + os.path.join(R_HOME, 'modules', _win_bindir)
os.environ['PATH'] += ';' + os.path.join(R_HOME, 'lib')
# Load the R dll using the explicit path
R_DLL_DIRS = ('bin', 'lib')
# Try dirs from R_DLL_DIRS
for r_dir in R_DLL_DIRS:
Rlib = os.path.join(R_HOME, r_dir, _win_bindir, 'R.dll')
if not os.path.exists(Rlib):
continue
win32api.LoadLibrary( Rlib )
_win_ok = True
break
# Otherwise fail out!
if not _win_ok:
raise RuntimeError("Unable to locate R.dll within %s" % R_HOME)
As you can see, the error message will always say that it is looking in whatever directory you have set as R_HOME, but it will actually be looking in the directory "R_HOME\ r_dir\win_bindir". So when you see a message like "Unable to locate R.dll within C:\Program Files\R\R-2.12.1\bin\i386", that's because it is actually looking for a directory named C:\Program Files\R\R-2.12.1\bin\i386\R-2.12.1\bin\i386\, or something like that.
Point 3 in Kadee's answer fixes this by leaving the path specified only down to immediately above the \bin level.

Categories

Resources