简体   繁体   中英

Django ImportError: Couldn't import Django When Use Docker

I am getting an error in console while trying to use docker. How to fix it but when I test With localhost it just work.

在此处输入图像描述

error

PS C:\Users\Test\Desktop\Projects\Testadmin\master> docker-compose up
Creating network "master_default" with the default driver
Creating master_web_1 ... done
Attaching to master_web_1
web_1  | Traceback (most recent call last):
web_1  |   File "manage.py", line 8, in <module>
web_1  |     from django.core.management import execute_from_command_line
web_1  | ModuleNotFoundError: No module named 'django'
web_1  |
web_1  | The above exception was the direct cause of the following exception:
web_1  |
web_1  | Traceback (most recent call last):
web_1  |   File "manage.py", line 10, in <module>
web_1  |     raise ImportError(
web_1  | ImportError: Couldn't import Django. Are you sure it's installed and available on your PYTHONPATH environment variable? Did you forget to activate a virtual environment?
master_web_1 exited with code 1

docker-compose.yml

version: '3'

services:
    web:
        build: .
        command: python manage.py runserver 0.0.0.0:8000
        volumes:
            - .:/app
        ports: 
            - "8000:8000"
            

Dockerfile

FROM python:3
ENV PYTHONUNBUFFERED l
RUN mkdir /app
WORKDIR /app
COPY requirements.txt /app/
RUN pip install -r requirements.txt
COPY . /app/

manage.py

#!/usr/bin/env python import os import sys

if __name__ == '__main__':
    os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'master.settings')
    try:
        from django.core.management import execute_from_command_line
    except ImportError as exc:
        raise ImportError(
            "Couldn't import Django. Are you sure it's installed and "
            "available on your PYTHONPATH environment variable? Did you "
            "forget to activate a virtual environment?"
        ) from exc
    execute_from_command_line(sys.argv)

requirements.txt

Django==3.8.5

Settings in config: ALLOWED_HOSTS = ['']

docker-compose build
docker-compose up

This smells like a bad configuration issue. Try replacing the command with

python3 manage.py runserver 0.0.0.0:8000

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