py2exe and setting up setup.py to get its target from argv? - python

I'm trying to create an executable from a .py file, and if I do things exactly as the py2exe tutorial says, it works. I put setup(console=["thingyIdLikeToDo.py"]) in my setup.py file, then type python setup.py py2exe into my console, and it works.
BUT. This means that whenever I want to make another python file into an executable, I have to go open and edit setup.py. And I'd rather do something else:
from distutils.core import setup
from sys import argv
import py2exe
setup(console=[argv[1]])
##This was originally setup(console=["MyTargetFile.py"]) and it DOES work that way
And then type in python setup.py MyTargetFile.py py2exe. On account of it being invalid command name 'MyTargetFile.py'
I've also tried it by changing the order, making it python setup.py py2exe MyTargetFile.py and changing the argv[1] to argv[2]. I get the exact same error message.
I mean, I DO have a functioning way to make my .py files into .exe files, but I'm really annoyed that something that seems like it ought to be such a simple change is't working. What am I missing here?

You're running into trouble because setup is using sys.argv. If you change your call to setup(console = [sys.argv.pop(1)] I believe you will stop stepping on distutils' toes and things should go smoothly.

Related

Creating an executable file with cx_Freeze. How do I allow user input?

Really struggling to get a user input when I run an exe file created by cx_Freeze. If I create a script that requires no input, it runs fine. A black window appears for a second then disappears, and script runs in the background.
The problem is, if user input is required there is no console window to type in
My setup script looks like this:
from cx_Freeze import setup, Executable
setup( name = "SpecialApp",
version= "0.1",
description="Performs useful things",
executables = [Executable("specialapp.py")])
I've tried Base="Console" and Base="Win32GUI" but neither make a difference.
Another thing, this may need its own question, but does anyone know how to run the Exe file without requiring the .dll files to be present? I don't want to give my end users a huge folder full of files, I'd like to give them just the exe file or a bat file if possible.
1) Try adding base = Win32GUI in executables = [Executable("specialapp.py")]) so it looks like:
[Executable("specialapp.py",base = None)])
Note that Win32GUI hides the console.
If that still does not work try running from command prompt (or your OS equivalent) and see if there are any error messages (just because it compiles and starts without errors it does not mean there are any).
2) For single file exe I suggest you go for Pyinstaller or build into an installer with bdist_msi command you could also try IExpress.
3) The DLLs provide the Python runtime libraries so realistically no not with Cx_Freeze.
Hope I helped

Py2Exe won't successfully compile the pygame.font module on Python 3

I made a program using the pygame module on Python 3 and it works fine within python, but when I try to compile is using py2exe it won't run. (I just get the programName.exe has stopped working error upon trying to run it).
I managed to narrow down this problem to the pygame.font module as when I comment all the lines that use that module everything works fine. I tried to forcefully include the module using the -i flag in py2exe, but it doesn't appear to change anything...
What am I doing terribly wrong?
Edit: I managed to get the reason of the program not working - it crashes as it can not find build\executable.exe\pygame\freesansbold.ttf . What I don't understand is why the hell is the pygame folder supposed to be located in a folder with the name of my executable? (Of course, I can not create a folder with the same name as an existing file in the directory). If anyone has a clue to how to fix it, please help!!
I had the same problem using cx_Freeze, so hopefully this will work for you as well. Open up your pygame package folder. It should be C:\Python34\Lib\site-packages\pygame. There should be a True Type Font File titled freesansbold.ttf. Copy that file then open the folder containing your exe program. There should be a zipped file called library. Open it up and go to the pygame folder inside the zipped file. Should look something like this \build\exe.win32-3.4\library.zip\pygame. And just paste the freesansbold.ttf file in that folder and it should work perfectly.
I managed to find a way! By including -l library.zip argument in the build_exe command and then following the instructions given by DeliriousSyntax in the answer above I managed to get it to work!

Converting .py to .exe with a GUI frontend

