Dror
Dror

Reputation: 13051

Setting up a python dev environment using Docker (with linting and code completion in vscode)

Setup

I'm exploring the ways of setting up a python dev environment inside a docker container.

In my local (host) I have the project directory:

.
├── Dockerfile
└── foo.py

Where my Dockerfile is:

FROM python:latest

RUN pip install --upgrade pip
RUN pip install matplotlib

RUN mkdir /src
WORKDIR /src

ENTRYPOINT [ "python" ]

and the script is:

try:
    import matplotlib
    print("import successfully")
except ImportError:
    print("unable to import matplotlib")

Assuming I don't have matplotlib installed on my host, then python foo.py yields unable to import matplotlib. When I run inside the container:

docker run -it --rm -v ~/tmp/:/src/ py-deb-test foo.py

I get import successfully.

Problem

Now, my intention is to work on my code using vscode on the local host and run it in the container. The problem is that in this setting I don't know how to point vscode to the python "installation" running in the container. I thought of starting the container and "mounting" the python from the container to a local location and point vscode to this location. But so far it didn't work.

Without solving this, vscode is not aware of what packages are installed, where to find the linter, etc...

Any help and ideas would be welcome!

Upvotes: 4

Views: 3629

Answers (3)

BerriJ
BerriJ

Reputation: 1019

If this is still interesting for someone:

I've set up a docker container with Python, R and Latex and code-completion, linting and compilation for those languages. A VS-Code devcontainer file is also provided which sets the settings to sensible defaults (especially path variables so that the devcontainer finds the executables when running).

You can find it here.

Upvotes: 0

Dror
Dror

Reputation: 13051

Good news. The folks at VScode recently released what seems to be exactly what I was looking for. Using the containers remote VScode it is now possible to run the editor on a container which has the exact environment that one needs. In this repo I compiled a minimal example and I hope you'd find it useful.

Upvotes: 3

Brett Cannon
Brett Cannon

Reputation: 16020

There isn't any official support for this scenario right now, but you can follow the issue tracking remote interpreter support to know when we do have some solution.

Upvotes: 1

Related Questions