[英]How to reference the current package for use with importlib.resources?
[英]Pyinstaller: How to include resources from package used by importlib_resources
我有以下項目結構:
package1/
__init__.py
some.py
package2
__init__.py
some.py
static_data/
__init__.py
file1.txt
file2.txt
...
my_script.py
my_script.py
包含來自整個結構的導入。 除了importlib_resources
使用之外,python 代碼也很好。
我使用importlib_resources
(Python 3.6) 訪問文件, importlib_resources
所示: importlib_resources.open_text(static_data, 'file1.txt').readlines()
構建可執行文件: pyinstaller my_script.py -F --noconsole --noupx
在結果可執行文件中,我收到以下錯誤:
File "lib\site-packages\importlib_resources\_py3.py", line 62, in open_text
File "lib\site-packages\importlib_resources\_py3.py", line 52, in open_binary
FileNotFoundError: 'file1.txt' resource not found in 'package1.package2.static_data'
包含與importlib_resources
使用的資源的正確方法是什么?
默認情況下,PyInstaller 不會將非二進制文件添加到生成的二進制文件中。 您可以手動添加使用--add-data
選項,說明在這里。
假設您正在運行的窗口和您正在訪問的資源文件都在package1
頂級包中,您可以嘗試這樣的事情:
pyinstaller my_script.py -F --noconsole --noupx --add-data "package1;package1"
在腳本中需要數據
import platform
if platform.sys.version.split()[0]>='3.7':
import importlib.resources as importlib_resources
else:
import importlib_resources
settings = json.loads(importlib_resources.read_text('packagename','data.json'))
在 setup.py 中
install_requires=[
'importlib_resources ; python_version<"3.7"'
],
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.