daveoncode daveoncode - 1 month ago 30
Python Question

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 :)

Answer

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