问题描述
我已经在我的 Windows 机器上设置了 Python、Django 和 MySQL.我正在尝试通过从名为zibit"的目录运行以下命令来验证我的数据库连接:
I have setup Python, Django and MySQL on my Windows machine. I'm trying to verify my database connection by running the following from a directory named 'zibit':
> myvenv\Scripts\activate
> set DJANGO_SETTINGS_MODULE=mysite.settings
> django-admin dbshell
运行最后一个命令后,我收到此错误:
After running the last command I get this error:
ModuleNotFoundError: No module named 'mysite'
在网上看到一些相关帖子后,我尝试将我的 wsgi.py 文件更新为以下内容:
I tried updating my wsgi.py file to the following after seeing some related posts online:
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
我觉得我错过了一些非常简单的东西.有什么想法吗?
I feel like I'm missing something very simple. Any ideas?
我已将路径更新为:
path = r'C:\Users\abc123\Documents\zibit'
不幸的是,我似乎仍然遇到同样的错误.这是完整的错误:
Unfortunately, I still seem to be getting the same error. This is the full error:
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'
推荐答案
反斜杠正在扼杀你的路径字符串:
The back-slashes are killing your path string:
path = 'C:\Users\abc123\Documents\zibit'
应该
path = r'C:\Users\abc123\Documents\zibit'
或
path = 'C:\\Users\\abc123\\Documents\\zibit'
如果您使用的是 virtualenvwrapper-win
,您可以转到 zibit 目录并发出
If you're using virtualenvwrapper-win
you could go to the zibit directory and issue
add2virtualenv .
它将为您设置路径(如果您需要使用多个 virtualenv,它会比编辑 wsgi 文件更有效).
which will set up the paths for you (it will work better than editing the wsgi file if you need to work with multiple virtualenvs).
这篇关于ModuleNotFoundError: 没有名为“mysite"的模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!