24

How to run manage.py from AWS EB (Elastic Beanstalk) Linux instance?

If I run it from '/opt/python/current/app', it shows the below exception.

Traceback (most recent call last):
  File "./manage.py", line 8, in <module>
    from django.core.management import execute_from_command_line
ImportError: No module named django.core.management

I think it's related with virtualenv. Any hints?

lmiguelvargasf
  • 63,191
  • 45
  • 217
  • 228
Wonil
  • 6,364
  • 2
  • 37
  • 55
  • 1
    You don't have django available for python interpreter used. Search the docs for more info about where the dependencies are installed. Are you sure they're installed at all? – Krzysztof Szularz Nov 15 '13 at 09:14
  • Yes. it's AWS EB Python 2.7 environment with Django 1.5.5 installed. In fact, Django web application itself running well and I can see it from browser. But, I want to run custom command from Linux shell through SSH. In this case, I can see the error like the above. – Wonil Nov 15 '13 at 12:02
  • Try to figure out the HTTP server does initialize the environments/setup paths then. – Krzysztof Szularz Nov 15 '13 at 15:05

4 Answers4

93

How to run manage.py from AWS Elastic Beanstalk AMI.

  • SSH login to Linux (eb ssh)
    • (optional may need to run sudo su - to have proper permissions)
  • source /opt/python/run/venv/bin/activate
  • source /opt/python/current/env
  • cd /opt/python/current/app
  • python manage.py <commands>

Or, you can run command as like the below:

  • cd /opt/python/current/app
  • /opt/python/run/venv/bin/python manage.py <command>
Francisco
  • 10,918
  • 6
  • 34
  • 45
Wonil
  • 6,364
  • 2
  • 37
  • 55
17

With the new version of Python paths seem to have changed.

  • The app is in /var/app/current
  • The virtual environment is in /var/app/venv/[KEY]

So the instructions are:

  1. SSH to the machine using eb shh
  2. Check the path of your environment with ls /var/app/venv/. The only folder should be the [KEY] for the next step
  3. Activate the environment with source /var/app/venv/[KEY]/bin/activate
  4. Execute the command python3 /var/app/current/manage.py <command>

Of course Amazon can change it anytime.

djvg
  • 11,722
  • 5
  • 72
  • 103
borjab
  • 11,149
  • 6
  • 71
  • 98
4

As of February 2022 the solution is as follows:

$ eb ssh
$ sudo su -
$ export $(cat /opt/elasticbeanstalk/deployment/env | xargs)
$ source /var/app/venv/*/bin/activate
$ python3 /var/app/current/manage.py <command name>

$ export $(cat /opt/elasticbeanstalk/deployment/env | xargs) is needed to import your environment variables if you have a database connection (most likely you will)

Tintin
  • 547
  • 5
  • 17
  • This is confirmed working as of TODAY; can we go a step further? ``PSQL`` does not seem to work.. – Snerd Jul 13 '22 at 15:18
2

TL;DR

This answer assumes you have installed EB CLI. Follow these steps:

  1. Connect to your running instance using ssh.
eb ssh <environment-name>
  1. Once you are inside your environment, load the environment variables (this is important for database configuration)
. /opt/python/current/env

If you wish you can see the environment variables using printenv.

  1. Activate your virtual environment
source /opt/python/run/venv/bin/activate
  1. Navigate to your project directory (this will depend on your latest deployment, so use the number of your latest deployment instead of XX)
cd /opt/python/bundle/XX/app/
  1. Run the command you wish:
python manage.py <command_name>

Running example

Asumming that your environment name is my-env, your latest deployment number is 13, and you want to run the shell command:

eb ssh my-env # 1
. /opt/python/current/env # 2
source /opt/python/run/venv/bin/activate # 3
cd /opt/python/bundle/13/app/ # 4
python manage.py shell # 5
lmiguelvargasf
  • 63,191
  • 45
  • 217
  • 228
  • 1
    Hey this doesnt seem to be working for me after ssh - ". /opt/python/current/env" doesnt exists for me. Infact even opt/python doesnt. Please help! – Aditya Nambiar Jun 05 '20 at 09:20