Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/348.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python I';我不知道如何[使用PyInstaller]在.exe文件中正确执行此代码_Python_Json_Python 3.x_Pyinstaller - Fatal编程技术网

Python I';我不知道如何[使用PyInstaller]在.exe文件中正确执行此代码

Python I';我不知道如何[使用PyInstaller]在.exe文件中正确执行此代码,python,json,python-3.x,pyinstaller,Python,Json,Python 3.x,Pyinstaller,我正在尝试使用PyInstaller将.py文件转换为.exe文件。这个过程很顺利。但是,当我尝试运行.exe文件时,程序提到它找不到“client_secret.json” 我不知道如何在文件包中正确包含JSON文件,以便正确执行转换后的.exe文件 我是PyInstaller的新手,所以请不要对我太苛刻:/ 我已尝试将“client_secret.json”添加到PALSheet.spec的.spec文件中,但.exe文件(更新)无法运行[也是由于无法找到“client_secret.jso

我正在尝试使用PyInstaller将.py文件转换为.exe文件。这个过程很顺利。但是,当我尝试运行.exe文件时,程序提到它找不到“client_secret.json”

我不知道如何在文件包中正确包含JSON文件,以便正确执行转换后的.exe文件

我是PyInstaller的新手,所以请不要对我太苛刻:/

我已尝试将“client_secret.json”添加到PALSheet.spec的.spec文件中,但.exe文件(更新)无法运行[也是由于无法找到“client_secret.json”]

以下是“PALSheet.py”的代码:

将“PALSheet.py”转换为“PALSheet.exe”后,文件读取以下错误:

回溯(最近一次呼叫最后一次): 文件“palsheet.py”,第9行,在 文件“site packages\oauth2client\service\u account.py”,第219行,from\u json\u keyfile\u name FileNotFoundError:[Errno 2]没有这样的文件或目录:“client\u secret.json”
[280]无论何时使用生成.exe,都无法执行脚本PALSheet

pyinstaller-F foobar.py

pyinstaller将在appdata fodler中创建一个临时目录,其中包含.exe的依赖项。如果不使用-F标志构建它,则SE将与可执行文件本身位于同一目录中。要访问此临时文件夹,您需要使用以下功能:

导入系统,操作系统 def资源路径(相对路径:str)->str: “”“获取资源的绝对路径”“” 尝试: #PyInstaller创建临时文件夹并将路径存储在_MEIPASS中 基本路径=系统路径 除例外情况外: 基本路径=os.path.abspath(“.”) 返回os.path.join(基本路径、相对路径) 然后,您可以用
ressource\u路径(“client\u secret.json”)
替换
“client\u secret.json”
,它甚至可以使用pyinstaller的-F标志(当然也可以不使用-F标志)。 您需要将数据附加到pyinstaller,因此新的build命令是:

pyinstaller-F——添加数据“client_secret.json;client_secret.json”foobar.py

这是您的新“PALSheet.py”:

