我收到一个错误,ModuleNotFoundError: No module named 'rest_framework' 当我运行命令 python manage.py runserver
时。
回溯说
Unhandled exception in thread started by <function check_errors.<locals>.wrapper at 0x108193ae8>
Traceback (most recent call last):
File "/Users/xxx/anaconda/envs/env/lib/python3.6/site-packages/django/utils/autoreload.py", line 226, in wrapper
fn(*args, **kwargs)
File "/Users/xxx/anaconda/envs/env/lib/python3.6/site-packages/django/core/management/commands/runserver.py", line 113, in inner_run
autoreload.raise_last_exception()
File "/Users/xxx/anaconda/envs/env/lib/python3.6/site-packages/django/utils/autoreload.py", line 249, in raise_last_exception
six.reraise(*_exception)
File "/Users/xxx/anaconda/envs/env/lib/python3.6/site-packages/django/utils/six.py", line 685, in reraise
raise value.with_traceback(tb)
File "/Users/xxx/anaconda/envs/env/lib/python3.6/site-packages/django/utils/autoreload.py", line 226, in wrapper
fn(*args, **kwargs)
File "/Users/xxx/anaconda/envs/env/lib/python3.6/site-packages/django/__init__.py", line 27, in setup
apps.populate(settings.INSTALLED_APPS)
File "/Users/xxx/anaconda/envs/env/lib/python3.6/site-packages/django/apps/registry.py", line 85, in populate
app_config = AppConfig.create(entry)
File "/Users/xxx/anaconda/envs/env/lib/python3.6/site-packages/django/apps/config.py", line 116, in create
mod = import_module(mod_path)
File "/Users/xxx/anaconda/envs/env/lib/python3.6/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 'rest_framework'
我已经运行命令pip3 install djangorestframework
,当我再次运行这个命令时,Requirement already satisfied: djangorestframework in/usr/local/lib/python3.6/site-packages shows in terminal.Python版本是 3.6.2。我的代码有什么问题?我该如何解决?我使用的是 Anaconda 虚拟环境。
最佳答案
看起来好像 pip3 install
已经将包安装到 /usr/local/lib/python3.6/site-packages
,而不是你的环境 /Users/xxx/anaconda/envs/env/lib/python3.6/site-packages
.
如果您使用 python -m pip
,则该包将安装在您运行 python manage.py runserver
时使用的同一版本的 python 中。这是建议的命令 in the Python docs .
python -m pip install djangorestframework
关于python - 模块未找到错误 : No module named 'rest_framework' I already installed djangorestframework,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48074225/