awslabs / awslabs/aws-ec2rescue-linux
Missing Python module 'requests' problem with virtual env in EC2 Linux instance
- Dominant language
- Python
- Stars
- 190
- Forks
- 57
- Avg merge
- 30m
- Merged PRs (30d)
- 2
Description
I encountered an issue when using **v1.1.7** of EC2Rescue for Linux to troubleshoot one of my EC2 instances, and I hope this helps others facing a similar problem.
```
ERROR: Missing Python module 'requests'.
Please install this module and rerun ec2rl.
```
The instance comes with a pre-installed Python, and you cannot install global libraries by default. The system warns you about creating a virtual environment (venv) to handle dependencies. After checking the ec2rl bash script, I created the environment under the EC2Rescue installation.
**Steps for the issue:**
1. Created a venv and installed requests module.
2. Verified the module is installed by checking the path and with the python command.
3. Verified the correct Python interpreter was being used by adding debug lines to both the bash script and the Python code:
```
import sys
print("Using Python interpreter:", sys.executable)
```
4. Verified that the `requests` library existed and that my virtual environment's site-packages was included in `sys.path`, but the error was still being thrown in `ec2rlcore/prediag.py`.
**Problem:**
Even though `sys.path` was set correctly and included my virtual environment's path, it was listed after the default installation paths, so Python did not prioritize it.
**Solution:**
To resolve the issue, move your virtual environment's `site-packages` directory to the first position in `sys.path`:
```
sys.path.insert(0, '/path/to/ec2rescue/ec2rl-VERSION/python/lib/python-VERSION/site-packages')
print("sys.path after modification:", sys.path)
```
Contributor guide
Assessment
This issue has not been assessed yet.