kyuden
kyuden

Reputation: 409

Best way to handle python packages using Docker and VSCode

I am currently developing a Django app using Docker and VSCode.

To start my development server, I run docker compose up, and write code on my VSCode. There is nothing wrong with the functionality of my app, but I sometimes need to look into package source codes using VSCode's "Go To Definition" right-click menu.

This had worked before when I was using venvs, since I was specifying "python.defaultInterpreterPath" to my venv path in my VSCode settings.json.

When I am using Docker, the packages live in my container, so naturally VSCode cannot find go to code definitions for imported packages. As an added downside, it also reports missing import errors.

Right now, I am maintaining a venv folder my machine on top of the packages inside my container, and by pointing "python.defaultInterpreterPath" to this venv, I can overcome both of the problems.

However, this does seem like a bit of extra work, since if I want to install a new package, I have to install both to my venv and my container.

Is there a better way to handl this situation?

Upvotes: 3

Views: 230

Answers (1)

Yu Wei Liu
Yu Wei Liu

Reputation: 805

I solved it by installing the "Python" and "Python Debugger" extensions into the container.

Additionally, to make it work, you may also need to ensure that install packages in the global environment rather than a virtual environment (venv) in the container.

Steps:

  1. Install the extension "Dev Containers" in your VSCode.

  2. Open the command palette: "Dev Containers: Attach to running container."

  3. Go to the "Extensions" tab.

  4. Install "Python" and "Python Debugger" in the container.

    Make sure not to install them on the local machine.

    enter image description here

  5. Open the command palette: "Reload window."

It should work now.

P.S. Compatible Versions

VSCode: 1.93.1
Extension - Dev Containers: 0.388.0
Extension - Python: 2024.14.1
Extension - Python Debugger: 2024.12.0

Upvotes: 0

Related Questions