Reputation: 978
I'm trying to make Python linters to work in VS Code when Python and all packages are installed in a Docker container.
I didn't use linters before. But as far as I understand how linters work (at least in VS Code), I need to point VS Code to Python interpreter and set paths to packages if needed. And this becomes a problem if everything is installed in Docker container.
I'm trying to use only Docker features. What I came up with is the following:
I tried to implement everything using Django sample project from Docker docs, so my files looks like following
docker-compose.yml
version: '3'
services:
db:
image: postgres
web:
build: .
command: python3 manage.py runserver 0.0.0.0:8000
volumes:
- .:/code
- ./.vscode/python:/usr/local/lib/python3.7 # The problem is here
ports:
- "8000:8000"
depends_on:
- db
Dockerfile
FROM python:3
ENV PYTHONUNBUFFERED 1
RUN mkdir /code
WORKDIR /code
ADD requirements.txt /code/
RUN pip install -r requirements.txt
ADD . /code/
requirements.txt
Django
psycopg2
Django project works when the line - ./.vscode/python:/usr/local/lib/python3.7
is commented.
But when I am trying to bind mount Python folder the same way as /code
folder mounted, I not only don't have content of this folder accessible locally, but Django project stops working.
Is it possible to mount Python executable in this way? Or maybe there is a better way to use arbitrary Python linters while using VS Code and Docker? It would be great to avoid:
Upvotes: 17
Views: 11128
Reputation: 101
I've accomplished this using the Remote - Containers extension for VS Code. The steps i followed were:
Install the Remote Containers extension https://marketplace.visualstudio.com/items?itemName=ms-vscode-remote.remote-containers
Open the Command Pallette (cmd + shift + p in Mac OS) and type Remote-Containers
, then select the option "Attach to Running Container"
Select the running container (where the python interpreter and code are)
Click in the bottom bar where the python interpreter is displayed (git branch and current container also are displayed in this bar)
When you get prompted which interpreter to use select "Enter interpreter path..." option and select the python path, the most common case will be /usr/local/bin/python
Now VS code is using the same interpreter as your docker container and you can install the linter you want to, in my case flake8
Upvotes: 1
Reputation: 4571
Not the Docker side, just some VS code considerations
VS Code relies on two mechanisms for resolving python highlighing: environment and linter.
For environment you can check https://code.visualstudio.com/docs/python/environments, which basically says either python is availsble on the system in path, or pick virtual environmrnt you create, or provide a path to python executable in json.
https://code.visualstudio.com/docs/python/linting tells to install linter with pip, runnable from environment you configured or provide a path to linter in json.
So it looks the only things you need to replicate VSCode python linting is python executabe, installation of pylint and json configuration for vscode.
Upvotes: 3