473,395 Members | 1,885 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes and contribute your articles to a community of 473,395 developers and data experts.

Simple guide to using PyInstaller

true911m
This is a simple walkthrough to get PyInstaller up and running.

I decided to give PI a try, because it claims to be more selective about what it bundles into its executable files by default, and it also integrates UPX (Ultimate Packer for eXecutables) into the build process, if you have it installed. It also claims functionality on linux, as a bonus (I didn't test this). I highly recommend the UPX options, and will cover how to get it working on Windows.

At the time of this writing, the latest stable version of PyInstaller is 1.3, and UPX is 2.03.

The result here will be to convert a simple python app into a single .exe file that can be copied and run on any Windows XP machine. It may work on many other Windows platforms (and linux), but I haven't tested it.

You'll need a working Python installation before you continue. PyInstaller claims compatibility with versions 1.5 through 2.4. I'm running 2.5, and it's working just fine.

You can download PyInstaller from SourceForge at http://www.pyinstaller.org/.

PyInstaller does not install under Python's site-packages directory as most libraries do. Instead, you can put the contents of its directory (what you see when you unzip the download file) wherever you wish, but remember that you will either have to type its location or alter your system paths for the build process to work properly, so try to keep it relatively short. I haven't even tried the paths option, so if you try it and it works, drop a note here. My install location is c:\python25\pyinstaller13, which means I took all those files from the archive folder and simply copied them to the install folder. Make a note of this path, because you'll have to substitute yours if it's different.

PyInstaller needs to be configured. Since it's compatible with all those Python versions, it needs to know which one it's working with. It's simple, though -- open the directory where you just copied those files, and double-click on Configure.py. PI will scan your Python install dirs, and make a config.dat file that customizes it for your installation.

Next, go ahead and download UPX from http://upx.sourceforge.net/#download . (I'm using the Win32 Console package). For the purposes of PyInstaller, you only need one file, upx.exe, placed in your system path. After unpacking the archive, copy upx.exe to your c:\pythonXX directory. That's all there is to it.

I'm starting with a basic Hello World app that I used in my prior walkthrough:

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/env python 
  2. for i in xrange(10000): 
  3.     print "Hello, World!"
Create this with your favorite editor and save it as HelloWorld.py. Next, create a batch file to run the build process. Remember that path where you copied PyInstall? You'll need that here, on the first line below. Back to your editor to type the following:

Expand|Select|Wrap|Line Numbers
  1. set PIP=c:\python25\pyInstaller13\
  2. python %PIP%Makespec.py --onefile --console --upx --tk HelloWorld.py
  3. python %PIP%Build.py HelloWorld.spec
You can use whatever naming convention you wish, but because this may vary for each project, I'm saving it as HelloWorld.bat, right next to my HelloWorld.py file. See the notes at the end for the basics regarding those command-line options.

You should now have two files, HelloWorld.py and HelloWorld.bat, both in the same directory.

Double-click on HelloWorld.bat and sit back. Depending on your hardware, it could take from a couple seconds to a minute to complete. It seems to take a bit longer than the comparable py2exe build I described recently, and the UPX compression usually seems to add the most time, but this example should be pretty quick.

When the cmd window finishes, check the directory where you stored the two files. You should now see a new directory (buildHelloWorld) and a few new files (HelloWorld.spec, warnHelloWorld.txt, and HelloWorld.exe). The build directory and the .spec file were used by PyInstaller, and can be deleted. the warnXXX.txt file contains warnings; it seems these are always here, but so far they don't affect the program at all. And the .exe file is ready to go.

If you saw my py2exe walkthrough, you may remember that HelloWorld.exe topped out at 3.2MB from my 110-byte source file. This one weighs in at 1.5MB, a considerable improvement. In larger projects, where external .dlls and other resources are bundled into your project, the differences should be even more significant.

Go ahead, double-click on HelloWorld.exe, I know you want to...

Copy HelloWorld.exe to another computer (without Python on it), and run it there, just to see that it really works.

A few notes:

Just like py2exe, PyInstaller bundled a python interpreter, any required DLLs, and any referenced libraries in its output file, turning 100 bytes into 1.5MB. However, this is much smaller than the 3.2MB results from py2exe.

When you create HelloWorld.bat, above, you want to make a couple of simple choices based on each program you're 'compiling'. Here's the quick summary:
Expand|Select|Wrap|Line Numbers
  1.  
  2.  
  3.  
  4.     --tk        This is required for inclusion of the tcl/Tk
  5.             libraries.  Unlike py2exe, PyInstaller doesn't 
  6.             recognized that this is used and include it
  7.             automatically.  However, you can leave it in
  8.             without doing any harm (as we did in this
  9.             example).  It adds some overhead, but the size
  10.             seems to vary.
  11.  
  12.     --upx        I mentioned that this is a great option, and 
  13.             it is, but it's really slow, especially as your
  14.             source file gets bigger.  It's a great option
  15.             for your final compile before distributing, but
  16.             you might save a lot of time if you turn it off
  17.             until then.
  18.  
  19.     --console    This is the DOS-like box, and carries the same
  20.     --noconsole    significance as the python vs. pythonw usage 
  21.             for your other programs.  If your program runs
  22.             in Windows and you've finished troubleshooting,
  23.             you probably want --noconsole.
  24.  
More info is available at www.pyinstaller.org, including some not-too-lengthy documentation pages.

Cheers,

- Mark
Dec 21 '06 #1
10 110111
bartonc
6,596 Expert 4TB
I like your style, Mark. This is great info and I can see that you have gone to some trouble to get it to our members. I also see that you are now linking threads. This is awesome member partisipation! Thank you so much.
I made this one sticky, so here is a link back to the py2exe thread.
Dec 21 '06 #2
How can I pack a more complicated structure of files ? any project have several directories - how can I manage a Tree of files ?

thanks ..
Jan 23 '08 #3
Excellent guide Mark! Really easy to follow and it actually worked. You saved me a lot of time in a project I'm working on. Thanks very much!

Since you obviously have a skill with explaining complex topics clearly, I hope you'll consider posting a few more guides.
Jul 21 '08 #4
Hey, I've been trying to get pyInstaller to work but i'm getting the following problems.
Can someone help me out with this?


(warnHelloWorld.txt)
W: no module named posix (conditional import by os)
W: no module named pwd (delayed, conditional import by posixpath)
W: no module named win32api (delayed import by iu)
W: no module named posix (delayed, conditional import by iu)
W: no module named org (top-level import by copy)
W: no module named _emx_link (conditional import by os)
W: delayed __import__ hack detected at line 0 - encodings (C:\Python25\lib\encodings\__init__.pyc)
W: delayed eval hack detected at line 0 - os (C:\Python25\lib\os.pyc)
W: delayed conditional exec statement detected at line 0 - iu (c:\python25\pyInstaller1.3\iu.pyc)
W: delayed conditional exec statement detected at line 0 - iu (c:\python25\pyInstaller1.3\iu.pyc)
Jul 22 '08 #5
The instructions are great but they did not work for me. Here is what I see when I run the HelloWorld.bat file:

c:\Python25\Scripts>python c:\python25\pyInstaller13\Build.py HelloWorld.spec 'python' is not recognized as an internal or external command, operable program or batch file

I get the "'python' is not recognized..." note when I enter the word 'python' before any .py script.

Any ideas?
Don
Nov 16 '08 #6
@dbpeckham
1 reason, 2 solutions:

1. the directory where python.exe resides, is not part of your PATH variable

1. add the directory where python.exe resides, to your PATH environment variable
2. prefix "python" in the batch file with the complete path

That should solve your issue.

Best rgds,
--Geert
Feb 12 '09 #7
I gave up on pyInstaller several months ago but I'm trying again.

I copied the upx.exe file to c:/python25/

I ran c:/python25/pyInstaller-1.3/Configure.py and got the following response:

I: Finding TCL/TK...
I: found TCL/TK version 8.4
I: testing for Zlib...
I: ... Zlib available
I: Testing for ability to set icons, version resources...
I: ... resource update available
I: Testing for Unicode support...
I: ... Unicode available
I: testing for UPX...
I: ...UPX unavailable
I: computing PYZ dependencies...

I ran HelloWorld.bat and got the following response:

..."You must run Configure.py before building!"
SyntaxError: invalid syntax

Any suggestions?
Jun 17 '09 #8
PS: I am using Windows Vista.
dbpeckham
Jun 17 '09 #9
Hi,


I'm also very interested having more information on how to compile a more structures projects with different directories with lib imported in the higher script and empty __init__.py files

Could you help ?
Apr 20 '11 #10
Hello,
I did everything you said, when i clicked on the .bat file, NOTHING happened. Am I missing something?
Thanks for your answer and best regards.
Dec 27 '15 #11

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Frank Millman | last post by:
Hi all From time to time there is a request for a simple way of storing and accessing data without using a full SQL database. I had this requirement recently, and I was pleasantly surprised by...
0
by: Joh | last post by:
hello, i've readed "Charming Python: Generator-based state machines" www-106.ibm.com/developerworks/ linux/library/l-pygen.html , but i still can not get the point on how using generator for FSM...
6
by: hoover_richard | last post by:
I am a newbie to C++ and I need help with a simple program I am trying to write. My program is designed to print all of the odd integers contained in an array and output the sum of the odd...
7
by: bfowlkes | last post by:
Hi, I am new to the C programming language and programming in general. I am writing a simple roulette program. I have everything working so far but it seems a little plain. What I would like to...
1
by: kbperry | last post by:
Hi all, I am pretty confused on this? While pyinstaller has a "MakeCOMServer.py" thing, it seems to blow up every time I run it. It keeps complaining that a dictionary that I have has...
4
true911m
by: true911m | last post by:
Here's a little walkthrough to get py2exe up and running. I'm not an expert, so I can't help much with any problems you might have. This is what worked for me. The result here will be to convert...
1
by: psbasha | last post by:
Hi, Is it possible to create a simple viewer using Tkinter Modueles?. If not ,is it possible thru Python and OpenGL or anyother ways of creating a simple viewer?. I would like to have the...
5
by: SM | last post by:
Hello I have simple question using simpleXML and PHP. i have an xml file that looks like this: <?xml version="1.0" encoding="UTF-8"?> <discography version="0.01"> <CD>...
0
by: asoft | last post by:
Please i need help with this. I just started using python and i hace run into several issues. The on that i can't tackle is the issue of no being able to compile the python code using PyQt4...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.