相关文章推荐

Can not load package data resources when running in a zipapp #2965

@sinoroc

Description

@sinoroc
  • OS version and name : Ubuntu Linux 18.04
  • Poetry version : 1.0.10 (in a zipapp )
  • Python : CPython 3.6.9
  • Issue

    $ poetry config -vvv --list
    [ValueError]
    Schema poetry-schema does not exist.
    Traceback (most recent call last):
      File "/home/sinoroc/.local/bin/poetry/clikit/console_application.py", line 131, in run
        status_code = command.handle(parsed_args, io)
      File "/home/sinoroc/.local/bin/poetry/clikit/api/command/command.py", line 120, in handle
        status_code = self._do_handle(args, io)
      File "/home/sinoroc/.local/bin/poetry/clikit/api/command/command.py", line 171, in _do_handle
        return getattr(handler, handler_method)(args, io, self)
      File "/home/sinoroc/.local/bin/poetry/cleo/commands/command.py", line 92, in wrap_handle
        return self.handle()
      File "/home/sinoroc/.local/bin/poetry/poetry/console/commands/config.py", line 75, in handle
        local_config_file = TomlFile(self.poetry.file.parent / 'poetry.toml')
      File "/home/sinoroc/.local/bin/poetry/poetry/console/commands/command.py", line 10, in poetry
        return self.application.poetry
      File "/home/sinoroc/.local/bin/poetry/poetry/console/application.py", line 49, in poetry
        self._poetry = Factory().create_poetry(Path.cwd())
      File "/home/sinoroc/.local/bin/poetry/poetry/factory.py", line 48, in create_poetry
        check_result = self.validate(local_config)
      File "/home/sinoroc/.local/bin/poetry/poetry/factory.py", line 272, in validate
        validation_errors = validate_object(config, 'poetry-schema')
      File "/home/sinoroc/.local/bin/poetry/poetry/json/__init__.py", line 22, in validate_object
        raise ValueError('Schema {} does not exist.'.format(schema_name))
    

    Culprit is probably this block of code:

    # [...]
    SCHEMA_DIR = os.path.join(os.path.dirname(__file__), "schemas")
    class ValidationError(ValueError):
    def validate_object(obj, schema_name):  # type: (dict, str) -> List[str]
        schema = os.path.join(SCHEMA_DIR, "{}.json".format(schema_name))
       # [...]
    

    Loading resources from package data should be done with pkgutil.get_data() (or importlib-resources or pkg_resources), especially if the code is running directly from a zipped file. These libraries are able to handle zipped resources.

    References:

  • https://github.com/wimglenn/resources-example
  • https://gitlab.com/python-devs/importlib_resources/-/issues/58#note_404347272 -- Meaning support in importlib-resources is now much better since 1.3 and it seems to have been ported into Python 3.9's importlib.resources
 
推荐文章