skip to Main Content

PyCharm reports ‘unresolved reference’ to Python imports with docker-compose interpreter running.
see image attached
unresolved references e.g. in settings.py

I have already read and tried some problems of the same kind and the solution answers on this portal, like marking the folders in the PYCharm IDE as source root. Also I have used the Repair IDE function a lot to rebuild the indexes. Nothing. Nothing has helped so far.

I’m having this problem with PyCharm since I’m not running my Python installation in a venv and switching the PyCharm interpreter to it, but working with a Docker Compose environment.

I have created a dockerfile and a docker-compose.yml file for this purpose. If I use the terminal command "docker compose up", the container environment runs and my Python/Django application can also be started without errors via the browser. The respective logs of the containers do not cause any problems either.So the problem doesn’t seem to be with my Docker environment, but rather with how the PyCharm IDE interacts with the Docker environment.

here is my Dockerfile code:

FROM python:3.10.4-slim-bullseye

# Set environment variables
ENV PIP_DISABLE_PIP_VERSION_CHECK 1
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1

# Set work directory
WORKDIR /cpp_base

# Install dependencies
COPY ./requirements.txt .
RUN pip install -r requirements.txt

# Copy project
COPY . .

and here my docker-compose.yml:

version: "3.9"

services:
  web:
    build: .
    container_name: python_django
    command: python /cpp_base/manage.py runserver 0.0.0.0:8000
    volumes:
      - .:/cpp_base
    ports:
      - "8000:8000"
    depends_on:
      - db
  db:
    image: postgres:14.5
    container_name: postgres_14.5
    restart: always
    ports:
      - "5432:5432"
    environment:
       POSTGRES_DB: cpp_base
       POSTGRES_USER: postgres
       POSTGRES_PASSWORD: postgres
    volumes:
      - postgres_data:/var/lib/postgresql/data

  pgadmin:
    container_name: pgadmin4_container
    image: dpage/pgadmin4
    restart: always
    volumes:
      - pgadmin_data:/var/lib/pgadmin
    environment:
      PGADMIN_DEFAULT_EMAIL: [email protected]
      PGADMIN_DEFAULT_PASSWORD: root
    ports:
      - "5050:80"

  blackd:
    restart: always
    image: docker.io/pyfound/black
    command: blackd --bind-host 0.0.0.0 --bind-port 45484
    ports:
      - "45484:45484"

  portainer:
    image: portainer/portainer-ce:latest
    container_name: portainer
    restart: unless-stopped
    security_opt:
      - no-new-privileges:true
    volumes:
      - /etc/localtime:/etc/localtime:ro
      - /var/run/docker.sock:/var/run/docker.sock:ro
      - ./portainer-data:/data
    ports:
      - "9000:9000"

volumes:
  postgres_data:
  pgadmin_data:

In my PyCharm Ide:

After all these configurations, I was able to start the Docker Environment inside the Ide with the green triangle play button. The code also seems to run because I can see the Django default app in the browser. I don’t have the slightest idea why the IDE makes the red underlines though. The funny thing is that if I don’t select any interpreter within the IDE I can still run the application and I don’t get any unresolved messages. So only when I set the interpreter to the "web" service in the Docker compose file the IDE starts to complain.

Does anyone know help.
Thank you very much.

My Software Versions:

  • PYCharm 2022.2.2
  • Windows 11, 10.0.22000
  • Docker v2.12.0, running on WSL2
  • Python 3.10.4
  • Django 4.1

2

Answers


  1. Chosen as BEST ANSWER

    I have found a solution. The jetbrain support and the bug tool from jetbrain YoutTrack helped me to solve the problem. There were 2 things I had to do:

    1. first solution section

    First of all, the support found an error in my PyCharm log that had to do with the PyCharm Docker interpreter. The error in the log had the following output:

    Error response from daemon: invalid environment variable: =::=::
    

    To fix this error you can do the same as in this bug report: https://youtrack.jetbrains.com/issue/PY-24604/Unable-to-create-Docker-Compose-interpreter-InternalServerErrorException-invalid-environment-variable

    So when setting up the remote Docker interpreter in PyCharm, uncheck the following option in the environment settings: include parent environment variables

    Unfortunately, this is quite hard to find and probably a lot of users won't find it right away and therefore run into the same error.

    2. second solution section

    A user on another platform could give me a hint about a bug in the current PyCharm and show the workaround for it.You can find the workaround here: https://youtrack.jetbrains.com/issue/PY-55617/Pycharm-doesnt-recognize-any-of-my-installed-packages-on-a-remote-host

    I can't say if the two solutions mentioned are dependent on each other. However, after the solution in point 1, the error messages were gone in the logs and after the workaround in point 2, all package dependencies and modules in the code were also no longer shown to me as "unresolved references". This has been my solution.


  2. I have this same issue.

    As far as I can tell, Jetbrains doesn’t support a remote interpreter in a docker orchestration. And although this is ~supposed to work, it is broken in 2022.2.

    Here’s the open issue on it.

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