I'm trying to convert .py to .exe , but I'm not able to convert it with the help of py2exe in the command line.
I searched on the internet about a py2exe with a GUI frontend and I got the results as:
GUI2EXE (3/5) (The best one I found, but the .exe comes with lots of .dll files and the .exe file is buggy and doesn't work properly.)
H-two-O (2/5) (Waste of time. Doesn't compile any .exe files associated with Tkinter. Very creative and useful for other file formats.)
PytoEXE (1.3/5) (Just as H-two-O , but doesn't compile Tkinter files to .exe)
GP2EXE (?/5) (I didn't try it out. Maybe you can give a view on it.)
PyBuilder (2.7/5) (Reliable, good GUI interface with options but lacks some of the features and compiling speed to that of GUI2EXE.)
PythontoEXE (1.3/5) (Same as PytoEXE)
But these weren't good. I need a compiler better than all of the compilers listed above which can compile Tkinter files to .exe without any bugs.
Here's how I use py2exe. I know this isn't what you're asking for, but according to my experience, it's really annoying until it works. So please hear me out.
Assuming your Python file is called main.py.
New file setup.py (same folder):
from distutils.core import setup
import py2exe
setup(console=['main.py'])
From here, you can create a .bat file in the same folder, or run it from the command line. Either way, you'll be running python setup.py py2exe to compile the code.

Using py2exe, how to create an executable usng python and tkinter?

I'm trying to turn a program that I've written in Python 2.7 (using tkinter for the GUI) into a standalone executable using py2exe. I've written the following script in a file called setup.py:
from distutils.core import setup
import py2exe
setup(data_files=['C:\Python27\tcl\tcl8.5\init.tcl'] , windows = ["Brand_Counter.py"])
When I run this, the command prompt opens for a second, then nothing happens. As far as I can tell, according to the documentation my code should create a subdirectory "dist" which will contain my executable, but this isn't happening. Does anybody see anything wrong with my code?
To do a build of a py2exe project, you should issue this command from the directory that contains your setup.py file:
python setup.py py2exe
Works well for me.
EDIT ---
In addition you're using the data_files parameter which takes a list of tuples. Your parameter should look something like this:
data_files=[('tclfiles', ['C:\Python27\tcl\tcl8.5\init.tcl'])]
Adjusted for wherever you want to place your init.tcl file. For more examples see this link: py2exe data_files

Problems with Tkinter in py2exe

I made a a simple GUI program in python with tkinter and attempted to convert it to an .exe using py2exe. However, I've run into a problem. When I try to run the exe it flashes an error very quickly then disapears. So the best I could do was take a screan shot of the error.
How do I go about fixing this?
Edit
Velociraptors, this is my setup file. It's about as basic as it can be. How would I go about integrating init.tcl into the code?
from distutils.core import setup
import py2exe
setup(console=[r'C:\Python26\Random Password Generator.py'])
Does your setup.py script include init.tcl in the data_files option? The py2exe list of options says that's how you should include images and other required data files.
Edit:
Your setup script specifies that your program should be converted to a console exe. If you want a GUI program (which you do, since you're using Tkinter), you need to use the windows option:
setup(windows=[r'C:\Python26\Random Password Generator.py'])
Py2exe should correctly include Tkinter's dependencies. If not, you can manually include init.tcl:
setup(data_files=['C:\Python26\tcl\tcl8.5\init.tcl'],
windows=[r'C:\Python26\Random Password Generator.py'])
Ensure that tcl is installed in C:\Users\splotchy\lib\tcl8.5 or C:\Users\lib\tcl8.5.
If you want to see the error messages for longer, run your program from a command prompt.
I found a bug on the virutalenv site which suggested the following https://github.com/pypa/virtualenv/issues/93
for windows in your directory "C:\Environments\VirtualEnv\Scripts\activate.bat" just add which are set to the right path to TCL and TK for your python version
set "TCL_LIBRARY=C:\Python27\tcl\tcl8.5"
set "TK_LIBRARY=C:\Python27\tcl\tk8.5"
restart your cmd or shell
I believe that the TCL location have changed from there default ones.

Categories