Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make an executable file in Python?

I want to make an executable file (.exe) of my Python application.

I want to know how to do it but have this in mind: I use a C++ DLL!

Do I have to put the DLL along side with the .exe or is there some other way?

like image 868
aF. Avatar asked Apr 25 '10 20:04

aF.


Video Answer


2 Answers

py2exe can generate single file executables. see this link for examples.

The setup.py I use uses the following combination of options:

'compressed': 1,
 'optimize':2,
 'bundle_files': 1

I usually add external dlls (p.e. msvcr71.dll) in the same folder where the executable is.

To facilitate distribution and to automate installation after generating your exe you can use Inno Setup to create an installer that puts your exe and your dlls, and other documents (readme, etc) into the application directory

like image 125
joaquin Avatar answered Oct 17 '22 00:10

joaquin


Follow These Steps: You can find this documentation in this site.

1) Install PyInstaller:

Assuming you have PIP installed in this directory c:\PythonXX\Scripts if not go to this site and see instructions on how to "Install Python Indexing Project (PIP)". Go to your command prompt and type the following command.

  • cd c:\Python27\Scripts to go to this directory press Enter
  • Then type -> pip install pyinstaller press Enter again
  • This should install PyInstaller successfully!
  • Obs: You don't need to have pyinstaller inside the Scripts folder after installation this was done because we needed PIP to install the pyinstaller files. To make things more organized I usually move the pyinstaller files to another folder. To move the pysintaller files go to the Scripts folder where the pyinstaller files are located and look for all the files that start with "pyi" you can also look at the Modified Date column to see which files where created at the same time. It should be 24 pyi files that where created when you ran the script above. Copy those files into another folder of your choice I usually create a folder within the C:\ drive and call it "CreateExecutables" with another folder within called "pyinstaller_files".

2) Install PyWin32:

Go to this SITE and look for the executable named pywin32-218.win32-py2.7.exe 2.7 which is for 32 bit systems for python27 look for the one that corresponds to you. Run the executable file and it should install PyWin32 successfully which works hand in hand with pyinstaller!

3) Create Single Executable file

Now that you have PyInstaller and PyWin32 you can start to create the single executable file. The single executable file will be created inside another folder called “dist” this folder is also created dynamically when you run the pyinstaller command. To make things clear your pyinstaller files where copied by you to another folder probably called "pyinstaller_files" folder so now type the following command in your command prompt.

  • c:\python27\CreateExecutables\pyinstaller_files>pyinstaller --onefile c:\test\test1\yourscript.py
  • Obs: The directory of yourscript.py is where you have your pydev module located.

4) Overview

The above command will create a folder called “dist” inside the pyinstaller_files folder this folder will contain your single executable file “yourscript.exe”. You can now move the single executable file somewhere of your choice and delete the “dist” & “build” folder with the “yourscript.spec” file as they are no longer needed to run your single executable file.

like image 41
Developer Avatar answered Oct 17 '22 01:10

Developer