简体   繁体   English

ImportError:在AWS EC2实例上没有名为boto3的模块

[英]ImportError: No module named boto3 on AWS EC2 instance

I am running my AWS EC2 instance and when I try to run my web app, MyCoolApp.py I get the following error: 我正在运行我的AWS EC2实例,当我尝试运行Web应用程序MyCoolApp.py时,出现以下错误:

Traceback (most recent call last):   
File "MyCoolApp.py", line 9, in <module>
    import boto3
ImportError: No module named boto3

This is after I try to start my application which I do, and used to do before I made this latest change with the following command: 这是在我尝试启动我曾经做过的应用程序之后以及在使用以下命令进行此最新更改之前曾经做过的事情:

[ec2-user@ip-XXX-XXX-XXX-XXX CoolApp]$ sudo python MyCoolApp.py

As you can guess I am trying to import Boto3 for use and I do that in the my python application with the following code at the top of my program: 如您所料,我正在尝试导入Boto3以供使用,我在python应用程序中使用程序顶部的以下代码进行了此操作:

from werkzeug.security import check_password_hash, generate_password_hash
from datetime import datetime
import time, os, random, json
import urllib
import boto3

Lastly, because I know you are going to ask this, YES I already installed Boto3 and when I run pip freeze I can see it. 最后,因为我知道您会问这个,是的,我已经安装了Boto3,当我运行pip Frozen时,我可以看到它。 This is the output below: 这是下面的输出:

[ec2-user@ip-XXX-XXX-XXX-XXX CoolApp]$ pip freeze
alembic==0.9.9
aws-cfn-bootstrap==1.4
awscli==1.15.83
Babel==0.9.4
backports.ssl-match-hostname==3.4.0.2
blinker==1.4
boto==2.48.0
boto3==1.9.87
botocore==1.12.87
chardet==2.0.1
click==6.7
cloud-init==0.7.6
colorama==0.2.5
configobj==4.7.2
docutils==0.14
ecdsa==0.11
Flask==1.0.2
Flask-Login==0.4.1
Flask-Mail==0.9.1
Flask-Migrate==2.1.1
Flask-Mobility==0.1.1
Flask-SQLAlchemy==2.3.2
futures==3.2.0
hibagent==1.0.0
iniparse==0.3.1
itsdangerous==0.24
Jinja2==2.10
jmespath==0.9.3
jsonpatch==1.2
jsonpointer==1.0
kitchen==1.1.1
lockfile==0.8
Mako==1.0.7
MarkupSafe==1.0
mysql-connector-python==8.0.11
paramiko==1.15.1
PIL==1.1.6
pip-tools==3.3.2
ply==3.4
protobuf==3.5.2.post1
pyasn1==0.1.7
pycrypto==2.6.1
pycurl==7.19.0
pygpgme==0.3
pyliblzma==0.5.3
pystache==0.5.3
python-daemon==1.5.2
python-dateutil==2.7.3
python-editor==1.0.3
pyxattr==0.5.0
PyYAML==3.10
requests==1.2.3
rsa==3.4.1
s3transfer==0.1.13
simplejson==3.6.5
six==1.11.0
SQLAlchemy==1.2.7
urlgrabber==3.10
urllib3==1.24.1
virtualenv==15.1.0
Werkzeug==0.14.1
yum-metadata-parser==1.1.4

Now because I suspect that someone will be suggesting this I am just going to head this off now. 现在,因为我怀疑有人会建议这样做,所以我现在要解决这个问题。 I have these packages all installed globally to my --user. 我已将这些软件包全部全局安装到--user。 I am NOT using a virtual environment, yes I know I should be but I didn't think that I was going to need one because everything runs on the one server. 我没有使用虚拟环境,是的,我知道应该这样做,但是我不认为我需要一个虚拟环境,因为一切都在一个服务器上运行。 Yes I acknowledge that this might be a mistake and I use them everywhere else but thats not the point here if I could get some help with this error that would be great. 是的,我承认这可能是一个错误,我在其他地方都使用了它们,但这不是重点,如果我可以从这个错误中获得帮助的话,那将是很大的。 It starts on my local machine and works just fine it is just when I try to run it on my server it fails. 它在我的本地计算机上启动,并且可以正常运行,只是当我尝试在服务器上运行它时,它失败了。

