Python 脚本可执行文件立即崩溃 [英] Python script executable crashes immediately

查看:57
本文介绍了Python 脚本可执行文件立即崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 python 2.7,我有一个 python 脚本,可以通过 ssh 连接到远程服务器,它使用来自 cmd 的 python 命令运行良好,但是当我使用 py2exe 或 cx_freeze 或 Pyinstaller 将此脚本转换为可执行文件并尝试运行它时,窗口立即打开和关闭,就像程序崩溃一样.我尝试了另一个简单的脚本,如打印函数或一些数学函数,可执行文件工作正常,所以任何人都可以帮助是什么原因?

I work with python 2.7 and I have a python script that ssh to remote servers and it works fine using python command from cmd but when I convert this script to executable file using py2exe or cx_freeze or Pyinstaller and try to run it, the window open and close immediately like if the program crashes. I tried another simple scripts like print function or some math function the executable files work fine so any one could help what would be the reason?

谢谢

这是我的代码:

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..")

推荐答案

您可以使用 pyinstaller-F 参数完全打包python解释器然后打开windows cmd并运行它pyinstaller -F .py

you can use the pyinstaller with -F argument to fully package the python interpreter then open windows cmd and run it pyinstaller -F <your_script>.py

这篇关于Python 脚本可执行文件立即崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