Page 1 of 1

Making an exe file with Pyinstaller

#1 Eric115  Icon User is offline

  • coderさん
  • member icon

Reputation: 63
  • View blog
  • Posts: 696
  • Joined: 19-January 09

Posted 29 September 2010 - 02:58 AM

Hello,
So, you just finish a program you write in Python and you want to share it with friends, family and upload it to the internet. Problem is, a lot of people aren't going to know what to do with a .py or .pyc file. So the solution is to make an exe file. There are several 'programs' that can be used to make a .exe file from a .py file. Some of them include; Pyinstaller, Py2exe, Gui2exe, cx_Freeze just to name a few. For me creating my first .exe file was a big task. I struggled to find a suitable program to do it and didn't know how to use it when I did. Unfortunately for me, py2exe does not work on my computer so I was unable to use it. So I am going to show you how to use pyinstaller to make an .exe file. There is a help guide for using pyinstaller, but for some of the features I found that guide confusing.
Getting Pyinstaller
First of all, you have to download pyinstaller. You can download it from here:.
After downloading the file, you need to unzip it. It should extract to a pyinstaller-1.4 folder. You don't need to install pyinstaller and it doesn't need to go in the usual Python\lib\site-packages. For the sake of this tutorial, I will use pyinstaller as if it was on the desktop.
Preparing your script
Before you make your .py file a .exe file you want to make sure it works. So run the script and make sure it works. For this tutorial I will use the following script for examples:
import sys
print "Hello World!"
sys.exit()


I have purposely imported the sys module for something later. For me, I often use PythonCard for my GUI's. For pyinstaller, you need to add a few imports to your script. So, for example if I make a GUI with a button that says 'Hello' and some static text which says 'Goodbye', I need to have the following in my script for pyinstaller:
from PythonCard.components import button, statictext


