Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I activate venv in each new terminal in a Dev Container?

I'm trying to create a devcontainer.json that will create a Python virtual environment and activate it in the terminal immediately.

I use postCreateCommand to create the environment, and that seems to work:

 "postCreateCommand": "python3 -m venv .venv",

But I haven't figured out how to get the terminal to always activate it. I've tried adding this setting:

            "settings": {
                "python.terminal.activateEnvInCurrentTerminal": true
            }

As well as post*Commands:

  "postAttachCommand": ". .venv/bin/activate",
  "postStartCommand": ". .venv/bin/activate",
  "postCreateCommand": ". .venv/bin/activate",

They don't seem to reliably activate it. I think postAttachCommand worked sometimes, but not always.

like image 658
Pamela Fox Avatar asked Jan 29 '26 22:01

Pamela Fox


1 Answers

When you also set the defaultInterpreterPath-setting it works as expected:

"settings": {
    "python.terminal.activateEnvInCurrentTerminal": true,
    "python.defaultInterpreterPath": ".venv/bin/python",
}
like image 140
Lars Bilke Avatar answered Feb 03 '26 09:02

Lars Bilke



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!