0

I'm utilizing VSCode devcontainers, and I'm having difficulty trying to figure out how to run a different .env file for my pytest setup, versus when I just want to run uvicorn/debug.

I can set an envfile in settings.json, which works, but I'd have to change this path back and forth depending on whether I wanted to test or just run the server. I've read that you can change the envfile for tests in launch.json, but when discovering/running tests in no longer appears these are valid settings for launch.json, and must be baked into the vscode-python extension?

I'm really confused because I feel like this is a common use case, all I am really trying to accomplish is setting a different DB connector so that test can drop/create tables for tests, but dev would persist in a separate database.

Nate Dellinger
  • 740
  • 5
  • 14
  • 1
    Not sure if this helps, but I've created a script that takes as parameter the name of the environment, like `test`, `dev`, `prod` and then sets an environment variable with the mode. Based on that I load via `pydantic` config the correct file. For more details about the configuration file, see https://fastapi.tiangolo.com/advanced/settings/#the-config-file – lsabi Nov 03 '20 at 13:40
  • Thanks for the comment, something like that was going to be my other workaround! It's kind of odd but I've somewhat solved it by having my test.env file be the default for settings.json and then specifying a different one for my launch.json. – Nate Dellinger Nov 03 '20 at 15:00
  • There is no silver bullet. Also, it depends if you run it bare metal or in a container. That can impact a lot, so it's difficult for the authors to create a unique solution, though the direction is towards containerization, git and CI/CD systems which can make use of different `.env` files that are not shared between `prod`, `dev` nor `test` – lsabi Nov 03 '20 at 17:45
  • Definitely no silver bullets haha! Yes, I was just surprised because this was actually previously configurable when the vscode python plugin put "Discover Tests" type stuff in the launch.json. That option no longer appears to be available and somehow its just baked in. – Nate Dellinger Nov 03 '20 at 19:12
  • Probably the plugin has not been updated yet. You could open an issue/bug on the project's repo (or whatever they have). Maybe the maintainers will fix it – lsabi Nov 03 '20 at 20:28

1 Answers1

2

For anyone who stumbles upon this, I found a workaround which is to specify a certain envfile for running and debugging, and then use a separate env in settings.json which will apply for the tests.

So Imagine you have

dev.env which contains

environment=dev
...

and test.env which contains

environment=test
...

Then you would include this line in your .vscode/settings.json

"python.envFile": "${workspaceFolder}/test.env"

and this is what my launch configuration looks like for bringing up fastapi with uvicorn:

{
    "name": "Python: FastAPI",
    "type": "python",
    "request": "launch",
    "module": "uvicorn",
    "envFile": "${workspaceFolder}/dev.env",
    "args": [
        "app.main:app",
        "--reload"
    ]
}

It's a little odd that this is how it has to be configured, but it seems to work for now.

Nate Dellinger
  • 740
  • 5
  • 14
  • 1
    are you aware of a way to set environment variables if you are not using the launch/debug tab and just clicking on the 'run/debug python file' icon that appears when u have a .py file open? it seems to always pick up on the .env file in that case. my use case is different env file for windows and linux. i can get it to do the right thing by selecting the correct launch setting..but not from clicking that button. – mike01010 Oct 10 '22 at 20:36