1
schemas
├── common
│   ├── common1.json
│   └── common2.json
├── __init__.py
└── main.json

Why we have to use pkg_resources or pkg_utils to read a static file inside the python package? I am asking this question because I am not able to reference the common1.json schema file from the main.json schema file.

here is the link for the initial head up on external file reference in JSON schema Liquid Studio: How to write a JSON schema $ref to another file.

here is the link to an issue I created on jsonschema package https://github.com/Julian/jsonschema/issues/836.

Below is my analysis of the cause.

This is the function to import the module:

>>> def import_module(module_name):
...     source_path = module_name + '.py'
...     with open(source_path,"r") as module_file:
...             source_code = module_file.read()
...     mod = types.ModuleType(module_name)
...     mod.__file__ = source_path
...     code = compile(source_code,source_path,"exec")
...     exec(code, mod.__dict__)
...     return mod
... 

Is it the same with module_name being __init__.py for package?

As python packages are also modules with an extra __path__ attribute. A file inside the python package should be importable and we need to follow the above sequence to import a module and in that sequence, there is a step where we need to create and execute the module which can be done for python files only.

Is pkg_resourse skipping the execution of the module?

martineau
  • 119,623
  • 25
  • 170
  • 301

0 Answers0