简体   繁体   English

PyCharm 在 docker-compose 解释器运行时报告 Python 导入的“未解决的引用”

[英]PyCharm reports 'unresolved reference' on Python imports with docker-compose interpreter running

PyCharm reports 'unresolved reference' to Python imports with docker-compose interpreter running. PyCharm 报告“未解决的引用”到 Python 导入,docker-compose 解释器正在运行。 see image attached unresolved references eg in settings.py查看附加的未解决引用的图像,例如在 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.我已经阅读并尝试了一些同类问题以及此门户网站上的解决方案答案,例如将 PYCharm IDE 中的文件夹标记为源根目录。 Also I have used the Repair IDE function a lot to rebuild the indexes.我也经常使用 Repair IDE function 来重建索引。 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.我在使用 PyCharm 时遇到了这个问题,因为我没有在 venv 中运行我的 Python 安装并将 PyCharm 解释器切换到它,而是使用 Docker Compose 环境。

I have created a dockerfile and a docker-compose.yml file for this purpose.为此,我创建了一个 dockerfile 和一个 docker-compose.yml 文件。 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.如果我使用终端命令“docker compose up”,容器环境运行,我的 Python/Django 应用程序也可以通过浏览器无错误地启动。 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.容器各自的日志也没有引起任何问题。所以问题似乎不是我的Docker环境,而是PyCharm IDE如何与Docker环境交互。

here is my Dockerfile code:这是我的 Dockerfile 代码:

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:这是我的 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: admin@admin.com
      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:在我的 PyCharm Ide 中:

After all these configurations, I was able to start the Docker Environment inside the Ide with the green triangle play button.在所有这些配置之后,我可以使用绿色三角形播放按钮启动 Ide 内的 Docker 环境。 The code also seems to run because I can see the Django default app in the browser.代码似乎也可以运行,因为我可以在浏览器中看到 Django 默认应用程序。 I don't have the slightest idea why the IDE makes the red underlines though.我一点也不知道为什么 IDE 会有红色下划线。 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.有趣的是,如果我没有 select IDE 中的任何解释器,我仍然可以运行该应用程序并且我没有收到任何未解决的消息。 So only when I set the interpreter to the "web" service in the Docker compose file the IDE starts to complain.因此,只有当我将解释器设置为 Docker 撰写文件中的“网络”服务时,IDE 才会开始抱怨。

Does anyone know help.有知道的帮忙。 Thank you very much.非常感谢你。

My Software Versions:我的软件版本:

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

I have found a solution.我找到了解决办法。 The jetbrain support and the bug tool from jetbrain YoutTrack helped me to solve the problem. jetbrain 支持和 jetbrain YoutTrack 的错误工具帮助我解决了这个问题。 There were 2 things I had to do:我必须做两件事:

1. first solution section 1. 第一个解决方案部分

First of all, the support found an error in my PyCharm log that had to do with the PyCharm Docker interpreter.首先,支持人员在我的 PyCharm 日志中发现了一个与 PyCharm Docker 解释器有关的错误。 The error in the log had the following output:日志中的错误有以下 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要修复此错误,您可以执行与此错误报告中相同的操作: 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所以在PyCharm中设置远程Docker解释器时,在环境设置中取消勾选如下选项: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 2.第二个解决方案部分

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另一个平台上的用户可以给我一个关于当前 PyCharm 中错误的提示并显示它的解决方法。您可以在这里找到解决方法: 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".然而,在第 1 点的解决方案之后,日志中的错误消息消失了,并且在第 2 点的解决方法之后,代码中的所有 package 依赖项和模块也不再显示为“未解析的引用”。 This has been my solution.这一直是我的解决方案。

I have this same issue.我有同样的问题。

As far as I can tell, Jetbrains doesn't support a remote interpreter in a docker orchestration.据我所知,Jetbrains 不支持 docker 编排中的远程解释器。 And although this is ~supposed to work, it is broken in 2022.2.虽然这应该可以工作,但它在 2022.2 中被打破了。

Here's the open issue on it. 这是它的未决问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM