我在Visual Studio代码中用anaconda作为解释器编写了一个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
答案 0 :(得分:2)
可能是因为您的代码只有几个输出。这意味着程序会非常快地执行这些行,并最终关闭。在代码末尾请求输入。那应该使程序保持打开状态。
答案 1 :(得分:0)
它对我有用。我将 pyinstaller 与命令一起使用:pyinstaller -F file_name.py
。有用。不会快速关闭并要求输入。