我是靠谱客的博主 老迟到灰狼,最近开发中收集的这篇文章主要介绍django启动服务器报错 ModuleNotFoundError: No module named 'django',觉得挺不错的,现在分享给大家,希望可以做个参考。
概述
环境:python3.6+django2.2.1
运行
python manage.py runserver
报错如下
Traceback (most recent call last):
File "manage.py", line 10, in main
from django.core.management import execute_from_command_line
ModuleNotFoundError: No module named 'django'
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "manage.py", line 21, in <module>
main()
File "manage.py", line 16, in main
) 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?
一开始以为是环境变量的问题,查找了path的环境变量,python和django的都没错,依然解决不了。
再看提示 'ModuleNotFoundError: No module named 'django'。
解决方法为:
在wsgi.py文件中加入
sys.path.append('E:yuanmadjangomyblog')
sys.path.append('D:PythonLibsite-packagesdjangobin')
sys.path.append('E:yuanmadjangomyblog') 是django项目的路径
sys.path.append('D:PythonLibsite-packagesdjangobin') 是site-packages的路径
注意:需要wsgi.py文件中导入sys模块
import sys
否则会报如下错误:
Watching for file changes with StatReloader
Performing system checks...
System check identified no issues (0 silenced).
You have 17 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): admin, auth, contenttypes, sessions.
Run 'python manage.py migrate' to apply them.
May 02, 2019 - 16:38:39
Django version 2.2.1, using settings 'myblog.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CTRL-BREAK.
Exception in thread django-main-thread:
Traceback (most recent call last):
File "D:Pythonlibthreading.py", line 917, in _bootstrap_inner
self.run()
File "D:Pythonlibthreading.py", line 865, in run
self._target(*self._args, **self._kwargs)
File "D:Pythonlibsite-packagesdjangoutilsautoreload.py", line 54, in wrapper
fn(*args, **kwargs)
File "D:Pythonlibsite-packagesdjangocoremanagementcommandsrunserver.py", line 137, in inner_run
handler = self.get_handler(*args, **options)
File "D:Pythonlibsite-packagesdjangocontribstaticfilesmanagementcommandsrunserver.py", line 27, in get_handler
handler = super().get_handler(*args, **options)
File "D:Pythonlibsite-packagesdjangocoremanagementcommandsrunserver.py", line 64, in get_handler
return get_internal_wsgi_application()
File "D:Pythonlibsite-packagesdjangocoreserversbasehttp.py", line 45, in get_internal_wsgi_application
return import_string(app_path)
File "D:Pythonlibsite-packagesdjangoutilsmodule_loading.py", line 17, in import_string
module = import_module(module_path)
File "D:Pythonlibimportlib__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
File "<frozen importlib._bootstrap>", line 983, in _find_and_load
File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 728, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "E:yuanmadjangomyblogmyblogwsgi.py", line 18, in <module>
sys.path.append('E:yuanmadjangomyblog')
NameError: name 'sys' is not defined
至此,解决问题。访问:127.0.0.1:8000
最后
以上就是老迟到灰狼为你收集整理的django启动服务器报错 ModuleNotFoundError: No module named 'django'的全部内容,希望文章能够帮你解决django启动服务器报错 ModuleNotFoundError: No module named 'django'所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复