Diego L
Diego L

Reputation: 908

How do I get visual studio code to recognize docker's python interpreter?

I am doing a course in which I make an application using fastapi. I started creating a virtual environment with fastapi and uvicorn, later what I did was continue the development using docker containers, one for the application and another for the database. There is a file called requirements.txt, and when the python image (based on python:3.10-slim-buster) is built all the dependencies are installed. However, in my visual studio code it does not recognize these dependencies since it selects my local version of python as interpreter. I don't know how this works, but is there any way that visual studio code uses the version of python that I have in the container with its dependencies as interpreter?

Upvotes: 1

Views: 4218

Answers (2)

larenite
larenite

Reputation: 287

Alternatively, if all of your dependencies are installed inside the docker container instead of a local venv, you can do the following:

  1. Make sure your container is running
  2. Install the Dev Containers VSCode Extension
  3. In VSCode, go to Command Palette (F1) -> Dev Containers: Attach to Running Container... > [docker container]
  4. In the new VSCode window, open the directory in which your code lives in the docker, e.g., /opt/app
  5. Install the Python VSCode Extension
  6. In the docker shell, run which python to see where docker's python lives
  7. Command Palette (F1) -> Python: Select Interpreter -> paste the path from Step 6

Tah dah!

Upvotes: 3

Admin
Admin

Reputation: 97

Inside VS Code, go into your python file, do ctrl + shift + p (Windows)/shift + command + p (Mac) to Python: select interpreter.

Enter interpreter path, go to the path of the virtual environment with your dependencies you created at the beginning and select the python executable.

Upvotes: 1

Related Questions