Shad_Ahm
Shad_Ahm

Reputation: 13

Python script executable crashes immediately

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?

Thanks

Here is my code:

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

Upvotes: 1

Views: 9520

Answers (5)

Okechukwu Malcolm
Okechukwu Malcolm

Reputation: 37

Running pyinstaller with the F flag should solve the problem of immediate close after startup.

Upvotes: 0

the unknown
the unknown

Reputation: 1

Worry not, my friend! Just add a window.mainloop() call at the end of your program. Then, everything should work properly. I was stumped by the same problem got revelation from your words:

I tried another simple scripts like print function or some math function the executable files work fine

So, I compared both programs side by side and received my answer.

Upvotes: 0

dignitas123
dignitas123

Reputation: 101

When you specifiy --debug=all after your pyinstall command when packaging, you will see specific errors when starting your applicataion in the dist folder.

Read here https://pyinstaller.readthedocs.io/en/stable/when-things-go-wrong.html to get more information on debugging specific errors and how to fix them.

Upvotes: 1

Nikhil.Nixel
Nikhil.Nixel

Reputation: 584

You can check the error by running the exe file in command prompt. This will give you an upper hand.

Especially in cx_freeze you have to mention the dependencies.

I think you are facing some dependency problem.

Upvotes: 1

Basim Aly
Basim Aly

Reputation: 134

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

Upvotes: 0

Related Questions