question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Cannot import module while using custom handler

See original GitHub issue

🐛 Bug report

I’m trying to use a custom crowd counting model and while serving it gives me an error while importing custom model libraries.

Python Import code is like below:

module_path = os.path.abspath(os.path.join('../../../../'))  # Parent Dir on local machine
if module_path not in sys.path:
    sys.path.append(module_path+"/SSDCNet")

...
from Network.SSDCNet import SSDCNet_classify

Error:


   File "/tmp/models/58eb8da6a1c8563374304a9fc66b2788a9c9c440/crdcnt_handler.py", line 9, in <module>
   from crowdmodel import CrowdCounter
   File "/tmp/models/58eb8da6a1c8563374304a9fc66b2788a9c9c440/crowdmodel.py", line 8, in <module>
    from Network.SSDCNet import SSDCNet_classify
ModuleNotFoundError: No module named 'Network'

Here is the log file:
server_log3.txt

Environment Details:

Operating System and version: Ubuntu 16.04

image

**Possible Fix: ** Instead of adding to sys.path. Adding the parent folder to PYTHONPATH before you run the torchserve command temporarily fixes the issue.

> export PYTHONPATH=/home/ubuntu/mayub/Github/SSDCNet:$PYTHONPATH 
> echo $PYTHONPATH
> torchserve --ts-config ./config.properties --start --ncs --model-store ./torchserve_model_store --models all

Since this is temporary and session based maybe a better solution is required.

Thanks !

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:1
  • Comments:9 (2 by maintainers)

github_iconTop GitHub Comments

3reactions
ghostcommented, Feb 6, 2021

@mohammedayub44 All the dependency files need to be packaged inside the mar file using the torch-model-archiver utility. TorchServe extracts the mar file in a temporary location and that is already added in the path for importing the modules.

From your code snippet it looks like you are trying to access modules which are outside the mar file.

Could you share your mar file.

how to add a folder as extra files ?

2reactions
harshbafnacommented, May 8, 2020

@mohammedayub44 : torch-model-archiver is just a utility tool for packaging your model and does not validates you model. It is expected that the user will supply all the other dependency files using the --extra-files flag. You can always create an egg/zip file to upload your custom python package if the number of files are more.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Resolve "Unable to import module" errors from Python ...
To resolve this error, create a deployment package or Lambda layer that includes the libraries that you want to use in your Python...
Read more >
Python - Handler doesn't detect the custom handler class
Your sys.path needs to contain the project/confs directory, otherwise you won't be able to import the custom_handler module. Try again after ...
Read more >
How do I resolve the "Unable to import module ... - YouTube
... Knowledge Center article with this video: https://aws.amazon.com/premiumsupport/knowledge-center/lambda- import - module - error -python/0...
Read more >
ModuleNotFoundError: no module named Python Error [Fixed]
How to fix the ModuleNotFoundError in Python · 1. Make sure imported modules are installed · 2. Make sure modules are spelled correctly...
Read more >
Unable to import module 'handler': No module named 'werkzeug'
I fixed it by install zappa in the venv itself. So, follow the steps (assuming you have venv in env folder locally) $...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found