⬆️ ⬇️

Little about py2exe

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" ]}} )
  1. Copy Source | Copy HTML from distutils .core import setup import py2exe setup( windows=[{ "script" : "main.py" }], options={ "py2exe" : { "includes" :[ "sip" ]}} )
  2. Copy Source | Copy HTML from distutils .core import setup import py2exe setup( windows=[{ "script" : "main.py" }], options={ "py2exe" : { "includes" :[ "sip" ]}} )
  3. Copy Source | Copy HTML from distutils .core import setup import py2exe setup( windows=[{ "script" : "main.py" }], options={ "py2exe" : { "includes" :[ "sip" ]}} )
  4. Copy Source | Copy HTML from distutils .core import setup import py2exe setup( windows=[{ "script" : "main.py" }], options={ "py2exe" : { "includes" :[ "sip" ]}} )
  5. Copy Source | Copy HTML from distutils .core import setup import py2exe setup( windows=[{ "script" : "main.py" }], options={ "py2exe" : { "includes" :[ "sip" ]}} )
  6. 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:



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
  1. import os, sys
  2. def module_path ():
  3. if hasattr (sys, "frozen" ):
  4. return os .path.dirname (
  5. unicode ( sys .executable, sys .getfilesystemencoding ())
  6. )
  7. 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)



  1. 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.
  2. Remove unicodedata.pyd, bz2.pyd, select.pyd, w9xpopen.exe. Weights are few, but at least there will be fewer files in the project.
  3. 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)
  4. 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 :)

Source: https://habr.com/ru/post/87224/



All Articles