我在 Django 的 Docker 容器中的虚拟环境中使用 pip 安装了 djangorestframework
、markdown
和 django filter
,并通过 pip freeze
进行了检查。在 OS X 中的绝对路径为 /Users/user/project/denv/lib/python2.7/site-packages
。在 settings.py 中添加了 'rest_framework'
,但是在运行 docker-compose up
后仍然出现以下错误。我猜测这与路径设置有关,但不知道如何修复。
Traceback (most recent call last):
web_1 | File "/usr/local/lib/python2.7/site-packages/django/utils/autoreload.py", line 226, in wrapper
web_1 | fn(*args, **kwargs)
web_1 | File "/usr/local/lib/python2.7/site-packages/django/core/management/commands/runserver.py", line 113, in inner_run
web_1 | autoreload.raise_last_exception()
web_1 | File "/usr/local/lib/python2.7/site-packages/django/utils/autoreload.py", line 249, in raise_last_exception
web_1 | six.reraise(*_exception)
web_1 | File "/usr/local/lib/python2.7/site-packages/django/utils/autoreload.py", line 226, in wrapper
web_1 | fn(*args, **kwargs)
web_1 | File "/usr/local/lib/python2.7/site-packages/django/__init__.py", line 27, in setup
web_1 | apps.populate(settings.INSTALLED_APPS)
web_1 | File "/usr/local/lib/python2.7/site-packages/django/apps/registry.py", line 85, in populate
web_1 | app_config = AppConfig.create(entry)
web_1 | File "/usr/local/lib/python2.7/site-packages/django/apps/config.py", line 90, in create
web_1 | module = import_module(entry)
web_1 | File "/usr/local/lib/python2.7/importlib/__init__.py", line 37, in import_module
web_1 | __import__(name)
web_1 | ImportError: No module named rest_framework
编辑2: docker-compose.yml
version: '2'
services:
db:
image: postgres
web:
build: .
command: python manage.py runserver 0.0.0.0:8000
volumes:
- .:/code
ports:
- "8000:8000"
depends_on:
- db
编辑2:我认为错误是因为应用程序实际上使用的是docker的python而不是虚拟环境的python。
有可能吗?如何将djangorestframework安装到docker的python包中或更好地使django应用程序使用自己的python?