0

I am deploying a custom pytorch model on AWS sagemaker, Following this tutorial. In my case I have few dependencies to install some modules.

I need pycocotools in my inference.py script. I can easily install pycocotool inside a separate notebook using this bash command,

%%bash

pip -g install pycocotools

But when I create my endpoint for deployment, I get this error that pycocotools in not defined. I need pycocotools inside my inference.py script. How I can install this inside a .py file

3
  • How about running a shell script in the beginning of the code inside inference.py where it runs pip install? Commented Apr 20, 2021 at 21:29
  • 1
    Look for example here Commented Apr 20, 2021 at 21:34
  • Thanks, let me give it a try. Commented Apr 20, 2021 at 21:36

1 Answer 1

1

At the beginning of inference.py add these lines:

from subprocess import check_call, run, CalledProcessError
import sys
import os

# Since it is likely that you're going to run inference.py multiple times, this avoids reinstalling the same package:
if not os.environ.get("INSTALL_SUCCESS"):
    
    try:
        check_call(
        [ sys.executable, "pip", "install", "pycocotools",]
        )
    except CalledProcessError:
        run(
        ["pip", "install", "pycocotools",]
        )
    os.environ["INSTALL_SUCCESS"] = "True"
Sign up to request clarification or add additional context in comments.

2 Comments

I get this error, "OriginalMessage": "Command '['/opt/conda/bin/python3.6', 'pip', 'install', 'pycocotools']' returned non-zero exit status. Please note, it may work in simple scnerio, but I am using it in deploying sagemaker endpoint and testing it on Postman app
This worked for me anyhow. Thanks! import subprocess list_files = subprocess.run(["pip", "install", "pycocotools"])

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.