Irfan Umar
Irfan Umar

Reputation: 197

Installing modules inside python .py file

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

Upvotes: 0

Views: 421

Answers (1)

Dr. Regex
Dr. Regex

Reputation: 166

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"

Upvotes: 1

Related Questions