导入tkinter
进口gspread
从oauth2client.service\u帐户导入ServiceAccountCredentials
导入系统,操作系统
def资源路径(相对路径:str)->str:
“”“获取资源的绝对路径”“”
尝试:
#PyInstaller创建临时文件夹并将路径存储在_MEIPASS中
基本路径=系统路径
除例外情况外:
基本路径=os.path.abspath(“.”)
返回os.path.join(基本路径、相对路径)
#使用creds创建与googledrive API和googlesheets API交互的客户端
范围=https://spreadsheets.google.com/feeds','https://www.googleapis.com/auth/drive'
creds=ServiceAccountCredentials.from_json_keyfile_name(ressource_path(“client_secret.json”),作用域)
client=gspread.authorize(creds)
#按名称查找工作簿并打开第一张工作表
#确保您在此处使用正确的名称。
sheet=client.open(“PAL教程数据电子表格”)。sheet1
def ResetData():
学生条目。删除('0','end')
等级\条目。删除('0','end')
类_条目。删除('0','end')
主题\u条目。删除('0','end')
def FindNextRow():
全球下一行
下一行=((len(sheet.col_值(int(1)))+1)
def InputData():
FindNextRow()
全球下一行
sheet.update\u单元格(下一行,1,student\u entry.get())
sheet.update_单元格(下一行,2,grade_entry.get())
sheet.update_单元格(下一行,3,class_entry.get())
sheet.update_单元格(下一行,4,topic_entry.get())
学生条目。删除('0','end')
等级\条目。删除('0','end')
类_条目。删除('0','end')
主题\u条目。删除('0','end')
window=tkinter.Tk()
窗口标题(“PAL数据输入”)
学生标签=tkinter.label(窗口,text=“学生姓名:”)
学生标签网格(row=“2”,column=“1”)
学生条目=tkinter.entry(窗口)
学生输入表格(row=“2”,column=“2”)
grade\u label=tkinter.label(窗口,text=“grade Level:”)
grade_标签网格(row=“3”,column=“1”)
等级\输入=输入(窗口)
grade_条目网格(row=“3”,column=“2”)
class\u label=tkinter.label(窗口,text=“数学类:”)
类别标签网格(row=“4”,column=“1”)
class_entry=tkinter.entry(窗口)
类别输入网格(行=“4”,列=“2”)
topic\u label=tkinter.label(窗口,text=“问题主题:”)
主题标签网格(row=“5”,column=“1”)
topic_entry=tkinter.entry(窗口)
主题\u条目网格(row=“5”,column=“2”)
进程按钮=tkinter.button(窗口,text=“更新工作表”,命令=InputData)
处理按钮网格(row=“6”,column=“2”)
重置按钮=tkinter.button(窗口,text=“重置字段”,命令=重置数据)
重置按钮网格(行=“6”,列=“1”)
window.mainloop()
现在您需要运行:

pyinstaller-F——添加数据“client_secret.json;client_secret.json”“PALSheet.py”


我是否需要在.py文件中包含上述函数?我不理解100%是的,因为您调用的是
ressource\u路径(“client\u secret.json”)
而不是
“ressource\u路径”
,您需要在.py中定义它,不客气。如果有效,请留下一票,并将其标记为已解决。否则:请随时再次询问:)foobar.py只是文件的占位符。。。由于您的名为“PALSheet.py”,只需将其替换为“PALSheet.py”……只需将该函数复制粘贴到脚本中,并将所有
“ressource\u path”
替换为
ressource\u path(“client\u secret.json”)
我还需要添加您建议的函数吗?或者这是pyinstaller的一部分?是的,forgott将实施它。现在它应该可以运行了,但现在我觉得我在可访问性方面有问题。我正在使用gspread(链接到Google Sheets),现在它显示:PermissionError:[Errno 13]权限被拒绝:'C:Users\\Bee\u Boy\\AppData\\Local\\Temp\\u MEI19522\\client\u secret.json以管理员身份运行?
import tkinter
import gspread
from oauth2client.service_account import ServiceAccountCredentials


# use creds to create a client to interact with the Google Drive API and Google Sheets API
scope = 'https://spreadsheets.google.com/feeds','https://www.googleapis.com/auth/drive'

creds = ServiceAccountCredentials.from_json_keyfile_name('client_secret.json', scope)
client = gspread.authorize(creds)

# Find a workbook by name and open the first sheet
# Make sure you use the right name here.
sheet = client.open("PAL Tutoring Data SpreadSheet").sheet1

def ResetData():
    student_entry.delete('0', 'end')
    grade_entry.delete('0', 'end')
    class_entry.delete('0', 'end')
    topic_entry.delete('0', 'end')

def FindNextRow():
    global next_row
    next_row = ((len(sheet.col_values(int(1)))) + 1)

def InputData():
    FindNextRow()

    global next_row
    sheet.update_cell(next_row, 1, student_entry.get())
    sheet.update_cell(next_row, 2, grade_entry.get())
    sheet.update_cell(next_row, 3, class_entry.get())
    sheet.update_cell(next_row, 4, topic_entry.get())

    student_entry.delete('0', 'end')
    grade_entry.delete('0', 'end')
    class_entry.delete('0', 'end')
    topic_entry.delete('0', 'end')

window = tkinter.Tk()
window.title("PAL Data Input")

student_label = tkinter.Label(window, text = " Student Name: ")
student_label.grid(row = "2", column = "1")
student_entry = tkinter.Entry(window)
student_entry.grid(row = "2", column = "2")

grade_label = tkinter.Label(window, text = "Grade Level: ")
grade_label.grid(row = "3", column = "1")
grade_entry = tkinter.Entry(window)
grade_entry.grid(row = "3", column = "2")

class_label = tkinter.Label(window, text = "Math Class: ")
class_label.grid(row = "4", column = "1")
class_entry = tkinter.Entry(window)
class_entry.grid(row = "4", column = "2")

topic_label = tkinter.Label(window, text = " Problem Topic: ")
topic_label.grid(row = "5", column = "1")
topic_entry = tkinter.Entry(window)
topic_entry.grid(row = "5", column = "2")

process_button = tkinter.Button(window, text = "Update Sheet", command = InputData)
process_button.grid(row = "6", column = "2")

reset_button = tkinter.Button(window, text = "Reset Fields", command = ResetData)
reset_button.grid(row = "6", column = "1")

window.mainloop()