ModuleNotFoundError:没有名为'mysite'的模块

时间:2017-12-31 15:52:00

标签: python mysql django

我在我的Windows机器上安装了Python,Django和MySQL。我正在尝试通过从名为'zibit'的目录运行以下内容来验证我的数据库连接:

> myvenv\Scripts\activate
> set DJANGO_SETTINGS_MODULE=mysite.settings
> django-admin dbshell

运行最后一个命令后,我收到此错误:

ModuleNotFoundError: No module named 'mysite'

我在网上看到一些相关帖子后尝试将我的wsgi.py文件更新为以下内容:

import os

from django.core.wsgi import get_wsgi_application

import sys
path = 'C:\Users\abc123\Documents\zibit'
sys.path.append(path)

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mysite.settings")

application = get_wsgi_application()

我的文件夹结构如下所示:

zibit
|
+--mysite
|  |
|  +--settings.py
|
+--myvenv

我觉得我错过了很简单的事情。有什么想法吗?

编辑:

我已将路径更新为:

path = r'C:\Users\abc123\Documents\zibit'

不幸的是,我似乎仍然遇到同样的错误。这是完整的错误:

Traceback (most recent call last):
  File "C:\Users\abc123\AppData\Local\Programs\Python\Python36\lib\runpy.py", line 193, in _run_module_as_main
    "__main__", mod_spec)
  File "C:\Users\abc123\AppData\Local\Programs\Python\Python36\lib\runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "C:\Users\abc123\Documents\zibit\myvenv\Scripts\django-admin.exe\__main__.py", line 9, in <module>
  File "c:\users\abc123\documents\zibit\myvenv\lib\site-packages\django\core\management\__init__.py", line 371, in execute_from_command_line
    utility.execute()
  File "c:\users\abc123\documents\zibit\myvenv\lib\site-packages\django\core\management\__init__.py", line 317, in execute
    settings.INSTALLED_APPS
  File "c:\users\abc123\documents\zibit\myvenv\lib\site-packages\django\conf\__init__.py", line 56, in __getattr__
    self._setup(name)
  File "c:\users\abc123\documents\zibit\myvenv\lib\site-packages\django\conf\__init__.py", line 43, in _setup
    self._wrapped = Settings(settings_module)
  File "c:\users\abc123\documents\zibit\myvenv\lib\site-packages\django\conf\__init__.py", line 106, in __init__
    mod = importlib.import_module(self.SETTINGS_MODULE)
  File "C:\Users\abc123\AppData\Local\Programs\Python\Python36\lib\importlib\__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 978, in _gcd_import
  File "<frozen importlib._bootstrap>", line 961, in _find_and_load
  File "<frozen importlib._bootstrap>", line 948, in _find_and_load_unlocked
ModuleNotFoundError: No module named 'mysite'

1 个答案:

答案 0 :(得分:0)

反斜杠正在杀死你的路径字符串:

path = 'C:\Users\abc123\Documents\zibit'

应该是

path = r'C:\Users\abc123\Documents\zibit'

path = 'C:\\Users\\abc123\\Documents\\zibit'

如果你正在使用virtualenvwrapper-win,你可以转到zibit目录并发出

add2virtualenv .

将为您设置路径(如果您需要使用多个virtualenvs,它将比编辑wsgi文件更好。)