Search code examples
djangodebuggingdocker-composepycharm

Integration Pycharm, Docker Compose and Django Debbuger getting error: /usr/local/bin/python: can't find '__main__' module. in ''


I'm having a problem using Pycharm to run a Docker container to debug a Django project on MacOs.

The Pycharm setup is working fine to run the Django project inside a Docker container. But when I try to debug I'm having the following issue:

/usr/local/bin/docker-compose -f /Users/claudius/Studies/Pycharm/test-api/docker-compose.yaml -f /Users/claudius/Library/Caches/JetBrains/PyCharm2021.2/tmp/docker-compose.override.1494.yml up --exit-code-from web --abort-on-container-exit web
Docker Compose is now in the Docker CLI, try `docker compose up`

mongo is up-to-date
postgres is up-to-date
Recreating test-api_web_1 ... 
Attaching to test-api_web_1
Connected to pydev debugger (build 212.4746.96)
web_1      | /usr/local/bin/python: can't find '__main__' module in ''
test-api_web_1 exited with code 1
Aborting on container exit...
ERROR: 1

Process finished with exit code 1

To set up the Pycharm I did:

  • Add a Python interpreter with docker-compose and the container of the web application (Django). Python interpreter
  • Add a Django config for the project. Django config project
  • Add a Run/Debugger config. Run debugger

UPDATE

As requested by @Thy and @DanielM that is the Dockerfile:

FROM python:3.9.1 AS backend

ARG DJANGO_ENV

ENV DJANGO_ENV=${DJANGO_ENV} \
  # pip:
  PIP_NO_CACHE_DIR=off \
  PIP_DISABLE_PIP_VERSION_CHECK=on \
  PIP_DEFAULT_TIMEOUT=100 \
  # poetry:
  POETRY_VERSION=1.1.7 \
  POETRY_VIRTUALENVS_CREATE=false

# Set work directory
WORKDIR /pysetup
COPY ./pyproject.toml ./poetry.lock*  /pysetup/

RUN pip install "poetry==$POETRY_VERSION"
RUN poetry install --no-interaction --no-ansi

# Copy project
COPY . /pysetup/

And that is the docker-compose.yaml:

# docker-compose.yml
version: '3.8'

services:

  db:
    image: postgres:12.0-alpine
    env_file:
      .env
    ports:
      - "5432:5432"
    container_name: postgres

  mongodb:
    image: mongo:5.0.5
    env_file:
      - .env
    ports:
      - "27017:27017"
    container_name: mongo

  web:
    build: .
    command: ["python", "manage.py", "runserver", "0.0.0.0:8000"]
    env_file:
      - .env
    volumes:
      - .:/test-api-volume
    ports:
      - 8000:8000
    stdin_open: true
    tty: true
    depends_on:
      - db
      - mongodb

Does someone have any suggestions?


Solution

  • After I did can't debug with Docker, I tried to debug using virtual environments and I did receive the same message:

    Connected to pydev debugger (build 212.4746.96)
        /usr/local/bin/python: can't find '__main__' module in ''
    

    So a work colleague suggested updating the IDE Pycharm.
    And it just worked. Now I can debug with Docker or using virtual envs.

    In other words, the problem was with IDE Pycharm Professional- version 2021.1.1 running on MacOS Monterey.