【问题标题】:PermissionError: [Errno 13] Permission denied: pyinstaller.exe/zipcodes windows 10PermissionError:[Errno 13] 权限被拒绝:pyinstaller.exe/zipcodes windows 10
【发布时间】:2021-01-04 19:05:00
【问题描述】:

我正在尝试将 python 模块(邮政编码)包含到我正在转换为 EXE 的程序中。第一次尝试 - 包含该模块数据的 bz2 文件未加载 - 所以我更改了命令行以提取该数据。

测试程序就像下面的(ziptest.py)代码一样简单:

import zipcodes
print('Test message')
checkzip=zipcodes.matching('92688')
print(checkzip)
print("returned the right record:", checkzip[0]['zip_code']=='92688')

pyinstaller 安装命令是:

pyinstaller --debug all --onefile ziptest.py --add-data "venv\lib\site-packages\zipcodes\zips.json.bz2;zipcodes\zips.json.bz2"

当我运行程序并捕获 STDERR - 我认为最相关的代码行是:

import 'json.encoder' # <_frozen_importlib_external.SourcelessFileLoader object at 0x0000019824E121C8>
import 'json' # <_frozen_importlib_external.SourcelessFileLoader object at 0x0000019824E018C8>
Traceback (most recent call last):
  File "ziptest.py", line 1, in <module>
  File "<frozen importlib._bootstrap>", line 983, in _find_and_load
  File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 728, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "c:\users\ken\onedrive - rootdir\code\companyppi-scripts\temp_dir\validation\venv\lib\site-packages\zipcodes\__init__.py", line 32, in <module>
    with bz2_open(_zips_json, "rb") as f:
  File "C:\Users\ken\AppData\Local\Programs\Python\Python37\lib\bz2.py", line 318, in open
    binary_file = BZ2File(filename, bz_mode, compresslevel=compresslevel)
  File "C:\Users\ken\AppData\Local\Programs\Python\Python37\lib\bz2.py", line 92, in __init__
    self._fp = _builtin_open(filename, mode)
PermissionError: [Errno 13] Permission denied: 'C:\\Users\\ken\\AppData\\Local\\Temp\\_MEI65962\\zipcodes\\zips.json.bz2'
[10396] LOADER: Cleaning up Python interpreter.
# cleanup[3] wiping _codecs

我可能需要根据我目前所看到的研究内容为这个模块编写一个钩子,但在我深入研究之前,我想确保这是正确的路径。我必须相信还有其他模块可以从模块安装中提取源数据 - 所以我想让我的方法适合解决方案。

【问题讨论】:

    标签: python python-3.x windows windows-10 pyinstaller


    【解决方案1】:

    我的同事帮我解决了这个问题 - 首先是在 Linux 上,然后我在 Windows 上得到了一个工作版本。

    这篇文章指出了问题所在以及如何解决它:
    Adding a data file in Pyinstaller using the onefile option

    鉴于第一个答案 - 我修改了 init.py 文件,通过将此函数添加到代码中并在第 31 行替换此行来识别“PyInstaller”:

    def resource_path(relative_path):
        """ Get absolute path to resource, works for dev and for PyInstaller """
        try:
            # PyInstaller creates a temp folder nad stores path in _MEIPASS
            base_path = sys._MEIPASS
        except Exception:
            base_path = os.path.abspath(".")
    
        return os.path.join(base_path, relative_path)
        
    _zips_json = resource_path(os.path.join(os.path.dirname(os.path.abspath(__file__)), "zips.json.bz2"))
    

    我更新了 EXE 生成命令行,如下所示:

    pyinstaller --onefile ziptest.py  --add-data ".\venv\Lib\site-packages\zipcodes\zips.json.bz2;zipcodes"
    

    这将创建我希望创建的 onefile EXE。

    我还访问了 zipcodes 项目并发布了这个补丁,希望他们能将其合并以使他们的代码与 PyInstaller 兼容。

    【讨论】:

      猜你喜欢
      • 2015-07-17
      • 2016-07-25
      • 2018-11-18
      • 2021-04-23
      • 2020-07-01
      • 2016-11-12
      • 2019-11-09
      • 2020-06-06
      • 2021-11-11
      相关资源
      最近更新 更多