【发布时间】:2020-09-05 22:56:29
【问题描述】:
我最近根据 youtube 上的教程开始将 Django 用于我的第一个 Web 应用程序,在此命令之前一切都很好:$ python manage.py runserver 意味着我能够创建一个虚拟环境并使用:$ python3 -m django startproject <projectname> 创建一个项目。
这是我的 manage.py 的样子:
"""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()
以下是我的一些不同尝试和错误:
$ python manage.py runserver
错误:
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?
第 1 次尝试:
$ python pip install django 错误:
python: can't open file 'pip': [Errno 2] No such file or directory
$ python -m pip install gekko 在$ python pip install django 之后仍然给出与上面相同的错误
第 2 次尝试:
$ python3 manage.py runserver 和 $ python3.6 manage.py runserver 都给出相同的错误:
File "/usr/local/lib/python3.6/sqlite3/dbapi2.py", line 27, in <module>
from _sqlite3 import *
ModuleNotFoundError: No module named '_sqlite3'
尝试使用:
$ pip3 install pysqlite3
$ sudo apt-get install libsqlite3-dev 然后在里面
~/Downloads/Python-3.6.2$ ./configure --enable-loadable-sqlite-extension && make && sudo make install
问题仍然存在。
第 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?
这是最近的错误:
from django.urls import path
ImportError: cannot import name path
这是我的 urls.py
from django.urls import path
urlpatterns = [
path('admin/', admin.site.urls),
]
这是我在 linux 上运行 sudo tree 命令时得到的结果
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
以下是我为了进入我的虚拟环境而使用的分步命令,直到我被抛出 ModuleNotFoundError: No module named '_sqlite3' 的 manage.py runserver 命令卡住。
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
感谢您的帮助!
【问题讨论】:
-
你安装的包不是
python pip,而是pip,所以pip install django,而不是python pip install django。
标签: python python-3.x django ubuntu