Py2exe bundling files into a single exe - python

I'm having some trouble getting Py2exe to bundle all the files into a single .exe. It works
fine for me when I don't bundle them together. So this is the setup.py script I use when I'm not bundling them together, and it always works:
from distutils.core import setup
import py2exe
setup(console=['test.py'])
So I wanted to bundle all the files into a single executable, so I used this setup.py script for that, and this is the one that doesn't work:
from distutils.core import setup
# I took this off the Internet
import py2exe, sys, os
sys.argv.append('py2exe')
setup(
options = {'py2exe': {'bundle_files': 1}},
windows = [{'script': "test.py"}],
zipfile = None,
)
When I run this script, a dist directory is created with the test.exe file. If I execute it
by typing "test.exe" this error message pops up:
See the logfile 'c:\Python26\dist\test.ext.log' for details
And this is the contents of that logfile:
Traceback (most recent call last):
File "test.py", line 1, in <module>
EOFError: EOF when reading a line
So does anyone know how I can do this? I just want to bundle all the files Py2exe generates
with test.py into a single executable. I know it can do this. Or are there any other ways in which this can be done?

just from the errorlog message, could you try again after assuring the last line of test.py ends with a carriage return? (press enter after the last line in test.py and save again)

Related

pyinstaller --onefile argument doesn't work with pygame [duplicate]

I am trying to build a python script via PyInstaller. I have used the following commands to configure, generate a spec file, and build:
wget pyinstaller.zip, extracted it, python Configure.py, etc, then:
python pyinstaller/Makespec.py --onefile myscript.py
python pyinstaller/Build.py myscript.spec
Here is the spec file it generated:
# -*- mode: python -*-
a = Analysis([os.path.join(HOMEPATH,'support/_mountzlib.py'), os.path.join(HOMEPATH,'support/useUnicode.py'), 'icinga.py'],
pathex=['/home/user/projects/icinga_python/releases/v2.1'])
pyz = PYZ(a.pure)
exe = EXE( pyz,
a.scripts,
a.binaries,
a.zipfiles,
a.datas,
name=os.path.join('dist', 'myscript'),
debug=False,
strip=False,
upx=True,
console=1 )
This built an executable file in dist/ directory. When trying to run this file, I get the following:
Traceback (most recent call last):
File "<string>", line 12, in <module>
File "/home/user/projects/myscript/releases/v2.1/pyinstaller/iu.py", line 455, in importHook
raise ImportError, "No module named %s" % fqname
ImportError: No module named mysql
If I moved this executable into the directory of the actual Python code, it gave different results:
Traceback (most recent call last):
File "<string>", line 12, in <module>
File "/home/user/projects/myscript/releases/v2.1/pyinstaller/iu.py", line 436, in importHook
mod = _self_doimport(nm, ctx, fqname)
File "/home/user/projects/myscript/releases/v2.1/pyinstaller/iu.py", line 521, in doimport
exec co in mod.__dict__
File "CLUSTER/mysql/icingasql.py", line 13, in <module>
import urllib2
File "/home/user/projects/myscript/releases/v2.1/pyinstaller/iu.py", line 455, in importHook
raise ImportError, "No module named %s" % fqname
ImportError: No module named urllib2
In the ... pyinstaller docs I see that --onefile is the option I need/want, but for some reason not everything is being compiled in.
The script is not really including anything fancy, just little quick modules I wrote for sql statements, and parsing certain websites.
The problem is that pyinstaller won't see second level imports. So if you import module A, pyinstaller sees this. But any additional module that is imported in A will not be seen.
There is no need to change anything in your python scripts. You can directly add the missing imports to the spec file.
Just add the following in a = Analysis(...):
hiddenimports=["mysql"],
This should be the result:
a = Analysis([os.path.join(HOMEPATH,'support/_mountzlib.py'), os.path.join(HOMEPATH,'support/useUnicode.py'), 'icinga.py'],
pathex=['/home/user/projects/icinga_python/releases/v2.1'], hiddenimports=["mysql"],)
After that run pyinstaller with the spec file as an argument.
This error can ocurre when you have dynamic imports in your code. In that case, pyinstaller don't include those packages in exe file. In that case you can:
Add unused import of those packages in your code
Tell pyinstaller to include it
One file option does not change anything in running your code. If you create --onefile exe all files created by pyinstaller are packed to exe file, and unpacked to local temp every time you run exe.
just gonna add my 2 cents because I encountered the same problem today - 6 years later :D
For Windows:
1) cmd => rightclick => with admin rights
2) Enter in cmd: "pip install pyinstaller"
3) navigate in cmd to the folder of "yourMain.py"
4) Enter in cmd: "pyinstaller --onefile --windowed yourMain.py"
5) If you import other scripts / data in "yourMain.py":
Manually enter the folder "dist" (gets created - where "yourMain.exe" should be by now),
and copy your scripts or folder structure there
(e.g. /assets/sounds; /assets/graphics; /scripts; anotherscript.py )
Then I was able to run the exe by double clicking.
Turned out to be pretty easy. What did the trick for me was the "--onefile" and adding my other files to the "dist" folder.
The "--windowed" is just so the python command window won't pop up when you start the exe.

How to create .exe using py2exe(or pyinstaller) on Ubuntu

