简体   繁体   中英

Compile multiple .py files to windows executable (.exe)

I have a relatively small python program that I want to convert to a windows executable. It was originally written with Pycharm and runs normally in it.

It consists of two .py files that I have written and some libraries (all installed from pip).

I am trying to do my job with cx_Freeze but not with much success. My setup.py is this:

from cx_Freeze import setup, Executable
import os.path

PYTHON_INSTALL_DIR = os.path.dirname(os.path.dirname(os.__file__))
os.environ['TCL_LIBRARY'] = os.path.join(PYTHON_INSTALL_DIR, 'tcl', 'tcl8.6') 
os.environ['TK_LIBRARY'] = os.path.join(PYTHON_INSTALL_DIR, 'tcl', 'tk8.6')

setup(name="MFS-printer",
      version='1.0.0',
      description='A parser for the log file from the terminal exit of the mfs system',
      options={"build_exe": {"packages": ["file_read_backwards", "Pil", "watchdog", "win32print", "win32ui", "tkinter", "log_parser"],
                             "include_files": ["Roboto-Bold.ttf", "mfs_robot(2).png", os.path.join(PYTHON_INSTALL_DIR, 'DLLs', 'tk86t.dll'), os.path.join(PYTHON_INSTALL_DIR, 'DLLs', 'tcl86t.dll'),]
                             }
                },
      executables = [Executable("main.py"), Executable("log_parser.py")], requires=['watchdog']
      )

After running python setup.py build to create the windows application no errors exist but when I try to run the application it crashes at start with this error:

这是在我双击可执行文件之后

My imports from those two files (main.py and log_parser.py) are the folowing:

main.py:

import os,time
import datetime
import log_parser

import win32print
from tkinter import filedialog
from tkinter import *

from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler
from pathlib import Path

log_parser.py

import os

import win32print
import win32ui

from file_read_backwards import FileReadBackwards
from PIL import Image, ImageDraw, ImageFont, ImageWin

There is probably something wrong with the setup.py but I can't find what. Any help would be appreciated.

There is probebly a better answer, but I would just use python threading. You can combine both scripts into one and run them at the same time.

Here is a threading example:

from threading import Thread
from time import sleep

def script1():
    while (True):
        print("1")
        sleep(2)

def script2():
    while (True):
        sleep(.1)
        print("2")
        sleep(1.9)

Thread(target = script1).start()
Thread(target = script2).start()

Edit, Try this:

Import all the necessary modules to the first (Main) script. Than add from [filenameofsecondscript] import * , the script will be ran on import so make sure to only use definitions, than call the definition when needed. Than create a new exe with the second script included as a .py (not exe!).

It seems that for now cx_Freeze does not really support Python3.7 for Windows 64bit (until this date). This it the Github issue that refers to it. I hope by the time someone else searches for it, it will be fixed!

https://github.com/anthony-tuininga/cx_Freeze/issues/399

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM