How to create an executable from python

Asked

Viewed 12,189 times

8

I’m doing a service where I have to do a program that reads and creates files with numerical data. The problem I have is that the computers on which the program will be used are not accessible to me.

Because of this, I needed to convert my file .py for .exe The program is ready in Python 3.5 and it seems that the only program that can help make the conversion is the CX_FREEZE. I used it and formed a file...

I have 2 problems:

The file does not work on computers that are windows 7 (depending on the version) and that do not have some dlls (they are not always the same)

The Executable is coming accompanied by SEVERAL folders with MANY files. This doesn’t seem feasible, wanted some way to "package" the files inside my executable.

The program is not so big, and does not use so many libraries, only OS and DATETIME.

I have tried using Pyinstaller and INNO setup, but none gives me any light.

I wonder if anyone has any alternatives or tips that I can use.

import sys
from cx_Freeze import setup, Executable


build_exe_options = {"packages": ["os", "datetime"], "excludes": []}

base = None
if sys.platform == "win32":
    base = "Console"  # para execuções em terminal

setup(name="GetSpecJoin",
      version="0.1",
      description="My GUI application!",
      options={"build_exe": build_exe_options},
      executables=[Executable("240117.py", base=base)])
  • The additional files are the dependencies (Python and libraries used, including perhaps cx_freeze itself). Sending multiple files should not be an impediment for you. Is there that question on Soen with answers that may be useful to you. But I didn’t test, I don’t know if it works what they say there. Anyway, if you really want to have one file and do not want to request the installation of Python in the client, maybe it is better to simply use another language (C, for example).

  • I understand that this would be the most guaranteed alternative. If I don’t get an answer in a few days I will have to do this. The fact is that the client does not insist that python is not installed, he even accepted, but I find it strange to be so complicated to do something as simple as compiling the file for distribution. The idea of making a singular . exe is that it can be at least a little portable.

  • It’s not that it’s complicated. You don’t want to send dependency Dlls together. And if it didn’t run on Windows 7 it may be because there are some missing Dlls that your version of the OS already has (it is common to occur with the Visual Studio 2015 Redistributables, for example, that some Python libraries depend on). It would only be the case to install, if it is the ones that are missing. Anyway, it would certainly be easier to have only one exe, but then the most uncomplicated would be to use an appropriate language.

  • I tried to insert the dll manually, but it seems that the files being required (api-win-msi-crt-[...].dll) do not work in the version of windows 7 installed (without sp1) in other versions with Sp2 the program worked. When trying to upgrade the system to sp1, the computer is loading for hours and shows no progress. until the microsoft Hotfix that was supposed to fix the error did not work. The biggest problem is being the client machines with outdated system, The system is "locked" in windows 7 without even sp1.

  • 1

    It’s just that these packaging tools just pick up the Dlls in use in your system. That’s why it doesn’t work on the target system if it’s different. To solve, only if you have a similar system (that is, in the same version of Windows) and package there too. Meanwhile, consider also using Cython: http://cython.org/ to convert your Python code to C and then compile the C code into an executable. Good luck!

  • Thank you very much! I will check. one last try. :)

  • I am not a professional, but I ask for permission to tell you what I did when I played to create . exe from python programs. I downloaded the Python installation files, if I am not mistaken a minimal version and put together with the files of my program, bundled everything using INNO and configuring it to install Python too, it worked, pity that only the python files add up to close to 100Mb, but compacted by the INNO everything fell to more or less 30Mb.

  • I remembered now another way I used; I downloaded the Python files, not the installer but the same files and folders and left along with my program. At INNO I configured to add an entry in the Windows registry, so that the files . py (I actually switched to an extension that does not exist in order to avoid conflicts) open with the Python executable (pythonw.exe) that is in the python folder that comes with my program. It’s kind of confusing, but it worked here.

  • What went wrong with Pyinstaller? I use python 3.5 and my pyinstaller works perfectly. You can even create a file. exe single with all dlls inserted in it with Pyinstaller, instead of a folder full of files.

  • pyinstaller just doesn’t run, it doesn’t open. I followed a couple of tutorials and gave in it

  • Try py2exe 0.9.2.2. Download link: https://pypi.python.org/pypi/py2exe/

  • Just compile what cx_Freeze generates with Iexpress. Take a look at my answer. Abs

Show 7 more comments

1 answer

3


You can use cx_Freeze.

  1. pip install cx_Freeze
  2. Create a file called setup.py in the same directory as your file (example test.py)
  3. Inside the setup.py file you play the code I’ll leave in end of response
  4. Execute the command python setup.py build
  5. Inside the build folder you will get your executable.

setup py.

from cx_Freeze import setup, Executable
import sys

base = None

if sys.platform == 'win32':
    base = None


executables = [Executable("teste.py", base=base)]

packages = ["idna"]
options = {
    'build_exe': {

        'packages':packages,
    },

}

setup(
    name = "Nome Executavel",
    options = options,
    version = "1.0",
    description = 'Descricao do seu arquivo',
    executables = executables
)

inserir a descrição da imagem aqui

About your problem with several files other than executable

cx_Freeze does not only compile . exe, but in its own documentation, it is indicated the use of Iexpress, for you to compress the entire directory generated by cx_Freeze into a single . EXE

You can use Iexpress to compress the build directory from cx_Freeze into a self-extracting Archive: an exe which unpacks your application into a Temporary directory and runs it. Iexpress is a Utility that’s included with Windows, intended for making installers, but it Works equally well if you Tell it to run the cx_Freeze-built exe after Extraction.

Source: http://cx-freeze.readthedocs.io/en/latest/faq.html

  • I believe his problem is much bigger than this, in fact lack the AP of the question tell which error exactly occurred.

  • Okay, I’m working on improving the response, and studying both cx_Freeze and pynsist and a few others in depth. I’ve edited the response.

  • 1

    I found other ways to continue the development without having to do exactly that, but the content is very good, I performed some tests with other programs I have and worked well, I think when I tried I was doing something wrong.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.