Given:
- Ubuntu
- py2exe and pyinstaller
- Python script with setup.py (or else)
from distutils.core import setup
import py2exe
import os
setup(
version = "1.0",
description = 'foo',
url = "",
name = "foo",
console=[{
"script":"main.py",
"dest_base":"foo",
}],
zipfile = "shared.dll",
options = {"py2exe":{
'bundle_files': 1,
'optimize': 2,
"dll_excludes": ['MSVCP90.dll', 'msvcr71.dll', "IPHLPAPI.DLL", "NSI.dll", "WINNSI.DLL", "WTSAPI32.dll"],
"includes": ["utils"]
}}
)
Need:
- One .exe file and maybe some .dll (I realy don't know)
Steps what I did:
- setup pip3 and python 3.4 (https://askubuntu.com/questions/524399/issues-with-py2exe)
- setup py2exe for ubuntu "pip3 install py2exe"
- run "python3.4 setup.py py2exe" And got following traceback:
Traceback (most recent call last):
File "setup.py", line 2, in <module>
import py2exe
File "/usr/local/lib/python3.4/dist-packages/py2exe/__init__.py", line 9, in <module>
patch_distutils()
File "/usr/local/lib/python3.4/dist-packages/py2exe/patch_distutils.py", line 68, in patch_distutils
from . import distutils_buildexe
File "/usr/local/lib/python3.4/dist-packages/py2exe/distutils_buildexe.py", line 91, in <module>
from . import runtime
File "/usr/local/lib/python3.4/dist-packages/py2exe/runtime.py", line 3, in <module>
from .dllfinder import Scanner, pydll
File "/usr/local/lib/python3.4/dist-packages/py2exe/dllfinder.py", line 5, in <module>
from . import _wapi
File "/usr/local/lib/python3.4/dist-packages/py2exe/_wapi.py", line 4, in <module>
_kernel32 = WinDLL("kernel32")
NameError: name 'WinDLL' is not defined
- setup pyinstaller for ubuntu (https://github.com/pyinstaller/pyinstaller/wiki)
- run "pyinstaller setup.py"(same as "pyinstaller -w setup.py") and got in dist folder many files with extension .so and one file "setup" without extension
What am I doing wrong?
How can I get .exe file under Ubuntu?
Is it possible?
PS: I've read Python executables: py2exe or PyInstaller? by I didn't find answer.
You cannot use py2exe on Ubuntu or Linux in general. You cannot use it on Mac either. It is a Windows-only utility! You have to use it within Windows, whether that be in a Windows virtual machine or an actual Windows machine.
As for PyInstaller, please read the docs:
Can I use PyInstaller as a cross-compiler?
Can I package Windows binaries while running under Linux?
No, this is not supported. Please use Wine for this, PyInstaller runs fine in Wine. You may also want to have a look at this thread in the mailinglist. In version 1.4 we had build in some support for this, but it showed to work only half. It would require some Windows system on another partition and would only work for pure Python programs. As soon as you want a decent GUI (gtk, qt, wx), you would need to install Windows libraries anyhow. So it's much easier to just use Wine. - source

"ImportError: No module named xlsxwriter" while converting python script to .exe

My script is working fine if I run it as Python from the command line.
I have converted the script to an .exe and am facing an issue with xlswrite. Below is the error output:
Traceback (most recent call last):
File "C:\Python27\lib\site-packages\cx_Freeze\initscripts\Console.py", line 27, in <module>
exec code in m.__dict__
File "loader.py", line 5, in <module>
ImportError: No module named xlsxwriter
I have used this video to create the .exe for my Python script.
How should I fix the import error?
I have only used cx_freeze a few times and was successful using these steps. You were possibly missing something in this. First, create a setup.py like so:
from cx_Freeze import setup, Executable
import sys
exe = Executable(
script="yourmodule.py",
base="Win32GUI",
)
setup(
name = "desiredname",
version = "1",
description = "example program",
executables = [exe]
)
Before running this, make sure that you have all non-default (built-in) modules and the setup.py file in the same folder as the yourmodule.py file. Then from the command line, run python setup.py build.

How to make a python script executable in py2exe?

I downloaded py2exe for python 2.6. I include this bit of code:
from distutils.core import setup
import py2exe
setup(console=['move2.py'])
'move2.py' being my first script. This code is from my second script as said in the video. I opened cmd and typed 'movesetup.py py2exe' and pressed enter. Instead it came up with a error message:
Traceback (most recent call last):
File "C:\Users\User\Desktop\move.py", line 1, in (module)
from distutlis.core import setup
ImportError: No module named distutlis.core
I downloaded py2exe right and I downloaded the right one for Python 2.6. Here is the website that I found how to change .py to .exe. Is he right? I also tried cx_freeze too but that didn't work either. https://www.youtube.com/watch?v=HR483VkOvkE
You have a typo: distutlis
Change to distutils, and read the error next time. ;)

Py2Exe Error: Win32com.client causes errors when trying to run created executable

My script that I'm trying to create into an executable contains the following imports:
import csv, time, BeautifulSoup, sys, mechanize, os, traceback, win32com.client as win32
My setup.py looks like this:
from distutils.core import setup
import py2exe, sys, os
sys.argv.append('py2exe')
setup(
options = {'py2exe': {'bundle_files': 1}},
console = [{'script': "tool.py"}],
zipfile = None,
)
The .exe of my program is created successfully, however when I try to run it I receive the following errors:
Traceback (most recent call last):
File "tool.py", line 1, in <module>
File "zipextimporter.pyc", line 82, in load_module
File "win32com\__init__.pyc", line 5, in <module>
File "zipextimporter.pyc", line 98, in load_module
ImportError: MemoryLoadLibrary failed loading win32api.pyd
Surprisingly, it compiles into an .exe fine if I use bundle_files: = 3 however I need this program to be in one executable, not multiple files. I've read this from the py2exe website but I'm not sure if that's what I need to do/use. If that is the answer I'm looking for, I don't know how to use that or what to do with it. Please explain.
Any help is greatly appreciated!
This was solved by upgrading to Python 2.7. I originally had Python 2.6 installed but the update solved this issue.

Categories