There is such an application. Called py2exe. It allows you to pack, convert a python program to an exe file (well, more precisely, exe and a handful of others). Why is it necessary? Well, not all windows users have a python interpreter installed with the right libraries. But the packaged program should ideally run on any windows-machine.
Installation
Unfortunately, py2exe does not support the third version of python.
Download py2exe at
SourceForge .
If you have python (and you probably should), installation problems should not arise. Put in the python directory.
Conversion
Now the fun begins. In the directory where your python program is located, you need to create a file setup.py with the following content
Copy Source | Copy HTML from distutils .core import setup import py2exe setup( windows=[{ "script" : "main.py" }], options={ "py2exe" : { "includes" :[ "sip" ]}} )
Copy Source | Copy HTML from distutils .core import setup import py2exe setup( windows=[{ "script" : "main.py" }], options={ "py2exe" : { "includes" :[ "sip" ]}} )
Copy Source | Copy HTML from distutils .core import setup import py2exe setup( windows=[{ "script" : "main.py" }], options={ "py2exe" : { "includes" :[ "sip" ]}} )
Copy Source | Copy HTML from distutils .core import setup import py2exe setup( windows=[{ "script" : "main.py" }], options={ "py2exe" : { "includes" :[ "sip" ]}} )
Copy Source | Copy HTML from distutils .core import setup import py2exe setup( windows=[{ "script" : "main.py" }], options={ "py2exe" : { "includes" :[ "sip" ]}} )
Copy Source | Copy HTML from distutils .core import setup import py2exe setup( windows=[{ "script" : "main.py" }], options={ "py2exe" : { "includes" :[ "sip" ]}} )
Copy Source | Copy HTML from distutils .core import setup import py2exe setup( windows=[{ "script" : "main.py" }], options={ "py2exe" : { "includes" :[ "sip" ]}} )
Where main.py is the name of your script.
')
Next, run the package with the command:
setup.py py2exe
Yes Yes exactly.
We look that we did. Two folders.
build - service, you can immediately demolish.
dist - in fact, it is our program.
As mentioned above, this is not one file:
- main.exe - program
- pythonXX.dll - python interpreter
- library.zip - archive with compiled sources (of all, except for the actual program, as I understand it)
- .pyd - python modules that the program imports
- .dll - libraries that appeared necessary
- and more files on the little things below will be said more
Is it really that simple? Not true;)
Difficulties
Most likely there will be some problems.
For example, file paths. Do not use relative paths. They do not know where. It is better to use absolute ones.
How to recognize him? On the Internet, there is a solution, the module_path function.
Copy Source | Copy HTML
- import os, sys
- def module_path ():
- if hasattr (sys, "frozen" ):
- return os .path.dirname (
- unicode ( sys .executable, sys .getfilesystemencoding ())
- )
- return os .path.dirname ( unicode (__file__, sys .getfilesystemencoding ()))
Or the application flatly refuses to start (perhaps not from you, but from someone else). Due to the lack of libraries
Visual Studio .
As a solution to the problem, you can install them on the computer (but this is not our method) or throw the dll and the manifest file into the program folder.
msvcr90.dll and Microsoft.VC90.CRT.manifest
(I do not know how it is licensed and I will not post it)
Where to get them? For me, the easiest thing was to reinstall python (everything else was left in place) in the “only for me” mode. And the required files were in the folder with python.
The purpose of the topic was not to reveal all the features of py2exe.
Here is the tutorial, and
here are some tips and tricks.
The size
Due to some features, the application can get a terrifying size. But this can and must be fought. Ideas prompted
kAIST (well, except for upx'a = p)
- The most effective. Compress libraries upx'om . Console application. It works elementary. The file is transferred to the input, it compresses it. For my game Reversi size decreased by ~ 3 times.
- Remove unicodedata.pyd, bz2.pyd, select.pyd, w9xpopen.exe. Weights are few, but at least there will be fewer files in the project.
- If you specify the optimize: 2 option in setup.py, then the modules will be compiled into .pyo (python optimized code), and not in .pyc (python compiler script). It does not give much effect, but who knows, maybe you are lucky)
- And finally, you can clean up the library.zip from unused modules and encodings. Just tidy.
Conclusion
Well, it seems that's all. We achieved what we wanted. Yes, the application turned out to be a solid size (this is not C ++ for you, for example), but on our favorite Python :)