This could apply to other GUI's and modules so if you keep getting weird errors where things aren't working, make sure you don't need to do an import thing like shown above. The reason you have to have imports that you usually wouldn't need is because when pyinstaller runs, it imports all the dependencies of your script and in a GUI, all the parts need to be there for them to show up on the GUI.
This mainly applies to PythonCard because there is a separate GUI file but could apply to other modules so I thought I should mention it.
Making an exe file
Ok, so you've made sure your script works so it's time to make the exe file. For this, I am assuming that pyinstaller is on the computer desktop. It doesn't have to be there it just makes it easier. So, open up the command prompt by going start>all programs>accessories or clicking run and typing cmd. For windows 7, you can just type cmd into the start search bar. When the cmd is open, you need to set it to where pyinstaller is. So for the desktop we enter the following:
cd desktop\pyinstaller-1.4
That sets it so it operates from that directory for when you launch the functions of pyinstaller.
Then run Configure.py to set up pyinstaller.
Now, you need to copy your script (.py file) and put in in the pyinstaller directory. If you have a custom module/dependency which isn't in the lib\site-packages, you need to put that with the .py file when you copy it. If you want to have an icon for your program, it has to be a .ico file and it must also be put with the .py file. If you don't use an your own icon, you will get the default icon of a blue butterfly. If you need to convert a picture (.jpg, .gif, .png, .bmp etc) to a .ico you can use the following website:
convert icon
Ok, for this bit, I am going to make a single .exe file with an icon and no command console.
In the cmd, enter in the following:
Makespec.py --icon=icon.ico --onefile --noconsole -nProgramName Program.py
(if you are using the script I have used above as an example, you need to change --noconsole to --console.
It should say something like:
wrote C:\...ProgramName.spec
now run Build.py to build the exe.
So, now we run Build.py to make the exe file. You can run it by entering this:
Build.py ProgramName\ProgramName.spec
It will come up with a whole bunch of stuff about what it is doing in making the exe file.
then it should tell you it appended the .exe file to a directory.
Go to that directory and then go to the folder called dist. In the folder you should have a single .exe file and it should have your icon as the image. Double click it and see if it runs correctly.
One thing you may notice about the .exe file is that it is usually over 1mb big, depending on your script. This is because all of the python launchers and readers need to be packaged into the .exe file. For me on a standard 350 line script (with a GUI) that is about 10kb, after running through pyinstaller it works out to be about 8.4mb which is quite large seeing as all the program does is move some files. You can use upx if you need a smaller .exe file, but I have found it doesn't really help much. It changed my 8.4mb .exe to 8.3mb.
All of the options you enter into the cmd for the Makespec.py are optional arguments. You don't have to have an icon or only a single file. For more information about the arguments you can look here:
Pyinstaller Manual
Some of the options are only available on certain platforms (e.g the --noconsole is windows only) so be careful!

good luck making .exe files!
Happy Coding!

Is This A Good Question/Topic? 0
  • +

Replies To: Making an exe file with Pyinstaller

#2 jegersej  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 27-November 10

Posted 27 November 2010 - 04:12 AM

Dont understand it because when i open configure it just comes with a "batch" window that just fast opens and close again? What's wrong about that?
Was This Post Helpful? 0
  • +
  • -

#3 Eric115  Icon User is offline

  • coderさん
  • member icon

Reputation: 63
  • View blog
  • Posts: 696
  • Joined: 19-January 09

Posted 08 December 2010 - 05:23 PM

When you run configure.py, you need to run it from the command prompt not just by double clicking on it. So when you're in the command prompt, just go to the directory where pyinstaller is (see the tut for more info) and then when you're there, just type in Configure.py and press enter. It should run. It doesn't really matter if you don't do it in the command prompt, it still does the same thing but if you wanted to run it from the command prompt, that is how.
Was This Post Helpful? 0
  • +
  • -

#4 Daaksin  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 02-November 12

Posted 19 May 2013 - 11:38 PM

This is an excellent tutorial! Thank you so much!
I do have one question though.. Forgive me if you previously answered it somewhere but is the EXE that is generated a standalone file? Or will I need to ask my friends to install Python before I give them the EXE?

Thanks!
Was This Post Helpful? 0
  • +
  • -

#5 CyroSplazer  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 13-April 12

Posted 02 June 2013 - 12:38 AM

View PostDaaksin, on 19 May 2013 - 11:38 PM, said:

This is an excellent tutorial! Thank you so much!
I do have one question though.. Forgive me if you previously answered it somewhere but is the EXE that is generated a standalone file? Or will I need to ask my friends to install Python before I give them the EXE?


Hi Daaksin,

From my history of working with Pyinstaller, you do not need python installed to run the exe's it produces. I have found, however, that some computers like to be picky with the exe's and refuse to run it. But it should work fine if your target operating system is the same platform as the system you ran the pyinstaller script on. I have also found that I get good results when I use the --onefile parameter.
Was This Post Helpful? 0
  • +
  • -

#6 pianist1119  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 18-July 13

Posted 18 July 2013 - 10:49 AM

Okay, really sorry if this is kind of obvious or I missed something. How do you package the rsrc in the executable? I usually use --onefile to compile my programs. Is there a way to make the code standalone, or will it run properly with the rsrc file just in the directory?
Was This Post Helpful? 0
  • +
  • -

#7 Eric115  Icon User is offline

  • coderさん
  • member icon

Reputation: 63
  • View blog
  • Posts: 696
  • Joined: 19-January 09

Posted 14 August 2013 - 11:35 PM

View Postpianist1119, on 18 July 2013 - 10:49 AM, said:

Okay, really sorry if this is kind of obvious or I missed something. How do you package the rsrc in the executable? I usually use --onefile to compile my programs. Is there a way to make the code standalone, or will it run properly with the rsrc file just in the directory?

Hi, It sounds like you are using pythoncard for your GUI. To package the rsrc file into your main exe, you need to put the rsrc file into your main code file. To do this, create a variable in your code (before you call your main class with the model.Background) and call it rsrc. Then where you call your class to actually start, you need to change the argument to have a two extra arguments to tell pythoncard that your rsrc is now in your main code like so:
#imports and other stuff

rsrc = #everything copied out of your rsrc file

class main(model.Background) 
    #main stuff

if __name__ == '__main__':
    app = model.Application(main, None, rsrc) #change the arguments here
    app.MainLoop()




Then when you compile/run the program, it will use the variable rsrc instead of the actual .rsrc file.

Hope that helps!
Was This Post Helpful? 0
  • +
  • -

#8 Aldoazra  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 29-August 13

Posted 29 August 2013 - 12:46 PM

Hi,
I have a problem, I've made a program in Python 2.7 which manipulates with word and excel simultaneously, I made it with pywin32 (used win32com.client with Dispatch and constants) it has some GUI made in wxPython, the program runs perfectly when it is started in Python Shell (no errors) then I tried to make an .exe from that program, I used pyinstaller 2.0, py2exe and cx_freeze and all of them signal the same error AttributeError: wdReplaceAll (when started from .exe, I haven't had that error when started in Python Shell).
All suggestions are more than welcome! Thanks in advance :D

P.S. I use office 2007 32-bit, the program is intended to go on other computers that don't have Python and use 32-bit windows.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1