0
votes

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.

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 fastapi.tiangolo.com/advanced/settings/#the-config-file - lsabi
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
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
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
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

1 Answers

0
votes

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.