1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Unable to import module 'lambda_function': No module namedとなった時の対応方法

Posted at
  • 環境
    • Cloud9
      • 同僚がEC2を利用したCloud9を使っていたので、私も使ってみたくてそのCloud9にアカウントを作ってもらって早速使い始めた。半端ない初心者。
    • Python 3.6.12

事象 : import requestsをして実行したらモジュールがなくて怒られた

Response
{
    "errorType": "Runtime.ImportModuleError",
    "errorMessage": "Unable to import module 'lambda_function': No module named 'requests'"
}

がーん!でもpip listrequestsがあるとおっしゃっている。

ponsuke:~/environment (asynchronous) $ pip list | grep requests
requests            2.24.0

原因 : 対象のLambda関数にモジュールがインストールされていないから

lambda_function.pyのディレクトリをみればモジュールがないのが分かる。
Cloud9を使ってLambda関数を作るときはpip listを見りゃいいというわけではないらしい。

モジュールのインストール先が間違っていたことがエラーの原因でした.
Cloud9 は Lambda アプリケーション を利用しているため, アプリケーションディレクトリ(今回はDemoApplication/)が Lambda ファンクションの実行ディレクトリになります.
したがって, import requests で requests モジュールを読み込みたい場合はアプリケーションディレクトリ直下にモジュールをインストールする必要がありました.
Cloud9 上で Lambda を実行する際に “Unable to import module : No module named” エラーが出た場合の対処 | Developers.IO

# Lambda関数のディレクトリへ移動して
$ cd lambda_name/

# 中身を見ると・・・モジュールは何にもなかった・・・
ponsuke:~/environment/lambda_name (asynchronous) $ ls -la
total 36
drwxr-xr-x  2 ec2-user ec2-user  4096 Nov 24 01:06 .
drwxr-xr-x 32 ec2-user ec2-user  4096 Nov 20 02:35 ..
-rw-r--r--  1 ec2-user ec2-user   197 Nov 20 02:35 .application.json
-rw-r--r--  1 ec2-user ec2-user  1082 Nov 20 09:04 lambda_function.py
-rw-r--r--  1 ec2-user ec2-user 10690 Nov 24 01:06 lambda-payloads.json
-rw-r--r--  1 ec2-user ec2-user   512 Nov 20 02:35 templap

対応 : 場所を指定してモジュールをインストールする

-t, --target


Install packages into . By default this will not replace existing files/folders in . Use --upgrade to replace existing packages in with new versions.
pip install — pip 9.1.0.dev0 ドキュメント
ponsuke:~/environment/lambda_name (asynchronous) $ pip install --target . requests
Collecting requests
  Downloading requests-2.25.0-py2.py3-none-any.whl (61 kB)
     |████████████████████████████████| 61 kB 7.1 MB/s
Collecting chardet<4,>=3.0.2
  Using cached chardet-3.0.4-py2.py3-none-any.whl (133 kB)
Collecting urllib3<1.27,>=1.21.1
  Downloading urllib3-1.26.2-py2.py3-none-any.whl (136 kB)
     |████████████████████████████████| 136 kB 15.9 MB/s
Collecting certifi>=2017.4.17
  Downloading certifi-2020.11.8-py2.py3-none-any.whl (155 kB)
     |████████████████████████████████| 155 kB 18.3 MB/s
Collecting idna<3,>=2.5
  Using cached idna-2.10-py2.py3-none-any.whl (58 kB)
Installing collected packages: chardet, urllib3, certifi, idna, requests
Successfully installed certifi-2020.11.8 chardet-3.0.4 idna-2.10 requests-2.25.0 urllib3-1.26.2

だめな対応 : 場所を指定しないでモジュールをインストールする

場所を指定しないでインストールしても、もうあるものはもうある。

# ぷ?もうある・・・権限がない?
ponsuke:~/environment/lambda_name (asynchronous) $ pip install requests
Defaulting to user installation because normal site-packages is not writeable
Requirement already satisfied: requests in /home/ec2-user/.local/lib/python3.6/site-packages (2.24.0)
Requirement already satisfied: idna<3,>=2.5 in /home/ec2-user/.local/lib/python3.6/site-packages (from requests) (2.10)
Requirement already satisfied: chardet<4,>=3.0.2 in /home/ec2-user/.local/lib/python3.6/site-packages (from requests) (3.0.4)
Requirement already satisfied: urllib3!=1.25.0,!=1.25.1,<1.26,>=1.21.1 in /home/ec2-user/.local/lib/python3.6/site-packages (from requests) (1.25.10)
Requirement already satisfied: certifi>=2017.4.17 in /home/ec2-user/.local/lib/python3.6/site-packages (from requests) (2020.6.20)

# --userオプションでDefaulting to user...は消えたけど状況変わらず
ponsuke:~/environment/lambda_name (asynchronous) $ pip install --user requests
Requirement already satisfied: requests in /home/ec2-user/.local/lib/python3.6/site-packages (2.24.0)
Requirement already satisfied: idna<3,>=2.5 in /home/ec2-user/.local/lib/python3.6/site-packages (from requests) (2.10)
Requirement already satisfied: chardet<4,>=3.0.2 in /home/ec2-user/.local/lib/python3.6/site-packages (from requests) (3.0.4)
Requirement already satisfied: urllib3!=1.25.0,!=1.25.1,<1.26,>=1.21.1 in /home/ec2-user/.local/lib/python3.6/site-packages (from requests) (1.25.10)
Requirement already satisfied: certifi>=2017.4.17 in /home/ec2-user/.local/lib/python3.6/site-packages (from requests) (2020.6.20)

# もしかして、Python2になってるとか?と思ってみたけど何も変わらず・・
ponsuke:~/environment/lambda_name (asynchronous) $ python3 -m pip install --user requests
Requirement already satisfied: requests in /home/ec2-user/.local/lib/python3.6/site-packages (2.24.0)
Requirement already satisfied: idna<3,>=2.5 in /home/ec2-user/.local/lib/python3.6/site-packages (from requests) (2.10)
Requirement already satisfied: chardet<4,>=3.0.2 in /home/ec2-user/.local/lib/python3.6/site-packages (from requests) (3.0.4)
Requirement already satisfied: urllib3!=1.25.0,!=1.25.1,<1.26,>=1.21.1 in /home/ec2-user/.local/lib/python3.6/site-packages (from requests) (1.25.10)
Requirement already satisfied: certifi>=2017.4.17 in /home/ec2-user/.local/lib/python3.6/site-packages (from requests) (2020.6.20)
1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?