Question

Type error when running model trained in Roboflow in production environment

I can make inference from my trained Roboflow model using Google Colab and the AWS cloud9 test environment.

To do this, I used the following code:

from roboflow import Roboflow

rf = Roboflow(api_key="xxxxxxxxxxxxxxxxx")
path = "/context/image.jpeg"
project = rf.workspace().project("xxxxxx")
model = project.version(x).model

# infer on a local image
currency_result= model.predict(path, confidence=40).json()```

However, when I put it into production I got the following error:

    [ERROR] TypeError: expected str, bytes or os.PathLike object, not NoneType
Traceback (most recent call last):
  File "/var/lang/lib/python3.8/imp.py", line 234, in load_module
    return load_source(name, filename, file)
  File "/var/lang/lib/python3.8/imp.py", line 171, in load_source
    module = _load(spec)
  File "<frozen importlib._bootstrap>", line 702, in _load
  File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 843, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "/var/task/app.py", line 1, in <module>
    from roboflow import Roboflow
  File "/var/lang/lib/python3.8/site-packages/roboflow/__init__.py", line 10, in <module>
    from roboflow.adapters import rfapi
  File "/var/lang/lib/python3.8/site-packages/roboflow/adapters/rfapi.py", line 8, in <module>
    from roboflow.config import API_URL, DEFAULT_BATCH_NAME, DEFAULT_JOB_NAME
  File "/var/lang/lib/python3.8/site-packages/roboflow/config.py", line 51, in <module>
    API_URL = get_conditional_configuration_variable("API_URL", "https://api.roboflow.com")
  File "/var/lang/lib/python3.8/site-packages/roboflow/config.py", line 22, in get_conditional_configuration_variable
    default_path = os.path.join(os.getenv("HOME"), ".config/roboflow/config.json")
  File "/var/lang/lib/python3.8/posixpath.py", line 76, in join
    a = os.fspath(a)

What I found interesting was that I was able to run the code in cloud9 using sam local invoke, but in the production environment I got the above error.

 3  56  3
1 Jan 1970

Solution

 0

As @iurisilvio pointed out, try adding HOME environmental variable into your lambda configuration. The stack trace hints you where the error is, os.getenv("HOME") is None, and hence os.path.join(os.getenv("HOME"), ".config/roboflow/config.json") results in error. Below is from python3.8

>>> import os
>>> os.path.join(None, "foo")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python3.8/posixpath.py", line 76, in join
    a = os.fspath(a)
TypeError: expected str, bytes or os.PathLike object, not NoneType
2024-07-08
Greg0ry