【问题标题】:AWS EC2 Instance appears to be running wrong python version from User DataAWS EC2 实例似乎从用户数据运行错误的 python 版本
【发布时间】:2020-01-17 12:37:31
【问题描述】:

我在 EC2 上运行 python 脚本,当我通过终端连接并运行“python /home/ec2-user/lambda_function.py”时,它运行良好。但是,当我尝试通过用户数据运行它时,脚本会在启动时执行:

MIME-Version: 1.0

--//
Content-Type: text/cloud-config; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment; filename="cloud-config.txt"

#cloud-config
cloud_final_modules:
- [scripts-user, always]

--//
Content-Type: text/x-shellscript; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment; filename="userdata.txt"

#!/bin/bash
echo "alias python='/usr/bin/python3.7'" >> /home/ec2-user/.bashrc
. ~/.bashrc
python /home/ec2-user/lambda_function.py

--//

我收到以下错误:

Traceback (most recent call last):
  File "/home/ec2-user/lambda_function.py", line 1, in <module>
    import pandas as pd
ModuleNotFoundError: No module named 'pandas'
Sep 16 15:54:17 cloud-init[3326]: util.py[WARNING]: Failed running /var/lib/cloud/instance/scripts/part-001 [1]
Traceback (most recent call last):
  File "/home/ec2-user/lambda_function.py", line 1, in <module>
    import pandas as pd
ImportError: No module named pandas
Sep 16 15:54:17 cloud-init[3326]: util.py[WARNING]: Failed running /var/lib/cloud/instance/scripts/userdata.txt [1]
Sep 16 15:54:17 cloud-init[3326]: cc_scripts_user.py[WARNING]: Failed to run module scripts-user (scripts in /var/lib/cloud/instance/scripts)
Sep 16 15:54:17 cloud-init[3326]: util.py[WARNING]: Running module scripts-user (<module 'cloudinit.config.cc_scripts_user' from '/usr/lib/python2.7/site-packages/cloudinit/config/cc_scripts_user.pyc'>) failed

看起来好像它正在尝试使用 python 2 运行,因为当我使用 python 终端时肯定安装了 pandas

Python 3.7.4 (default, Jul 30 2019, 19:56:38)
[GCC 7.3.1 20180712 (Red Hat 7.3.1-6)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import pandas
>>> exit()
[ ~]$ python2
Python 2.7.16 (default, Jul 19 2019, 23:05:17)
[GCC 7.3.1 20180712 (Red Hat 7.3.1-6)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import pandas
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named pandas

任何帮助将不胜感激!

【问题讨论】:

    标签: python python-3.x pandas amazon-web-services amazon-ec2


    【解决方案1】:

    根据 AWS Linux 中的配置,cloud-init 在根用户账户中运行用户数据脚本,因此当您显式修改 /home/ec2-user/.bashrc,然后源 ~/.bashrc 时,您指的是两个不同的 .bashrc 文件。

    我建议明确采购ec2-user 文件夹中的.bashrc 文件。或者,您可以在脚本顶部添加类似的内容,以将上下文切换到 ec2-user 用户(如果它以 root 身份运行):

    #!/bin/bash
    
    if [ $UID -eq 0 ]; then
      sudo chmod 777 "$0"
      exec su ec2-user "$0"
    fi
    

    【讨论】:

      【解决方案2】:

      别名命令可能不起作用。如this link 中所述,您可能需要在别名命令之前插入shopt -s expand_aliases,以便 bash 脚本能够正确运行。

      【讨论】:

        猜你喜欢
        • 2021-07-04
        • 2016-12-24
        • 2022-01-23
        • 2017-12-12
        • 2020-09-29
        • 2018-08-30
        • 1970-01-01
        • 2020-01-13
        • 1970-01-01
        相关资源
        最近更新 更多