Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

django-admin.py and python path on EC2 Amazon Beanstalk

I deployed my django app on Elastic Benastalk, but my commands are failing and I think that the problem is that django-admin.py is not in the $PYTHONPATH, I would like to add my app to the python path, but I don't know what is the exact path on the EC2 instance. Actually I found it under: /opt/python/bundle/3/app (I used "find" command via SSH)… but is that a fixed and reliable path?? ps: WTF is that "3"?? (for sure not the version or the count of my deploys ^_^)

UPDATE:

if I cd to /opt/python/bundle/3/app/myappname and run:

python manage.py shell

I get:

  File "manage.py", line 3, in <module>
    from django.core.management import execute_from_command_line
ImportError: No module named django.core.management

SOLVED!

Amazon Beanstalk uses a virtualenv (like I do myself locally), in order to activate it you have to:

source /opt/python/run/venv/bin/activate
cd /opt/python/current/app
python manage.py commandname

BUT, in order to use custom management commands (that is why I need to access to the django shell on my EC2 instance), you have to add your application to the python path and also all the environment variables used by your app, so I did:

vi /home/ec2-user/.bash_profile

and added:

export PYTHONPATH=$PYTHONPATH:/opt/python/current/app

and my env variables... now it works! :)

In order to automatically activate the virtualenv and to be able to use the django shell as soon as logged via ssh, is it possible to add:

source /opt/python/run/venv/bin/activate
cd /opt/python/current/app

in the .bash_profile :)

like image 369
daveoncode Avatar asked Feb 13 '14 20:02

daveoncode


People also ask

How do I access EC2 instance of Elastic Beanstalk?

Right-click the instance ID for the Amazon EC2 instance running in your environment's load balancer, and then select Connect from the context menu. Make a note of the instance's public DNS address on the Description tab. Connect to an instance running Linux by using the SSH client of your choice, and then type ssh -i .


2 Answers

You can add an option_name if you need to change PYTHONPATH, or set any environment variable in general.

In your .ebextensions/myapp-env.config file (or whatever your *.config is named):

option_settings:
  - option_name: PYTHONPATH
    value: /opt/python/ondeck/app/myapp:$PYTHONPATH
like image 188
Banjer Avatar answered Sep 28 '22 08:09

Banjer


The message:

  File "manage.py", line 3, in <module>
    from django.core.management import execute_from_command_line
ImportError: No module named django.core.management

is not because your app is not in the PYTHONPATH but rather because it can't find your django application at all. Meaning your site-packages directory is not in the PYTHONPATH.

Try to find the site-packages directory in your server and that should in the PYTHONPATH. I haven't deployed a python app with Elastic Beanstalk but I believe it maybe using a python virtual environment. In which case you need to source your virtual environment before running python ./manage shell

like image 29
Rico Avatar answered Sep 28 '22 08:09

Rico