I am trying to run a bash script through the user_data argument of the spot_fleet_request method. 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:
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".
I have attempted to assign the directory to the PYTHONPATH environmental variable as suggested in this post, here is my code:
#!/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 (i.e. $HOME=/home/ec2-user/) and run perfectly and produced all the right 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:
su -l "ec2-user", su - ec2-user
and I tried running the python script using
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.