Reputation: 33
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
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.
virtualenv
using the following command:pip3 install virtualenv
venv
, env
, etc.virtualenv --python=python3 --no-site-package <virtualenv-name>
#example:
virtualenv --python=python3 --no-site-package venv
source <virtualenv-name>/bin/activate
(<virtualenv-name>) pip3 install django
(<virtualenv-name>) python3 manage.py runserver
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
pipenv
you have to enter the following line in your terminal.pipenv --python 3.6
pipenv install django
python3 manage.py runserver
PS: If you still have the problem running the project, it's better to create a new project from scratch:
Create a new virtualenv as described above.
Activate the virtualenv.
Install Django.
(venv) pip3 install django
(venv) django-admin startproject <project-name>
(venv) cd <project-name>
(venv) python3 manage.py makemigrations
(venv) python3 manage.py migrate
(venv) python3 manage.py runserver
Upvotes: 1
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