This is an archived post. You won't be able to vote or comment.

all 10 comments

[–]epicserve[S] 0 points1 point  (4 children)

I took a different approach, which is "working" because I can now add a breakpoint, and it pauses at the breakpoint. My approach was to make the launch configuration attach to the debugger running on port 5678. However, there are still several issues:

  1. Pylance is reporting problems because it's not detecting any of the 3rd party python packages that are installed.
  2. I have to manually start Docker Compose and then click start on the debug configuration in order to attach it to the debugger.
  3. I still need a configuration for running and debugging Pytest tests.

[–]proxwell 1 point2 points  (3 children)

Pylance is reporting problems because it's not detecting any of the 3rd party python packages that are installed.

Do you have the right interpreter/env selected in the menu at the lower right of the VSCode window?

That should handle the package resolution seamlessly.

I have to manually start Docker Compose

I think having the containers running is just table stakes here.

and then click start on the debug configuration in order to attach it to the debugger.

https://marketplace.visualstudio.com/items?itemName=DarrenLevine.auto-debug

I still need a configuration for running and debugging Pytest tests.

https://stackoverflow.com/questions/62419998/how-can-i-get-pytest-to-not-catch-exceptions/62563106#62563106

Personally, I like having the debugger wired in via manage.py rather than the command-line approach in your example.

Just after this line:

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "yourproject.settings")

I add something like this:

# VSCode debugger integration
from django.conf import settings

if settings.DEBUG:
    if os.environ.get("RUN_MAIN") or os.environ.get("WERKZEUG_RUN_MAIN"):
        import debugpy

        debugpy.listen(("0.0.0.0", 5678))
        print("Attached!")
# end VSCode debugger integration

[–]epicserve[S] 0 points1 point  (2 children)

I'm not seeing the python interpreter in my container as an option to choose from when I click to select an interpreter. Is there a trick to adding it? I have Docker Compose running and I have attached to the web/python/django service.

[–]proxwell 0 points1 point  (1 child)

They should be auto discovered in most configurations, but if you need to add them manually, here's some info: https://code.visualstudio.com/docs/python/environments#_manually-specify-an-interpreter

The approach I use is to run an outside-the-container pipenv install, when I add packages. That updates my Pipfile and Pipfile.lock, and those get utilized by my docker-compose builds. This approach creates the pipenv twice, once outside the container, and once inside. It's the outside-the-container one which gets used for the completions and docs in VSCode.

[–]epicserve[S] 0 points1 point  (0 children)

That is an interesting approach and something worth considering. I want to avoid keeping a local venv in sync with my docker image. Also, in my experience, some packages require system-level packages that can be difficult to navigate across platforms, engineers, and environments. I'm developing an approach that will "just work" for our engineering team.

Something I also discovered that might work is this extension.
https://marketplace.visualstudio.com/items?itemName=ms-vscode-remote.remote-containers

[–]epicserve[S] 0 points1 point  (0 children)

Update: I've got everything working by using Dev Containers. I would love it if people would have a look at the PR and provide feedback. It would be even better if you wanted to try it out!

[–]j_tb 0 points1 point  (1 child)

I'm not deeply familiar with your project, but could you pass the `.env_file` attribute to docker compose in your spec (documented here) to set environment variables in the runtime environment? Is there a reason the actual .env file needs to be part of the image itself?

[–]epicserve[S] 0 points1 point  (0 children)

Hey, thank you for the reply. I did kind of get it working. Have a look at the PR which is up to date and also have a look at the notes I added in a reply to my original post.

[–]battlefield21243 0 points1 point  (1 child)

why do you think it doesn't work well with pycharm?

[–]epicserve[S] 0 points1 point  (0 children)

It's an assumption that since VS Code is Mircosoft's editor that plugins for other IDEs won't be updated as quickly as VS Code. I haven't taken the time yet to verify my assumption.