Ramesh Raj
Ramesh Raj

Reputation: 36

import boto3 ImportError: No module named boto3 docker container

I am trying to deploying code from bitbucket to AWS Elastic beanstalk. While deploying code from bitbucket i am facing import boto3 problem.

But in my old docker image working fine.

OLD Docker File

ENV CRYPTOGRAPHY_DONT_BUILD_RUST=1    
 RUN apk add --update --no-cache build-base python3-dev python3 libffi-dev libressl-dev bash git gettext ca-certificates curl groff less zip libstdc++ jq \
  && curl -O https://bootstrap.pypa.io/get-pip.py \
  && python3 get-pip.py \
  && pip install --upgrade --use-feature=2020-resolver six awscli awsebcli \
  && rm -rf /var/cache/apk/* 
  
 RUN apk --no-cache add python py-pip \
  && pip --no-cache-dir install --upgrade pip \
  && pip --no-cache-dir install --upgrade --user boto3 boto
 
 RUN apk update && \
     apk add --no-cache git openssh perl && \
     pip install pytz tzlocal

in Python2.7 everything working fine. when i am upgrading python version from 2.7 to 3.7 getting this import boto3 error.

Docker File

ENV CRYPTOGRAPHY_DONT_BUILD_RUST=1    
 RUN apk add --update --no-cache build-base python3-dev python3 libffi-dev libressl-dev bash git gettext ca-certificates curl groff less zip libstdc++ jq py3-pip\
  && pip3 install  --upgrade six awscli awsebcli \
  && rm -rf /var/cache/apk/* 
  
 RUN apk --no-cache add python3 py3-pip\
  && python3 -m venv env-env \
  && source env-env/bin/activate \
  && pip3 install --upgrade pip \
  && pip3 install --upgrade boto3 
 RUN apk update  && \
     apk add --no-cache git openssh perl && \
     python3 -m venv env-env && \
     source env-env/bin/activate && \
     pip3 install pytz tzlocal
 ENV PATH "$PATH:~/.local/bin"
 
 ADD deployment-scripts /opt/deployment-scripts

Python Script

from __future__ import print_function
import os
import sys
from time import strftime, sleep
import boto3
from botocore.exceptions import ClientError
from time import strftime
from datetime import datetime
from pytz import timezone
from tzlocal import get_localzone

when i am running this script file i got

import boto3 ImportError: No module named boto3

i had tried

python3 -m pip install boto3

but i get same error while running script. help me out from this.

As per python update documet https://boto3.amazonaws.com/v1/documentation/api/1.16.56/guide/migrationpy3.html

we have to upgrade from python 2.7 to python3.7

Upvotes: 1

Views: 4625

Answers (1)

larsks
larsks

Reputation: 311238

You've only installed boto3 inside your env-env virtual environment. If you want to make use of that module, you'll need to activate the environment first.

Compare:

/ # which python3
/usr/bin/python3
/ # python3
Python 3.9.5 (default, May 12 2021, 20:44:22)
[GCC 10.3.1 20210424] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import boto3
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'boto3'

With:

/ # . env-env/bin/activate
(env-env) / # which python3
/env-env/bin/python3
(env-env) / # python3
Python 3.9.5 (default, May 12 2021, 20:44:22)
[GCC 10.3.1 20210424] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import boto3
>>>

Another solution is to not use the virtual environment. It's not clear why you're creating it, since you're building a container image -- the normal reasons for creating an isolated Python environment for the most part don't apply, because the container itself will be your isolation.


On an unrelated note, this...

 ENV PATH "$PATH:~/.local/bin"

...doesn't do what you think it does. The ENV keyword doesn't expand ~, so you end up with a path that contains a literal ~ which isn't going to match anything. Additionall, $PATH is going to match the value of $PATH on your host, not whatever the container default is, so you may end up defining a value for $PATH containing a variety of directories that don't exist.


There's a lot going on in your Dockerfile that I'm not sure is necessary (since I'm not running your code). This was sufficient to allow me to write Python code that uses the boto3 module:

FROM alpine:latest

ENV CRYPTOGRAPHY_DONT_BUILD_RUST=1
RUN apk add --update --no-cache \
    build-base \
    ca-certificates \
    curl \
    git \
    py3-cryptography \
    py3-pip \
    python3 \
    python3-dev \
    zip

RUN pip3 install --upgrade \
    boto3 \
    pytz \
    tzlocal

Upvotes: 3

Related Questions