简体   繁体   English

在 EC2 实例 (AWS) 上使用 Bash 脚本导入错误加载模块

[英]Import Error Loading Module using Bash Script on EC2 Instance (AWS)

I am trying to run a bash script through the user_data argument of the spot_fleet_request method.我正在尝试通过 spot_fleet_request 方法的 user_data 参数运行 bash 脚本。 The script executes all the installations and connects to the filesystem, but when it runs the python file I need to run it gives me the following error:该脚本执行所有安装并连接到文件系统,但是当它运行 python 文件时,我需要运行它给我以下错误:

Traceback (most recent call last):
  File "/home/ec2-user/efs/dir/create_lvl_output.py", line 8, in <module>
    from modeling import generators
ImportError: No module named modeling

Where modeling is a folder with a script I am importing "modeling/generators.py". modeling是一个带有脚本的文件夹,我正在导入“modeling/generators.py”。 I have attempted to assign the directory to the PYTHONPATH environmental variable as suggested in this post , here is my code:我已尝试按照本文中的建议将目录分配给 PYTHONPATH 环境变量,这是我的代码:

#!/bin/bash
export PYTHONPATH=/home/ec2-user/efs/Day-Trader/day_trader/
echo $PYTHONPATH

...mount efs
...installs

python /home/ec2-user/efs/dir/create_lvl_output.py > levelset.txt

but it continues to fail with the same error.但它继续失败并出现同样的错误。 I tried this solution with each of the following variations and they all failed to solve the import issue.我使用以下每种变体尝试了此解决方案,但它们都未能解决导入问题。

echo "export PYTHONPATH=/home/ec2-user/efs/dir/" >> ~/.bashrc
. ~/.bashrc      #--- failed to import, set PYTHONPATH variable correctly     

echo "export PYTHONPATH=/home/ec2-user/efs/dir/" >> /home/ec2-user/.bashrc
. /home/ec2-user/.bashrc   #--- failed to import, set PYTHONPATH variable correctly but got following error as well: /home/ec2-user/.bashrc: line 2: /root/.dlamirc: No such file or directory

I also made sure that the script actually worked.我还确保脚本确实有效。 If I copy and paste the script directly into the linux terminal when I've logged in a ec2-user (ie $HOME=/home/ec2-user/) and run perfectly and produced all the right output.如果我在登录 ec2 用户(即 $HOME=/home/ec2-user/)后将脚本直接复制并粘贴到 linux 终端并完美运行并生成所有正确的 output。 So I thought that if I changed the user at the top of the bash script as suggested in this post it might work but the following commands still ran everything that followed as root:所以我想,如果我按照这篇文章中的建议更改 bash 脚本顶部的用户,它可能会起作用,但以下命令仍然以 root 身份运行以下所有内容:

su -l "ec2-user" , su - ec2-user su -l "ec2-user" , su - ec2-user

and I tried running the python script using我尝试使用运行 python 脚本

su ec2-user -c 'python /home/ec2-user/efs/dir/create_lvl_output.py'

but it didn't work either.但它也没有用。 I'm out of ideas.我没主意了。 Please help.请帮忙。

I think the recommended method is of course to try to package your code as a proper module and install it prior with pip, either at the user/system level or a pythonenv style environment.我认为推荐的方法当然是尝试将您的代码 package 作为适当的模块,并在用户/系统级别或 pythonenv 样式环境中使用 pip 之前安装它。 This of course, can be highly difficult and time consuming depending on how much code you have to change to accommodate this, and its recommended that you try to do this at the beginning of making projects to avoid this headache.当然,这可能非常困难和耗时,具体取决于您必须更改多少代码以适应这种情况,并且建议您在开始制作项目时尝试这样做以避免这种头痛。

Short of the doing the nontrivial amount of work that would be though, I actually have encountered this specific error you're having on Amazon's EC2 LTS ubuntu.虽然没有做大量的工作,但我实际上遇到了您在 Amazon 的 EC2 LTS ubuntu 上遇到的这个特定错误。 No idea what causes it, but its like python just side steps the normal environment settings, and doesn't work as you expect.不知道是什么原因造成的,但它就像 python 只是在正常环境设置的旁边步骤,并且不能按您预期的那样工作。 In my case, I just went for the dirty fix of:就我而言,我只是进行了以下肮脏的修复:

import os

os.environ['PYTHONPATH'] = '{}:{}'.format('my/addition/to/pythonpath', os.environ.get('PYTHONPATH', ''))
print(os.environ['PYTHONPATH'])
# prints my/addition/to/pythonpath:existing/things/on/pythonpath

so for your usecase, try:因此,对于您的用例,请尝试:

import os

os.environ['PYTHONPATH'] = '{}:{}'.format('/home/ec2-user/efs/Day-Trader/day_trader/', os.environ.get('PYTHONPATH', ''))
print(os.environ['PYTHONPATH'])
# prints /home/ec2-user/efs/Day-Trader/day_trader/:

you can of course do some pretty nasty things with this, such as:你当然可以用这个做一些非常讨厌的事情,例如:

python -c "import os; os.environ['PYTHONPATH'] = '/my/addition/to/pythonpath'; import mymodule; mymodule.doThing()'

if for some reason you dislike everyone who has the displeasure of working with this.如果出于某种原因,您不喜欢所有对此感到不满的人。 Jokes aside, I really did use things like this in production, if you every figure out what the hell causes these env issues, I'd be interested to know even though I no longer work on the machines that had these issues.撇开笑话不谈,我确实在生产中使用过这样的东西,如果你们都弄清楚到底是什么导致了这些环境问题,即使我不再在有这些问题的机器上工作,我也很想知道。

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

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