Run manage.py from AWS EB Linux instance
Asked Answered
F

4

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?

Fuegian answered 15/11, 2013 at 9:12 Comment(3)
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?Kyd
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.Fuegian
Try to figure out the HTTP server does initialize the environments/setup paths then.Kyd
F
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>
Fuegian answered 19/11, 2013 at 11:21 Comment(6)
Even more compact: /opt/python/run/venv/bin/python /opt/python/current/app/manage.py <command>Stroup
It gives Permission deniedLeonardoleoncavallo
@RoNiT, you can use "sudo su -" to get root access, then you'll have the permission.Doubleedged
This is not the best answer. The whole point of EB is to automate deployment and not have to SSH into a machine.Gramps
@Gramps - its very common with django systems to use the manage.py shell command to interrogate data using the ORM rather than raw SQL, when its to do with data rather than changes to the filesystem this is completely finePintsize
This is unfortunately no longer valid response in the new Python3.7 and Amazon Linux 2 platform.Nudicaul
P
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.

Paulita answered 29/10, 2020 at 22:5 Comment(4)
Wow - why was this so hard to find - thanks very much!Equivalence
can you please clarify what [KEY] is above? it's not clear to me...Subsidize
@Subsidize As described above KEY is the name of the only folder in /var/app/vent/Paulita
You can replace the [key] with * and this worksCadastre
M
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)

Metonym answered 9/2, 2022 at 7:31 Comment(1)
This is confirmed working as of TODAY; can we go a step further? PSQL does not seem to work..Standish
A
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
Attack answered 21/11, 2019 at 18:54 Comment(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!Caprine

© 2022 - 2024 — McMap. All rights reserved.