Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Python linters with Docker in VS Code

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:

  1. Bind mount Python directory to some local folder
  2. Select Python interpreter in VS Code from that folder
  3. If needed, add directories for installed packages in a similar way (but I didn't manage to reach this stage yet)

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:

  • creating locally the same environment as in Docker container
  • installing extensions that don't allow to use arbitrary linters
like image 406
Den Kasyanov Avatar asked Sep 15 '18 17:09

Den Kasyanov


1 Answers

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.

like image 140
Evgeny Avatar answered Sep 22 '22 16:09

Evgeny