python Django导入错误:使用Docker时无法导入Django

juzqafwq  于 2023-01-08  发布在  Python
关注(0)|答案(2)|浏览(186)

我在控制台中得到一个错误,而试图使用docker。如何修复它,但当我测试与localhost它只是工作。

误差

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"

停靠文件

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导入操作系统导入系统

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
x8diyxa7

x8diyxa71#

这看起来像是错误的配置问题。请尝试将命令替换为

python3 manage.py runserver 0.0.0.0:8000
jtoj6r0c

jtoj6r0c2#

配置中的设置:第一个月

docker-compose build
docker-compose up

相关问题