cant get py2exe to work

风格不统一 提交于 2019-12-10 11:28:51

问题


for ages now i have been trying to package my python applications up as a stand alone executable. i have been attempting to use py2exe but it has never seemed to work.

lets say we have machine A and machine B, i build the program on machine A and try to run as an executable on machine B. on the first few attempts when trying to run on machine B it would say FATAL ERROR: Cannot load python library but i thought that was down to the py2exe process saying 1 missing module full:

  1 missing Modules
  ------------------
? readline                            imported from cmd, code, pdb
Building 'dist\workinghoursGUI.exe'.
Building shared code archive 'dist\library.zip'.
Copy c:\python34\python34.dll to dist
Copy C:\Python34\DLLs\select.pyd to dist\select.pyd
Copy C:\Python34\DLLs\_lzma.pyd to dist\_lzma.pyd
Copy C:\Python34\DLLs\_socket.pyd to dist\_socket.pyd
Copy C:\Python34\DLLs\unicodedata.pyd to dist\unicodedata.pyd
Copy C:\Python34\DLLs\_bz2.pyd to dist\_bz2.pyd
Copy C:\Python34\DLLs\pyexpat.pyd to dist\pyexpat.pyd
Copy C:\Python34\DLLs\_hashlib.pyd to dist\_hashlib.pyd
Copy C:\Python34\DLLs\_ssl.pyd to dist\_ssl.pyd
Copy C:\Python34\DLLs\_ctypes.pyd to dist\_ctypes.pyd
Copy C:\Python34\DLLs\_tkinter.pyd to dist\_tkinter.pyd
Copy DLL C:\Python34\MSVCR100.dll to dist\
Copy DLL C:\Python34\DLLs\tk86t.dll to dist\
Copy DLL C:\Python34\DLLs\tcl86t.dll to dist\

then i saw this question Py2Exe "Missing Modules" and found out i needed to install pyreadline so i did, then the py2exe process didnt return any missing modules like so:

Building 'dist\workinghoursGUI.exe'.
Building shared code archive 'dist\library.zip'.
Copy c:\python34\python34.dll to dist
Copy C:\Python34\lib\site-packages\win32\_win32sysloader.pyd to dist\_win32sysloader.pyd
Copy C:\Python34\DLLs\_ssl.pyd to dist\_ssl.pyd
Copy C:\Python34\DLLs\select.pyd to dist\select.pyd
Copy C:\Python34\DLLs\_socket.pyd to dist\_socket.pyd
Copy C:\Python34\DLLs\_bz2.pyd to dist\_bz2.pyd
Copy C:\Python34\DLLs\_lzma.pyd to dist\_lzma.pyd
Copy C:\Python34\DLLs\pyexpat.pyd to dist\pyexpat.pyd
Copy C:\Python34\DLLs\_tkinter.pyd to dist\_tkinter.pyd
Copy C:\Python34\DLLs\unicodedata.pyd to dist\unicodedata.pyd
Copy C:\Python34\lib\site-packages\win32\win32api.pyd to dist\win32api.pyd
Copy C:\Python34\DLLs\_hashlib.pyd to dist\_hashlib.pyd
Copy C:\Python34\lib\site-packages\win32\win32evtlog.pyd to dist\win32evtlog.pyd
Copy C:\Python34\DLLs\_ctypes.pyd to dist\_ctypes.pyd
Copy DLL C:\Python34\DLLs\tcl86t.dll to dist\
Copy DLL C:\Python34\MSVCR100.dll to dist\
Copy DLL C:\Python34\DLLs\tk86t.dll to dist\
Copy ExtensionDLL C:\Python34\pywintypes34.dll to dist\

but now on machine B when the .exe is run a window with just the output port> is shown while the application runs fine on machine A

this is my setup file:

from distutils.core import setup
import py2exe,sys,os

setup(console=['workinghoursGUI.py'])

and the imports in my program are all in the standard release of python which are:

from tkinter import *
from tkinter import ttk
import tkinter as tk
import tkinter.messagebox as tkm
import datetime,os,time

im probably doing something obviously wrong but i cant work out what, does anyone know how i can resolve this issue or am i just going to have to give up?

the dist folder looks like this:


回答1:


i have finally gotten this to work, using this question py2exe - generate single executable file

i found out i had to modify my setup file which now looks like this:

from distutils.core import setup
import py2exe, sys, os

sys.argv.append('py2exe')
#bundle files set to 2 because its a tkinter app
setup(
    options = {'py2exe': {'bundle_files': 2, 'compressed': True}},
    windows = [{'script': "workinghoursGUI.pyw"}],
    zipfile = None,
)

but then because in my code i was using os.path.dirname(os.path.realpath(__file__)) and py2exe doesnt support __file__, i found myself on this question How do I get the path of the current executed file in Python? and used ArtOfWarfare's answer to get the current path, which is:

from inspect import getsourcefile
from os.path import abspath
...
abspath(getsourcefile(lambda:0))


来源:https://stackoverflow.com/questions/42395538/cant-get-py2exe-to-work

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!