1

I published python package on pypi.org structure looks like this:

/my_package_name-0.0.1
-- README LICENSE ETC..
-- /my_package_name
   -- __init__.py
   -- train_model.py
   -- predict.py
   -- /saved_models
      -- november_model

In predict.py I have function that loads model:

def my_function():
    (some code...)
    net.load_model('./saved_models/november_model')

When I'm trying to use the package:

from my_package.predict import my_function

my_function()

I get error that it can't see the model:

OSError: Unable to open file 
(unable to open file: name = './saved_models/november_model',
errno = 2, error message = 'No such file or directory', flags = 0, o_flags = 0)

I tried also:

net.load_model('saved_models/november_model')
net.load_model('./saved_models/november_model')
net.load_model('../saved_models/november_model')

I can't figure out correct path

rumcajs
  • 129
  • 1
  • 10
  • Make sure that those files are packaged correctly (see "package data") and that they install correctly. To access those package data files at run-time, use [importlib.resources](https://docs.python.org/3/library/importlib.resources.html). – sinoroc Nov 26 '22 at 10:48
  • I don't understand what should I do to acces this "package data". First I tried to run importlib.resources but I get: AttributeError: module 'importlib' has no attribute 'resources' – rumcajs Nov 26 '22 at 12:51
  • Maybe you have an old Python. -- Also read [this](https://stackoverflow.com/a/58941536). – sinoroc Nov 26 '22 at 14:44
  • Thanks I find out the solution after some trials and errors from your source :) – rumcajs Nov 28 '22 at 16:47

1 Answers1

1

The solution was to use importlib.resources in predict.py:

try:
    import importlib.resources as pkg_resources
except ImportError:
    # Try backported to PY<37 `importlib_resources`.
    import importlib_resources as pkg_resources
from my_package import saved_models

and instead of:

net.load_model('saved_models/november_model')

I used:

f = pkg_resources.open_text(saved_models, 'november_model')
net.load_model(f.name)
rumcajs
  • 129
  • 1
  • 10
  • Why rename as `pkg_resources`? [`pkg_resources` is an actual existing library which is part of _setuptools_](https://setuptools.pypa.io/en/latest/pkg_resources.html), and it has a different API, in particular it has no `open_text` function. -- Also do you really want to support "Python < 3.7"? – sinoroc Dec 02 '22 at 12:22