我正在尝试将 django-rest-swagger ui 添加到我的 api,但我得到了
ImportError: No module named rest_framework_swagger
,即使我已经完成了 pip install django-rest-swagger
。我也可以通过pip list
看到我的virtualenv上安装的包。我也尝试过python manage.py syncdb
但实际上没有做任何事情。有人对我有什么建议吗?谢谢。
INSTALLED_APPS = (
'rest_framework',
'at_api',
'rest_framework_swagger',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
)
编辑:系统路径信息
>>> import sys
>>> for p in sys.path: print p
...
C:\Users\Andrew\.virtualenvs\adventure_time\lib\site-packages\psycopg2-2.5.3-py2.7-win-amd64.egg
C:\Users\Andrew\.virtualenvs\adventure_time\lib\site-packages\six-1.8.0-py2.7.egg
C:\Users\Andrew\.virtualenvs\adventure_time\lib\site-packages\cssselect-0.9.1-py2.7.egg
C:\Users\Andrew\.virtualenvs\adventure_time\lib\site-packages\pyopenssl-0.14-py2.7.egg
C:\Users\Andrew\.virtualenvs\adventure_time\lib\site-packages\lxml-3.4.1-py2.7-win-amd64.egg
C:\Users\Andrew\.virtualenvs\adventure_time\lib\site-packages\queuelib-1.2.2-py2.7.egg
C:\Users\Andrew\.virtualenvs\adventure_time\lib\site-packages\w3lib-1.10.0-py2.7.egg
C:\Users\Andrew\.virtualenvs\adventure_time\lib\site-packages\twisted-14.0.2-py2.7-win-amd64.egg
C:\Users\Andrew\.virtualenvs\adventure_time\lib\site-packages\zope.interface-4.1.1-py2.7-win-amd64.egg
C:\Users\Andrew\.virtualenvs\adventure_time\lib\site-packages\zipline-0.7.0-py2.7.egg
C:\Users\Andrew\.virtualenvs\adventure_time\lib\site-packages\pandas-0.15.1-py2.7-win-amd64.egg
C:\Users\Andrew\.virtualenvs\adventure_time\lib\site-packages\pywin32-214-py2.7-win-amd64.egg
C:\Users\Andrew\.virtualenvs\adventure_time\lib\site-packages\pyyaml-3.11-py2.7-win-amd64.egg
C:\windows\system32\python27.zip
C:\Users\Andrew\.virtualenvs\adventure_time\DLLs
C:\Users\Andrew\.virtualenvs\adventure_time\lib
C:\Users\Andrew\.virtualenvs\adventure_time\lib\plat-win
C:\Users\Andrew\.virtualenvs\adventure_time\lib\lib-tk
C:\Users\Andrew\.virtualenvs\adventure_time\Scripts
C:\Python27\Lib
C:\Python27\DLLs
C:\Python27\Lib\lib-tk
C:\Users\Andrew\.virtualenvs\adventure_time
C:\Users\Andrew\.virtualenvs\adventure_time\lib\site-packages
>>>
回溯:
Traceback (most recent call last):
File "C:/Users/Andrew/Desktop/rocketu/adventure_time/manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "C:\Python27\lib\site-packages\django\core\management\__init__.py", line 399, in execute_from_command_line
utility.execute()
File "C:\Python27\lib\site-packages\django\core\management\__init__.py", line 392, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "C:\Python27\lib\site-packages\django\core\management\base.py", line 242, in run_from_argv
self.execute(*args, **options.__dict__)
File "C:\Python27\lib\site-packages\django\core\management\base.py", line 280, in execute
translation.activate('en-us')
File "C:\Python27\lib\site-packages\django\utils\translation\__init__.py", line 130, in activate
return _trans.activate(language)
File "C:\Python27\lib\site-packages\django\utils\translation\trans_real.py", line 188, in activate
_active.value = translation(language)
File "C:\Python27\lib\site-packages\django\utils\translation\trans_real.py", line 177, in translation
default_translation = _fetch(settings.LANGUAGE_CODE)
File "C:\Python27\lib\site-packages\django\utils\translation\trans_real.py", line 159, in _fetch
app = import_module(appname)
File "C:\Python27\lib\site-packages\django\utils\importlib.py", line 40, in import_module
__import__(name)
ImportError: No module named rest_framework_swagger
Process finished with exit code 1
按照以下步骤解决:
1- 确保您的虚拟环境已激活。
2- 使用 pip 安装需求或 rest_framework_swagger
pip install django-rest-swagger
3- 在 File-> Settings -> Project Interpreter -> : 为你的项目选择虚拟环境
4- 在工具栏 -> Edit configurations...-> python Interpreter : 选择你的项目虚拟环境
解决了……比想象中简单多了。我刚刚查看了我的项目的解释器,我注意到它没有使用安装了 django-rest-swagger 的 virtualenv。
面临同样的问题。通过使用 pip 安装 rest_framework_swagger 解决了这个问题
运行这个:
pip install django-rest-swagger