简体   繁体   English

在 python.py 文件中安装模块

[英]Installing modules inside python .py file

I am deploying a custom pytorch model on AWS sagemaker, Following this tutorial.我按照教程在 AWS sagemaker 上部署自定义 pytorch model。 In my case I have few dependencies to install some modules.就我而言,我几乎没有安装一些模块的依赖项。

I need pycocotools in my inference.py script.我的 inference.py 脚本中需要 pycocotools。 I can easily install pycocotool inside a separate notebook using this bash command,我可以使用这个 bash 命令轻松地将 pycocotool 安装在单独的笔记本中,

%%bash

pip -g install pycocotools

But when I create my endpoint for deployment, I get this error that pycocotools in not defined.但是,当我为部署创建端点时,我收到了未定义 pycocotools 的错误。 I need pycocotools inside my inference.py script.我的 inference.py 脚本中需要 pycocotools。 How I can install this inside a.py file我如何在 .py 文件中安装它

At the beginning of inference.py add these lines:在 inference.py 的开头添加以下行:

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"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM