How to create an executable for a python program that installs the used libraries?

6

Hello, I have a code that I did in python and uses some libraries. I would like to know if it is possible to make an executable for this program and the user who uses it does not need to have python or these libraries installed.

    
asked by anonymous 16.04.2017 / 00:32

2 answers

10

It looks like you want to compilar your source code into an executable.

If your code was written to python 2.x ou 3.0-3.1 , you can use, for example, the library py2exe .

To create the executable, you should:

  • install py2exe ( pip install py2exe )

  • Have the code to be compiled saved on the computer (for example, code hello.py )

  • Create a file named setup.py in the same folder as the code to be compiled, with the following content:

    from distutils.core import setup
    import py2exe
    
    setup(console=['hello.py'])
    
  • Navigate the terminal to the folder with the files ( hello.py and setup.py ) and run the command python setup.py install

  • Ready. The folder should have been created 2 new folders. In one of them you will find your .exe .

  • NOTE:

    • 1) You can see in the more parameters documentation to add at compile time ;

    • 2) If your code was written to Python 2.6, 2.7, 3.0 ou 3.1 , you should be aware that py2exe does not automatically include DLL MSVCR90.dll in dist , so you should put it there manually.

    If your code was written to python 2.7 ou 3.3—3.5 , you can use, for example, the library PyInstaller .

    To create the executable, you should:

  • Install package pyinstaller ( pip install pyinstaller )

  • To create the .exe, navigate through the terminal to the folder with the code to compile and type:

    pyinstaller --onefile script.py
    
  • Ready.

  • 16.04.2017 / 12:05
    4

    You can also use cx_freeze to "package" your program and generate an executable.

    I and a friend created a graphical version for the compiler sometime ago using the Tkinter library, which you can find at my Github .

        
    24.04.2017 / 22:18