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
Related
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.
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.
This question already has answers here:
Create a single executable from a Python project [closed]
(3 answers)
Closed 3 years ago.
I wrote a script that will help a Windows user in her daily life. I want to simply send her the .exe and not ask her to install python, dlls or have to deal with any additional files.
I've read plenty of the stackoverflow entries regarding compiling Python scripts into executable files. I am a bit confused as there are many options but some seem dated (no updates since 2008) and none were simple enough for me not to be asking this right now after a few hours spent on this.
I'm hoping there's a better, up-to-date way to do this.
I looked into:
pylunch
py2exe
cx_Freeze
py2app (only for Mac)
pyinstaller
bbfreeze
but either I couldn't get them to work or couldn't understand how to get the result I need. The closest I got was with py2exe but it still gave me the MSVCR71.dll
I would appreciate a step-by-step answer as I was also unable to follow some of the tweaking answers here that require some prior understanding of how to use py2exe or some of the other tools.
I'm using Python 2.5 as one of the modules is only available for that version.
PyInstaller will create a single-file executable if you use the --onefile option (though what it actually does is extracts then runs itself).
There's a simple PyInstaller tutorial here. If you have any questions about using it, please post them...
Using py2exe, include this in your setup.py:
from distutils.core import setup
import py2exe, sys, os
sys.argv.append('py2exe')
setup(
options = {'py2exe': {'bundle_files': 1}},
windows = [{'script': "YourScript.py"}],
zipfile = None,
)
then you can run it through command prompt / Idle, both works for me. Hope it helps
i would recommend going to http://sourceforge.net/projects/py2exe/files/latest/download?source=files to download py2exe. Then make a python file named setup.py.
Inside it, type
from distutils.core import setup
import py2exe
setup(console=['nameoffile.py'])
Save in your user folder
Also save the file you want converted in that same folder
Run window's command prompt
type in setup.py install py2exe
It should print many lines of code...
Next, open the dist folder.
Run the exe file.
If there are needed files for the program to work, move them to the folder
Copy/Send the dist folder to person.
Optional: Change the name of the dist folder
Hope it works!:)
I would join #Nicholas in recommending PyInstaller (with the --onefile flag), but be warned: do not use the "latest release", PyInstaller 1.3 -- it's years old. Use the "pre-release" 1.4, download it here -- or even better the code from the svn repo -- install SVN and run svn co http://svn.pyinstaller.org/trunk pyinstaller.
As #Nicholas implies, dynamic libraries cannot be run from the same file as the rest of the executable -- but fortunately they can be packed together with all the rest in a "self-unpacking" executable that will unpack itself into some temporary directory as needed; PyInstaller does a good job at this (and at many other things -- py2exe is more popular, but pyinstaller in my opinion is preferable in all other respects).
1) Get py2exe from here, according to your Python version.
2) Make a file called "setup.py" in the same folder as the script you want to convert, having the following code:
from distutils.core import setup
import py2exe
setup(console=['myscript.py']) #change 'myscript' to your script
3) Go to command prompt, navigate to that folder, and type:
python setup.py py2exe
4) It will generate a "dist" folder in the same folder as the script. This folder contains the .exe file.
you may want to see if your app can run under IronPython. If so, you can compile it to an exe
http://www.codeplex.com/IronPython
You can create executable from python script using NSIS (Nullsoft scriptable install system). Follow the below steps to convert your python files to executable.
Download and install NSIS in your system.
Compress the folder in the .zip file that you want to export into the executable.
Start NSIS and select Installer based on ZIP file. Find and provide a path to your compressed file.
Provide your Installer Name and Default Folder path and click on Generate to generate your exe file.
Once its done you can click on Test to test executable or Close to complete the process.
The executable generated can be installed on the system and can be distributed to use this application without even worrying about installing the required python and its packages.
For a video tutorial follow: How to Convert any Python File to .EXE
You could create an installer for you EXE file by:
1. Press WinKey + R
2. Type "iexpress" (without quotes) into the run window
3. Complete the wizard for creating the installation program.
4. Distribute the completed EXE.
I'm trying to convert my .PY script into a .EXE file, in the process I have tried PY2EXE with the following command line..
python C:\Users\Noob\Desktop\setup.py py2exe -p ftplib
-> -p ftplib is for my module import.
When I use this code, I'm left with an .EXE and a bunch of files, if I take the .EXE out then it will come up with an error. It is required to have "_socket.pyd" and "python26.dll" or it will crash (the program). How do I compile this (not having to use PY2EXE or having to use it) without these extra files?
Thanks a lot!
You should not be taking the exe out of your folder and executing. Just execute it from the dist directory where got created, it has the python26.dll which the executable needs. If you want to ship it, zip the dist directory and ship it. Otherwise you have to create an installer using specific installer software like NSIS.
I hope you took a look at the Python2.6 specific section, which has details on how to write data_files to include msvcr90.dll
Also, I am finding this -p ftplib cmd option pretty new. Usually all things to do are within setup.py. Can you point out where this kind of option specification is mentioned.
It is possible to make single file executables with py2exe, the setup.pyfile would look something like this:
from distutils.core import setup
import py2exe
setup(
console=['yourscript.py'], # list of scripts to convert into console exes
zipfile=None, # library files in exe instead of zipfile (defaults to "library.zip")
options = {
'py2exe': {
'bundle_files': 1, # bundle dlls and python interpreter in the exe
}
}
)
See py2exe's list of options for more info. Also have a look at the "What are all those files?" question from the FAQ.
You might also want to have a look at PyInstaller (which seems to be doing a better job of creating single file exe's with Microsoft MSVCR*.DLL files included if you need those) Be sure to use the 1.5 series for python 2.6 on Windows.
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.