1
votes

I am trying to deploy my Django Projects on Amazon AWS using Ubuntu 16.04. I am running python version 2.7.12 and Django 1.10.5. I created my virtualenv named venv and then activated it. I get this error when I try to run python manage.py runserver.

Traceback (most recent call last): File "manage.py", line 17, in "Couldn't import Django. Are you sure it's installed and " ImportError: Couldn't import Django. Are you sure it's installed and available on your PYTHONPATH environment variable? Did you forget to activate a virtual environment?

Then I realize Django might not be in my python path. So I added

export PYTHONPATH="/usr/local/lib/python2.7/dist-packages/django"

into my venv/bin/activate script. Now with the virtualenv activated I can go into python and type

import sys
sys.path
['', '/usr/local/lib/python2.7/dist-packages/django', '/home/ubuntu/TravelBuddy/venv/lib/python2.7', '/home/ubuntu/TravelBuddy/venv/lib/python2.7/plat-x86_64-linux-gnu', '/home/ubuntu/TravelBuddy/venv/lib/python2.7/lib-tk', '/home/ubuntu/TravelBuddy/venv/lib/python2.7/lib-old', '/home/ubuntu/TravelBuddy/venv/lib/python2.7/lib-dynload', '/usr/lib/python2.7', '/usr/lib/python2.7/plat-x86_64-linux-gnu', '/usr/lib/python2.7/lib-tk', '/home/ubuntu/TravelBuddy/venv/local/lib/python2.7/site-packages', '/home/ubuntu/TravelBuddy/venv/lib/python2.7/site-packages']

As you can see now django is indeed in my python path. I thought this was going to fix the problem but it didn't: it still says couldn't import Django. Now I am confused because when I deactivate my virtualenv and import Django it does work.

this is what prints out when I deactivate my virtualenv and do sys.path

['', '/usr/local/lib/python2.7/dist-packages/django', '/usr/lib/python2.7', '/usr/lib/python2.7/plat-x86_64-linux-gnu', '/usr/lib/python2.7/lib-tk', '/usr/lib/python2.7/lib-old', '/usr/lib/python2.7/lib-dynload', '/usr/local/lib/python2.7/dist-packages', '/usr/lib/python2.7/dist-packages']

2
Have you installed Django inside the virtual env, not just added it to the path?gallen
@Neelik Yes I installed Django inside the virtual envethan93
In my virtual environments that I have on server, all the packages are in the site-packages directory. I notice you have the path for django listed as '/usr/local/lib/python2.7/dist-packages/django' in your example when venv is activated. There is also, later in the list, '/home/ubuntu/TravelBuddy/venv/lib/python2.7/site-packages'. What happens if you move Django into the site-packages directory and attempt to import it?gallen
Run pip freeze inside virtualenv and post the output.narendra-choudhary

2 Answers

2
votes

ok I found out what the problem was. It turns out when I started my virtualenv I used sudo command but when I pip install my packages I didn't use the sudo command which caused a permission problem or some sort when installing the packages. So it made django not showing up on the path. When starting a virtual env never use the sudo command...

0
votes

1- install python3 brew install python3 2- install django pip3 install django