简体   繁体   English

如何告诉VSCode python 包实际上已安装?

[英]How to tell VSCode that python packages are in fact installed?

I am building a Python application with the Pygame library and I am using Docker to containerize it.我正在使用 Pygame 库构建 Python 应用程序,并且我正在使用 Docker 将其容器化。

Here is the Dockerfile :这是Dockerfile

FROM python

WORKDIR /app

COPY requirements.txt .

RUN pip install -r requirements.txt

COPY . .

CMD ["python", "main.py"]

Here is requirements.txt :这是requirements.txt

autopep8==1.6.0
pygame==2.1.0

Here is the docker-compose.yaml file:这是docker-compose.yaml文件:

version: "3.8"
services:
  app:
    build: ./
    stdin_open: true
    tty: true

Here is main.py :这是main.py

import pygame

print("hello there")

This is what happens when I run docker-compose up :这就是我运行docker-compose up时发生的情况:

Attaching to connect-four_app_1
app_1  | pygame 2.1.0 (SDL 2.0.16, Python 3.10.1)
app_1  | Hello from the pygame community. https://www.pygame.org/contribute.html
app_1  | hello there
connect-four_app_1 exited with code 0

As you can see, the application is working perfectly fine.如您所见,该应用程序运行良好。 The only problem is that in my main.py file, VSCode isn't able to find pygame , ie it thinks it isn't installed, and therefore I don't get any autocompletion for the library.唯一的问题是,在我的main.py文件中,VSCode 无法找到pygame ,即它认为它没有安装,因此我没有得到任何库的自动完成功能。 How do I tell VSCode that pygame is in fact installed, but only in the container?我如何告诉 VSCode pygame实际上已安装,但仅在容器中?

Thanks a bunch.谢谢一堆。

I have never worked with Docker so I do not know if this will help you.我从未与 Docker 合作过,所以我不知道这是否会对您有所帮助。 But my experience working with virtualenvs is that you should tell VS Code what Python interpreter you want it to use to run your code.但是我使用 virtualenvs 的经验是,您应该告诉 VS Code 您希望它使用什么 Python 解释器来运行您的代码。 Depending on where you have installed the packages, maybe you are using an interpreter that cannot reach the packages you have installed (maybe you have this package using a different Python installation, on a different directory).根据您安装软件包的位置,也许您使用的解释器无法访问您已安装的软件包(也许您有这个 package 使用不同的 Python 安装,在不同的目录上)。

Just to rule out that this is not the problem, try pressing F1 and then typing/selecting "Python: Select interpreter" on VS Code.只是为了排除这不是问题,请尝试按 F1,然后在 VS Code 上键入/选择“Python:Select 解释器”。 And then you can go through all Python versions installed in your PC to see which one recognizes the packages you want.然后您可以通过安装在您的 PC 中的所有 Python 版本 go 来查看哪一个可以识别您想要的包。 Hope this help you a bit.希望这对您有所帮助。

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

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