如何通过pyinstaller在可执行文件中包含json
How to include json in executable file by pyinstaller
尝试使用以下内容构建 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')
就像 Clint 在 add json file with pysintaller
中推荐的一样
但不工作。
- 在 cmd 中像这样构建规范文件 -
pyi-makespec specs.py
- 然后构建可执行文件 -
pyinstaller.exe --onefile --windowed --icon=logo1.ico script.py
- 如果 JSON 文件与可执行文件放在同一目录中,则无法运行
- 有什么建议吗?
添加带有 add-data
标志的文件后,在运行时这些文件将被解压缩到类似 C:/User/Appdata/local/temp/_MEIXXX
的临时目录中,因此您需要从该目录加载文件。
您可以使用 sys._MEIPASS
获取当前的临时目录并从那里加载您的文件。
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)
然后生成可执行文件并添加 --add-data
标志:
--add-data <SRC;DEST or SRC:DEST>
Additional non-binary files or folders to be added to the executable. The path separator is platform specific, os.pathsep (which is ; on Windows and : on most unix systems) is used. This option can
be used multiple times.
# The path separator is ; on Windows and : on most unix systems
pyinstaller -F --add-data "configREs.json;." script.py
尝试使用以下内容构建 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')
就像 Clint 在 add json file with pysintaller
中推荐的一样但不工作。
- 在 cmd 中像这样构建规范文件 -
pyi-makespec specs.py
- 然后构建可执行文件 -
pyinstaller.exe --onefile --windowed --icon=logo1.ico script.py
- 如果 JSON 文件与可执行文件放在同一目录中,则无法运行
- 有什么建议吗?
添加带有 add-data
标志的文件后,在运行时这些文件将被解压缩到类似 C:/User/Appdata/local/temp/_MEIXXX
的临时目录中,因此您需要从该目录加载文件。
您可以使用 sys._MEIPASS
获取当前的临时目录并从那里加载您的文件。
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)
然后生成可执行文件并添加 --add-data
标志:
--add-data <SRC;DEST or SRC:DEST>
Additional non-binary files or folders to be added to the executable. The path separator is platform specific, os.pathsep (which is ; on Windows and : on most unix systems) is used. This option can be used multiple times.
# The path separator is ; on Windows and : on most unix systems
pyinstaller -F --add-data "configREs.json;." script.py