3N1GM4
3N1GM4

Reputation: 33

Django : python manage.py runserver (multiple errors)

I started using Django recently for my first web app following a tutorial on youtube, every thing went fine until this command : $ python manage.py runserver meaning i was able to create a virtual environment and create a project using : $ python3 -m django startproject <projectname>.

Here's what my manage.py looks like:

"""Django's command-line utility for administrative tasks."""
import os
import sys


def main():
    """Run administrative tasks."""
    os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'django_blog.settings')
    try:
        from django.core.management import execute_from_command_line
    except ImportError as exc:
        raise 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?"
        )
    execute_from_command_line(sys.argv)


if __name__ == '__main__':
    main()

Here are some of my different attempts and errors:

$ python manage.py runserver Error:

  File "manage.py", line 22, in <module>
    main()
  File "manage.py", line 14, in main
    "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?

Attempt n°1:

$ python pip install django Error: python: can't open file 'pip': [Errno 2] No such file or directory $ python -m pip install gekko Still gives the same error as above after $ python pip install django

Attempt n°2:

$ python3 manage.py runserver and $ python3.6 manage.py runserver both gives the same error:

File "/usr/local/lib/python3.6/sqlite3/dbapi2.py", line 27, in <module>
from _sqlite3 import *
ModuleNotFoundError: No module named '_sqlite3'

Tried using: $ pip3 install pysqlite3 $ sudo apt-get install libsqlite3-dev then inside ~/Downloads/Python-3.6.2$ ./configure --enable-loadable-sqlite-extension && make && sudo make install

still the problem persisted.

Attempt n°3:

$ python3.5 manage.py runserver

Traceback (most recent call last):
  File "manage.py", line 11, in main
    from django.core.management import execute_from_command_line
ImportError: No module named 'django'

During handling of the above exception, another exception occurred:

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

This is the most recent error:

from django.urls import path
ImportError: cannot import name path

Here is my urls.py

from django.urls import path

urlpatterns = [
    path('admin/', admin.site.urls),
]

Here is what i get when i run the sudo tree command on linux

thalagalage@3n1gm4:~/chamika/Python/DjangoBlog$ sudo tree
.
├── django_blog
│   ├── django_blog
│   │   ├── asgi.py
│   │   ├── __init__.py
│   │   ├── __init__.pyc
│   │   ├── __pycache__
│   │   │   ├── __init__.cpython-36.pyc
│   │   │   ├── settings.cpython-36.pyc
│   │   │   └── urls.cpython-36.pyc
│   │   ├── settings.py
│   │   ├── settings.pyc
│   │   ├── urls.py
│   │   ├── urls.pyc
│   │   └── wsgi.py
│   ├── manage.py
│   └── manage.sublime-workspace
└── django_env
    ├── bin
    │   ├── activate
    │   ├── activate.csh
    │   ├── activate.fish
    │   ├── activate.ps1
    │   ├── activate_this.py
    │   ├── activate.xsh
    │   ├── django-admin
    │   ├── django-admin.py
    │   ├── easy_install
    │   ├── easy_install3
    │   ├── easy_install-3.6
    │   ├── easy_install3.6
    │   ├── pip
    │   ├── pip3
    │   ├── pip-3.6
    │   ├── pip3.6
    │   ├── __pycache__
    │   │   └── django-admin.cpython-36.pyc
    │   ├── python -> /usr/bin/python3
    │   ├── python3 -> python
    │   ├── python3.6 -> python
    │   ├── sqlformat
    │   ├── wheel
    │   ├── wheel3
    │   ├── wheel-3.6
    │   └── wheel3.6

Below are the step by step commands i use in order to get in to my virtual environment, until i get stuck with the manage.py runserver command which throws the ModuleNotFoundError: No module named '_sqlite3'.

thalagalage@3n1gm4:~/chamika/Python/DjangoBlog$ ll
total 16
drwxr-xr-x  4 thalagalage thalagalage 4096 sept.  5 19:02 ./
drwxrwxr-x 11 thalagalage thalagalage 4096 sept.  5 18:13 ../
drwxr-xr-x  3 thalagalage thalagalage 4096 sept.  5 19:32 django_blog/
drwxr-xr-x  4 thalagalage thalagalage 4096 sept.  5 18:03 django_env/
thalagalage@3n1gm4:~/chamika/Python/DjangoBlog$ source django_env/bin/activate
(django_env) thalagalage@3n1gm4:~/chamika/Python/DjangoBlog$ python3 -m django --version
3.1.1
(django_env) thalagalage@3n1gm4:~/chamika/Python/DjangoBlog$ cd django_blog
(django_env) thalagalage@3n1gm4:~/chamika/Python/DjangoBlog/django_blog$ ll
total 32
drwxr-xr-x 3 thalagalage thalagalage  4096 sept.  5 19:32 ./
drwxr-xr-x 4 thalagalage thalagalage  4096 sept.  5 19:02 ../
drwxr-xr-x 3 thalagalage thalagalage  4096 sept.  6 01:02 django_blog/
(django_env) thalagalage@3n1gm4:~/chamika/Python/DjangoBlog/django_blog$ python3 manage.py runserver

Thank you for your help!

Upvotes: 1

Views: 4692

Answers (2)

Mostafa Ghadimi
Mostafa Ghadimi

Reputation: 6766

The first thing you should do is to create a virtual environment. Virtual environment is a place where your packages would be placed, For example Django is one of the modules. In order to create a virtual environment you have multiple choices like pipenv, virtualenv, etc.

A) Virtualenv

  1. You can install virtualenv using the following command:
pip3 install virtualenv
  1. Create a new virtualenv. The common virtualenv names are venv, env, etc.
virtualenv --python=python3 --no-site-package <virtualenv-name>
#example:
virtualenv --python=python3 --no-site-package venv
  1. Activate the virtualenv. In current directory open terminal (in linux and Mac OSX) or powershell (in windows) and execute the following command:
source <virtualenv-name>/bin/activate
  1. Install the packages you want. The name of your virtualenv should be prepended to the shell in paranthesis.
(<virtualenv-name>) pip3 install django
  1. Finally run the server successfully.
(<virtualenv-name>) python3 manage.py runserver

B) Pipenv

  1. Installing pipenv.
pip3 install pipenv

or in linux you can execute the following lines:

sudo apt install software-properties-common python-software-properties
sudo add-apt-repository ppa:pypa/ppa
sudo apt update
sudo apt install pipenv
  1. To create new environment using pipenv you have to enter the following line in your terminal.
pipenv --python 3.6
  1. Install the packages you need.
pipenv install django
  1. Run the server.
python3 manage.py runserver

PS: If you still have the problem running the project, it's better to create a new project from scratch:

  1. Create a new virtualenv as described above.

  2. Activate the virtualenv.

  3. Install Django.

(venv) pip3 install django
  1. Create new django project.
(venv) django-admin startproject <project-name>
  1. Change directory to the django project directory and then run the server.
(venv) cd <project-name>
(venv) python3 manage.py makemigrations
(venv) python3 manage.py migrate
(venv) python3 manage.py runserver

Upvotes: 1

Naterz
Naterz

Reputation: 437

maybe use a venv like pipenv for your project

pip3 install pipenv

pipenv install django install the necessary packages u want to use

pipenv shell to activate the venv

py manage.py runserver

Upvotes: 1

Related Questions