skip to Main Content

I have imported my entire project into docker, and I am getting a

ModuleNotFoundError

from one of the modules I have created.

FROM python:3.8
WORKDIR /workspace/
COPY . /workspace/
RUN pip install pipenv
RUN pipenv install --deploy --ignore-pipfile
#EXPOSE 8000
#CMD ["pipenv", "run", "python", "/workspace/bin/web.py"]

I tried looking around for answers, but I cannot seem to get it working.

commands:

docker build -t atletico .
docker run -p 8000:8000 atletico

Docker Build:
https://pastebin.com/FXMrY2En

Traceback (most recent call last):
  File "/workspace/bin/web.py", line 3, in <module>
    from bin.setup import setup_app
ModuleNotFoundError: No module named 'bin'

A copy of my directory:

├── Dockerfile
├── Pipfile
├── Pipfile.lock
├── README.md
├── bin
│   ├── __init__.py
│   ├── __pycache__
│   │   └── web.cpython-38.pyc
│   ├── setup.py
│   └── web.py
├── docker-compose.yml
├── frio
│   ├── __init__.py
│   ├── __pycache__
│   │   └── __init__.cpython-38.pyc
│   ├── app_events.py
│   └── config.py
├── routes
│   ├── __init__.py

docker-compose.yml:

version: '3'
services:
  db:
    image: postgres:12
    ports:
      - "5432:5432"
    environment:
      - POSTGRES_USER=postgres
      - POSTGRES_PASSWORD=postgres
      - POSTGRES_DB=test_db
  redis:
    image: "redis:alpine"
  web:
    env_file:
      - .env.local
    build: .
    ports:
      - "8000:8000"
    volumes:
      - .:/workspace
    depends_on:
      - db
      - redis
    command: "pipenv run python /workspace/bin/web.py"

2

Answers


  1. Chosen as BEST ANSWER

    So I finally fixed the issue. For those who may be wondering how was it that I fixed it. You need to define a PYTHONPATH environment variable either in the Dockerfile or docker-compose.yml.


  2. docker-compose.yml modified part

    I had the same issue, then it works after adding the PYTHONPATH=/workspace into docker-compose.yml like in the example photo.
    In my case, it works without __init__.py as well.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search