Reputation: 421
In vscode, we can install the python extension, then select the interpreter which we want, like python in the conda environment.
So we can use "shift" + "enter" key for running the code line by line in the terminal. For managing different virtual environment, using docker container is a better way.
If I already install the docker, and pull the python image. How to select the interpreter which is created in the docker container? Not just remote to the docker container.
Upvotes: 24
Views: 35595
Reputation: 704
Tested on VSCode 1.61:
Remote-Containers
extensionRemote-Containers: Attach to Running Container...
, then select the running Docker containerGet Started
page, click the Open...
and enter the path to the Docker volume mounted to your source code. It must be set to the same path as WORKDIR
in your Dockerfile-local
, e.g. to /app
.Python
extension on the containerPython: Select Interpreter
, then select the Docker interpreterPython: Configure Tests
, then select the framework you useSource: https://dev.to/alvarocavalcanti/setting-up-a-python-remote-interpreter-using-docker-1i24
UPD #1. Remote development extensions
seem to be one of the main focuses in VSCode development currently, e.g. the newer versions have got the Remote explorer
Activity tab enabled by default, which allows much more intuitive approach to connecting to Docker containers. Check release notes here: https://github.com/microsoft/vscode-docs/tree/main/remote-release-notes
UPD #2. Nowadays you should use Dev Containers instead of Remote Containers
See the docs: https://code.visualstudio.com/docs/devcontainers/containers
.devcontainer/devcontainer.json
{
"name": "My Devcontainer",
"dockerComposeFile": "docker-compose.yml",
"service": "app",
"workspaceFolder": "/workspaces/${localWorkspaceFolderBasename}",
"customizations": {
...
Docs: https://containers.dev/implementors/json_reference/
Dockerfile
, docker-compose.yml
, .env
to .devcontainers
...
services:
app:
build:
context: ..
dockerfile: .devcontainer/Dockerfile
volumes:
- ../..:/workspaces:cached
env_file: .env
...
Dev containers: Open folder in container
and Dev containers: Rebuild container
prompts to proceedUpvotes: 39
Reputation: 1310
Make sure all the extensions you want/need are installed in the container. Click Extensions on the left hand side to see which are installed locally and which are installed within the container.
Install everything within the container that you typically use locally (particularly Python and Pylance). You'll see a 'Install in Dev Container' button. Install everything you need, then restart VS Code and you should be good to go.
Example showing the install in Dev Container button
Upvotes: 0
Reputation: 3180
If your objective is to have vscode to work on a local project and run it with a docker-based interpreter, the solution is: mounting the local project directory to the docker container that contains the interpreter, then in vscode open the project directory (mounted) from the container.
How to mount your project directory:
docker run -v /user/localproject:/root/mountedproject
https://docs.docker.com/storage/volumes/
I have tested it. It should work.
Upvotes: 0
Reputation: 8421
I think it's impossible, I am afraid you must remote to the docker container.
I really can't imagine out you taking a python interpreter in Linux to work on windows directly.
Upvotes: 0
Reputation: 5404
inside your devcontainer.json
file that vscode created you have the image
key and its value is the route to the image, if you want to use to change the python version you can do so there or using the quick instructions in vscode docs here
Upvotes: 0