i与Python 2.7一起工作,并且我有一个python脚本,可以使用python命令,但是当我使用python命令使用python,但是当我使用py2exe或cx_freeze或cx_freeze或cx_freeze将此脚本转换为可执行文件时Pyinstaller并尝试运行它,窗口会像程序崩溃一样立即打开和关闭。我尝试了另一个简单的脚本,例如打印函数或一些数学功能,可执行文件正常工作,以便任何人可以帮助原因?
谢谢
这是我的代码:
import sys
import paramiko
import getpass
def print_menu():
print 30 * "-", "MENU", 30 * "-"
print "1. LAB1"
print "2. LAB2"
print "3. LAB3"
print "4. Exit"
print 67 * "-"
def ssh_command(ssh):
while True:
command = raw_input("Enter command or q :")
ssh.invoke_shell()
stdin, stdout, stderr = ssh.exec_command(command)
stdout = stdout.readlines()
if command == "q":
break
for line in stdout:
if "Size" in line:
print "found the string"
break`enter code here`
else:
print "There was no output for this command"
def ssh_connect(host, user, password):
try:
ssh = paramiko.SSHClient()
print('Connecting...')
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(hostname=host, username=user, password=password)
ssh_command(ssh)
except Exception as e:
print('Connection Failed')
print(e)
def ssh_close():
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.close()
def credentials(host):
user = raw_input("Username:")
password = getpass.getpass("password for " + user + ":")
ssh_connect(host, user, password)
loop = True
while loop:
print_menu()
choice = input("Enter your choice [1-3]: ")
if choice == 1:
credentials('x.x.x.x')
elif choice == 2:
credentials('x.x.x.x')
elif choice == 3:
credentials('x.x.x.x')
elif choice == 4:
loop = False
print "Closing SSH connection"
print
ssh_close()
else:
raw_input("Wrong option selection. Enter any key to try again..")
您可以通过在命令提示符中运行exe文件来检查错误。这会给您带来巨大的优势。
尤其是在cx_freeze中,您必须提及依赖项。
我认为您正在面临一些依赖性问题。
当您在包装时指定pyinstall
命令后指定--debug=all
时,在DIST文件夹中启动应用程序时会看到特定的错误。
在此处阅读https://pyinstaller.readthedocs.io/en/stable/when-things-go-wrong.html,以获取有关调试特定错误以及如何修复它们的更多信息。
您可以使用带有-F
参数的Pyinstaller来完全打包Python解释器,然后打开Windows CMD并运行它 pyinstaller -F <your_script>.py
不用担心,我的朋友!只需在程序末尾添加window.mainloop()
调用即可。然后,一切都应该正常工作。我被同样的问题所困扰,从你的话中得到了启示:
我尝试了另一个简单的脚本,例如打印功能或某些数学功能,可执行文件正常工作
所以,我并存两个程序并收到了答案。
用f标志运行pyinstaller应该解决启动后立即关闭的问题。