Skip to content
Advertisement

Startup with Django runserver error

I am starting up with Python-Django in Ubuntu 18.04.

I have python3 installed.

python3 --version says Python 3.5.2

After installing Python, I installed Django as below:

sudo apt install python3-pip
pip3 install django

I also have Django installed.

django-admin --version says 2.0.5

In my project, startproject worked successfully, but when I am trying to run the following command inside my project:

python3 manage.py runserver

It gives following errors:

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'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "manage.py", line 14, in <module>
    ) from exc
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?

Following command also gives error:

python3 -c "import django; print(django.__path__)"

Error is:

python3 -c "import django; print(django.__path__)"
Traceback (most recent call last):
  File "<string>", line 1, in <module>
ImportError: No module named 'django'

which django gives blank output

echo $PYTHONPATH gives blank output

python3 -m django --version says /usr/local/bin/python3: No module named django

echo $PATH shows /home/shobhit/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin

What is the problem and what is the solution here?

Update

ls /usr/local/lib | grep python says

libpython3.5m.a

python3.5

python3.6

Advertisement

Answer

I think there is problem with the path.

I strongly recommend using virtual environment for all django development.

You can follow this process:

Install pip3

sudo apt-get install python3-pip

Install Virtual Environment for Python3

sudo pip3 install virtualenv

Create a project directory

mkdir ~/newproject
cd ~/newproject

Create a new virtual environemnt and activate it

virtualenv .venv
source .venv/bin/activate

Now Install Django

pip install django

and then create project and start it,

django-admin startproject my_project

cd my_project

python manage.py runserver

It should work this way.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement