简体   繁体   中英

python .exe file won't open/run

I wrote a python file in visual studio code with anaconda as the interpreter. I used pyinstaller to turn the file into an exe but when I try to open the exe a cmd window flashes open for a second and then closes. I don't know why it won't open.My program is supposed to read and print out specific data requested by the user from a HDF5 file and it does exactly that in visual studio code. I really just need a way to make it able to be run by someone on a different computer with python not installed.

Here is my entire code I know its probably bad because I don't have much python experience, but it works in visual studio code:

import numpy as np
import h5py

print ("Make sure to move the HDF file to the folder where this program is located.")
valid = "valid"

#gets the file name and checks if file exists
while valid == "valid":
    filename = input("\nwhat is the name of the HDF file including the .hdf: ")
    try:
        with h5py.File(filename,"r") as hdf:
            path = hdf.get("/Results/Unsteady/Output/Output Blocks/Base Output/Unsteady Time Series/2D Flow Areas/Flow Area")
            break
    except IOError:
        print ("File not found")

#opens file
with h5py.File(filename,"r") as hdf:

    #navigates to file location
    path = hdf.get("/Results/Unsteady/Output/Output Blocks/Base Output/Unsteady Time Series/2D Flow Areas/Flow Area")
    path_items = list(path.items())

    #keeps running until user tells it to stop
    run = "y"
    while run == "y" or run == "Y" or run == "yes" or run == "Yes":

        #user input
        while valid == "valid":
            choice = input("\nWhich file would you like to get data from?  Depth, Face Shear stress, Face Velocity, or Water Surface: ")
            choice = choice.lower()
            if choice == "depth" or choice == "face shear stress" or choice == "face velocity" or choice == "water surface":
                break
            else:
                print ("Invalid")

        #checks the user input then converts the data set into a numpy array
        if choice == "depth":
            dataset = np.array(path.get("Depth"))
        if choice == "face shear stress":
            dataset = np.array(path.get("Face Shear Stress"))
        if choice == "face velocity":
            dataset = np.array(path.get("Face Velocity"))
        if choice == "water surface":
            dataset = np.array(path.get("Water Surface"))

        #gets the shape/size of the dataset
        shape = str(dataset.shape)
        shape = shape.replace("(","")
        shape = shape.replace(")","")
        shape = shape.split(",")
        timeAmount = shape[0]
        pointAmount = shape[1]
        timeAmount = int(timeAmount)
        pointAmount = int(pointAmount)
        timeAmount -= 1
        pointAmount -= 1
        print ("\nThis data set has",timeAmount,"time values and",pointAmount,"data values.")

        #user input
        while valid == "valid":
            time = input("\nEnter a single time step: ")
            try:
                int(time)
                break
            except ValueError:
                print ("Invalid")
        time = int(time)

        while valid == "valid":
            minR = input("\nEnter the first (smaller) value in a range of cell locations: ")
            try:
                int(minR)
                break
            except ValueError:
                print ("Invalid")
        minR = int(minR)

        while valid == "valid":
            maxR = input("\nEnter the second (larger) value in a range of cell locations: ")
            try:
                int(maxR)
                break
            except ValueError:
                print ("Invalid")
        maxR = int(maxR)

        #calculates all the numbers in the range between the two numbers
        rangeL = []
        while minR != maxR:
            rangeL.append(minR)
            minR += 1
        rangeL.append(maxR)

        #prints the value at each point
        count = 0
        for x in range(len(rangeL)):
            tempN = rangeL[count]
            tempV = dataset[time,rangeL[count]]
            print (str(tempN) + "," + str(tempV))
            count += 1

        #asks the user if they want to get more data
        run = input("\nWould you like to enter more parameters to get more/different data? (y/n): ")
        if run == "y" or run == "Y" or run == "yes" or run == "Yes":
            pass
        else:
            print ("Goodbye")
            break

Probably because you have a code with just a few outputs. That means that the programm executes those lines really fast and eventually closes. Request for an input at the end of your code. That should make the program to stay open.

It worked for me. I used pyinstaller with command: pyinstaller -F file_name.py . It works. Doesn't close fast and asks for the input also.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM