Python pyinstaller如何在可执行文件中包含json
尝试使用以下内容构建Python pyinstaller如何在可执行文件中包含json,python,json,pyinstaller,specifications,Python,Json,Pyinstaller,Specifications,尝试使用以下内容构建specs.spec文件,以便在可执行文件中包含JSON文件 block_cipher = None added_files = [ ( 'configREs.json', '.'), # Loads the '' file from # your root folder and outputs it with
specs.spec
文件,以便在可执行文件中包含JSON文件
block_cipher = None
added_files = [
( 'configREs.json', '.'), # Loads the '' file from
# your root folder and outputs it with
# the same name on the same place.
]
a = Analysis(['gui.pyw'],
pathex=['D:\\OneDrive\\Programming\\Python Projects\\Python'],
binaries=[],
datas=added_files,
hiddenimports=[],
hookspath=[],
runtime_hooks=[],
excludes=[],
win_no_prefer_redirects=False,
win_private_assemblies=False,
cipher=block_cipher)
pyz = PYZ(a.pure, a.zipped_data,
cipher=block_cipher)
exe = EXE(pyz,
a.scripts,
exclude_binaries=True,
name='name here',
debug=False,
strip=False,
upx=True,
console=False, icon='iconname.ico', version='version.rc' )
coll = COLLECT(exe,
a.binaries,
a.zipfiles,
a.datas,
strip=False,
upx=True,
name='gui')
就像克林特在书中推荐的那样
但不工作。
pyi makespec specs.py中像这样构建spec文件
pyinstaller.exe--onefile--windowed--icon=logo1.ico script.py
添加带有标志的文件后,在运行时,这些文件将被提取到临时目录中,如
C:/User/Appdata/local/temp/_MEIXXX
,因此需要从该目录加载文件
您可以使用获取当前临时目录并从那里加载文件
import os
import sys
def resource_path(relative_path):
if hasattr(sys, '_MEIPASS'):
return os.path.join(sys._MEIPASS, relative_path)
return os.path.join(os.path.abspath("."), relative_path)
if __name__ == "__main__":
scope = ['https://spreadsheets.google.com/feeds',
'https://www.googleapis.com/auth/drive']
credentials = ServiceAccountCredentials.from_json_keyfile_name(
resource_path('configREs-.json'), scope)
client = gspread.authorize(credentials)
然后使用以下命令生成可执行文件:
pyinstaller -F --add-data "configREs.json;." script.py
您的json文件名
configREs.json
是否存在?您的配置似乎很好,您如何在代码中调用json文件?@py_saad yes。名称不同,但它与脚本存在于同一文件夹中。@M.R.类似于so-scope=['https://spreadsheets.google.com/feeds', 'https://www.googleapis.com/auth/drive']credentials=ServiceAccountCredentials.from_json_keyfile_name('configREs-.json',scope)client=gspread.authorize(凭证)
@M.R.有没有办法在代码中写入json文件的内容,这样它就不必通过文件访问了?太棒了!添加“驱动程序”文件也可以吗?需要进行哪些调整?@Omri使用上述功能,您可以加载任意数量的文件。对每个文件使用添加数据
标志。我尝试在另一台计算机上激活exe,但失败。虽然在我的电脑上它可以工作-你知道为什么吗?“无法执行脚本SCRIPTNAME”。我意识到——添加数据只适用于非代码文件。我的脚本正在代码中使用这些文件(JSON、driver.exe、icon.ico)。那么问题就在路上了,你怎么看?