因此,我们可以使用“shift” + “enter”键在终端逐行运行代码。为了管理不同的虚拟环境,使用Docker容器是一种更好的方式。
如果我已经安装了Docker,并拉取了Python镜像。如何选择在Docker容器中创建的解释器?而不仅仅是远程连接到Docker容器中。
在VSCode 1.61上进行测试:
Remote-Containers
扩展Remote-Containers: Attach to Running Container...
,然后选择正在运行的Docker容器Get Started
页面上,点击Open...
并输入挂载到源代码的Docker卷的路径。它必须设置为与Dockerfile-local
中的WORKDIR
相同的路径,例如/app
。Python
扩展Python: Select Interpreter
,然后选择Docker解释器Python: Configure Tests
,然后选择您使用的框架来源:https://dev.to/alvarocavalcanti/setting-up-a-python-remote-interpreter-using-docker-1i24
更新 #1。 远程开发扩展
目前似乎是 VSCode 开发的主要关注点之一,例如,较新版本默认启用了 远程资源管理器
活动选项卡,这使得连接到 Docker 容器更加直观。请在此处查看发布说明:https://github.com/microsoft/vscode-docs/tree/main/remote-release-notes
更新 #2。 现在应该使用 Dev Containers 而不是 Remote Containers
请查看文档:https://code.visualstudio.com/docs/devcontainers/containers
.devcontainer/devcontainer.json
{
"name": "My Devcontainer",
"dockerComposeFile": "docker-compose.yml",
"service": "app",
"workspaceFolder": "/workspaces/${localWorkspaceFolderBasename}",
"customizations": {
...
文档:https://containers.dev/implementors/json_reference/
Dockerfile
、docker-compose.yml
、.env
添加到 .devcontainers
中...
services:
app:
build:
context: ..
dockerfile: .devcontainer/Dockerfile
volumes:
- ../..:/workspaces:cached
env_file: .env
...
docker run -v /user/localproject:/root/mountedproject
https://docs.docker.com/storage/volumes/
我已经测试过了,应该可以正常工作。
在 vscode 创建的 devcontainer.json
文件中,您有一个 image
键和它的值是指向镜像的路径。如果您想要更改 Python 版本,您可以在那里进行更改,或者使用 vscode 文档 这里 中的快速说明。
我认为这是不可能的,恐怕您必须远程连接到Docker容器。
我真的无法想象您直接在Windows上使用Linux中的Python解释器。