簡體   English   中英

python .exe 文件無法打開/運行

[英]python .exe file won't open/run

我用 anaconda 作為解釋器在 Visual Studio 代碼中編寫了一個 python 文件。 我使用 pyinstaller 將文件轉換為 exe,但是當我嘗試打開 exe 時,一個 cmd 窗口會閃爍打開一秒鍾然后關閉。 不知道為什么打不開我的程序應該從 HDF5 文件中讀取和打印用戶請求的特定數據,它在 Visual Studio 代碼中完全做到了這一點。 我真的只需要一種方法來讓它能夠被另一台未安裝 python 的計算機上的人運行。

這是我的整個代碼,我知道它可能很糟糕,因為我沒有太多的 Python 經驗,但它適用於 Visual Studio 代碼:

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

可能是因為您的代碼只有幾個輸出。 這意味着程序會非常快速地執行這些行並最終關閉。 在代碼末尾請求輸入。 這應該使程序保持打開狀態。

它對我有用。 我使用pyinstaller命令: pyinstaller -F file_name.py 有用。 不會快速關閉並要求輸入。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM