[英]Docker-compose can't import Django (ModuleNotFoundError)
我对 Docker 很陌生,我一直在尝试让一个非常简单的“Hello World”程序在 docker 上运行。 无论我做什么,我总是得到:
ModuleNotFoundError:没有名为“Django”的模块
我究竟做错了什么?
这是终端输出。
C:\path\to\app\root>docker-compose up
Creating network "hello-world_default" with the default driver
Creating hello-world_web_1 ... done Attaching to hello-world_web_1
web_1 | Traceback (most recent call last):
web_1 | File "/code/manage.py", line 10, in main
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 "/code/manage.py", line 21, in <module>
web_1 | main()
web_1 | File "/code/manage.py", line 16, in main
web_1 | ) from exc
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?
hello-world_web_1 exited with code 1
这是 dockerfile
# Pull base image
FROM python:3.7
# Set environmental variables
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1
# Set work directory
WORKDIR /code
# Install dependencies
COPY Pipfile Pipfile.lock /code/
RUN pip install pipenv && pipenv install --system
# Copy project
COPY . /code/
这是 docker-compose.yml 文件:
version: '3.7'
services:
web:
build: .
command: python /code/manage.py runserver 0.0.0.0:8000
volumes:
- .:/code
ports:
- 8000:8000
这是我的pipfile
的内容:
[[source]]
name = "pypi"
url = "https://pypi.org/simple"
verify_ssl = true
[dev-packages]
[packages]
pytz = "==2019.3"
sqlparse = "==0.3.1"
Django = "==2.2.7"
[requires]
python_version = "3.7"
嗨,我刚刚解决了这个问题。 跑完后
docker build .
运行docker-compose build
而不是docker-compose up
。
然后最后运行docker-compose up
代替
COPY Pipfile Pipfile.lock /code/
RUN pip install pipenv && pipenv install --system
你可以使用:
RUN pip install pipenv
COPY pipfile* /tmp
RUN cd /tmp && pipenv lock --requirements > requirements.txt
RUN pip install -r /tmp/requirements.txt
这是这里的一个片段
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.