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 
    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:\yuanma\django\myblog')
sys.path.append('D:\Python\Lib\site-packages\django\bin')

sys.path.append('E:\yuanma\django\myblog') 是django项目的路径
sys.path.append('D:\Python\Lib\site-packages\django\bin') 是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:\Python\lib\threading.py", line 917, in _bootstrap_inner
    self.run()
  File "D:\Python\lib\threading.py", line 865, in run
    self._target(*self._args, **self._kwargs)
  File "D:\Python\lib\site-packages\django\utils\autoreload.py", line 54, in wrapper
    fn(*args, **kwargs)
  File "D:\Python\lib\site-packages\django\core\management\commands\runserver.py", line 137, in inner_run
    handler = self.get_handler(*args, **options)
  File "D:\Python\lib\site-packages\django\contrib\staticfiles\management\commands\runserver.py", line 27, in get_handler
    handler = super().get_handler(*args, **options)
  File "D:\Python\lib\site-packages\django\core\management\commands\runserver.py", line 64, in get_handler
    return get_internal_wsgi_application()
  File "D:\Python\lib\site-packages\django\core\servers\basehttp.py", line 45, in get_internal_wsgi_application
    return import_string(app_path)
  File "D:\Python\lib\site-packages\django\utils\module_loading.py", line 17, in import_string
    module = import_module(module_path)
  File "D:\Python\lib\importlib\__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "", line 1006, in _gcd_import
  File "", line 983, in _find_and_load
  File "", line 967, in _find_and_load_unlocked
  File "", line 677, in _load_unlocked
  File "", line 728, in exec_module
  File "", line 219, in _call_with_frames_removed
  File "E:\yuanma\django\myblog\myblog\wsgi.py", line 18, in 
    sys.path.append('E:\yuanma\django\myblog')
NameError: name 'sys' is not defined

至此,解决问题。访问:127.0.0.1:8000

django启动服务器报错 ModuleNotFoundError: No module named 'django'_第1张图片

 

你可能感兴趣的:(django)