Lastly, if this is due to a conflict with packages on my server it would be great if someone could help me figure out how to remove them, instructions would be appreciated as I am on AWS AMI Linux. 最后,如果这是由于与服务器上的软件包冲突而引起的,那么如果有人可以帮助我确定如何删除它们,那将是很好的选择,就像我在AWS AMI Linux上一样,请多多指教。

So with the details as provide in the question, we have the scenario where a Python script and the environment it will be executed at has: 因此,通过问题中提供的详细信息,我们得出了一个场景,其中有一个Python脚本及其执行环境:

  1. Dependency on some packages; 对某些软件包的依赖;
  2. No virtualenv is used, but the Python user install directory (as the ec2-user user) is used instead for simplicity for the installation of the script's dependencies; 没有使用virtualenv,但是为了简化脚本依赖项的安装,使用了Python用户安装目录(作为ec2-user用户)。
  3. The script however requires root privileges to run (due to the usage of low ports , with the elevated permission achieved via sudo (as the root user). 但是,该脚本需要运行root特权(由于使用了低端端口 ,而通过sudo (以root用户身份)获得了更高的权限。

Naturally, running a script as root while the dependencies were installed local to a user other than root will mean that none of the dependencies will actually be accessible by that script. 当然,运行一个脚本root ,而依赖被安装到本地以外的用户root将意味着没有任何依赖性实际上是通过脚本访问。 As the dependencies are installed under the site.USER_BASE for the ec2-user , in order for the script to be able to import its dependency while being executed under the root user, the location may be defined using the PYTHONUSERBASE environment variable. 由于依赖项已安装在ec2-usersite.USER_BASE下,为了使脚本能够在root用户下执行时导入其依赖项,可以使用PYTHONUSERBASE环境变量来定义位置。

To achieve that while using sudo , try: 要在使用sudo实现这一点,请尝试:

$ sudo PYTHONUSERBASE=/home/ec2-user/.local python MyCoolApp.py

If that works, great, we can move onto a more cleaner approach if desired, as this approach can potentially result in situations where the ec2-user user cannot remove files from their .local directory, as files like .pyc files will be written into there as the root. 如果可以,那么很好,我们可以根据需要采用更清洁的方法,因为这种方法可能会导致ec2-user用户无法从其.local目录中删除文件的情况,因为将.pyc文件之类的文件写入其中。有根。

A number of approaches can be done: 可以采用多种方法:

  1. Laziest manner: sudo pip , install packages globally; 最懒惰的方式: sudo pip ,全局安装软件包; comes with all the problems of having globally available python packages. 带有拥有全球可用的python软件包的所有问题。
  2. Use sudo pip --user instead - only local to the root user. 请改用sudo pip --user仅对root用户本地。
  3. A number of approaches at the OS level that is available on Linux may be attempted 可以尝试在Linux上可用的多种操作系统级别的方法
  4. Use virtualenv, and start the Python script (with sudo as root ) using the env/bin/python created by virtualenv. 使用virtualenv,并使用由virtualenv创建的env/bin/python启动Python脚本(以sudo作为root )。 Example (uses /var/tmp/env as the base for demonstration): 示例(使用/var/tmp/env作为演示基础):
$ virtualenv /var/tmp/env
Using base prefix '/usr'
New python executable in /var/tmp/env/bin/python3.6
Also creating executable in /var/tmp/env/bin/python
Installing setuptools, pip, wheel...done.
$ . /var/tmp/env/bin/activate
(env) $ pip install -r requirements.txt
...
(env) $ deactivate
$ sudo /var/tmp/env/bin/python MyCoolApp.py

Finally, it may be beneficial to drop root privileges back to the original user (or another limited user) after the port has been opened, to improve the security situation of the runtime environment. 最后,在打开端口后, 将root用户特权放回原始用户 (或另一个受限制的用户)可能是有益的,以改善运行时环境的安全性。

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

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