简体   繁体   中英

No module named rest_framework . Django + Docker

I've installed djangorestframework with markdown and django filter using pip in virtualenv in django docker container, checked via pip freeze . The absolute path in OS X is /Users/user/project/denv/lib/python2.7/site-packages . Added 'rest_framework', in settings.py but still after docker-compose up get the following error. I guess it has something to do with wrong path, but have no idea how to fix that.

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

EDIT: terminal screenshot 在此处输入图片说明

EDIT2: 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

EDIT2: I suppose that error is because of application actually using docker's python not virtualenv python.

Is it possible? How to either install djangorestframework to docker's python packages or better make django application use own python ?

You don't need to pip install manually. The following line in your dockerfile will install requirements.

RUN pip install -r requirements.txt

You just need to add djangorestframework to your requirements.txt

# previous stuff Django etc...
djangorestframework

and run

docker-compose up --build

Can you show us your Dockerfile ?

It should look like this :

FROM python:2.7
ENV PYTHONUNBUFFERED 1
WORKDIR /code
ADD . /code/
RUN pip install -r requirements.txt

actually using docker's python

Yes, building an Docker-Image with FROM python:2.7 installs a fresh Python Environment.

not virtualenv python.

Yes, unless you copy your virtualenv inside your Docker-Image and activate it there (which you should do). 做)。

it's activated. and it was activated when I was installing those things

It doesn't matter unless you pip freeze > requirements.txt and use RUN pip install -r requirements.txt inside your Dockerfile , which will install all your Python-Dependencies inside your virtualenv in the Python-Environment inside your Docker-Image.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM