Skip to content

Debug configuration env variables should overwrite env variables defined in .env file  #16984

Description

@ccrvlh

Environment data

  • VS Code version: 1.58.2
  • Extension version (available under the Extensions sidebar):
  • OS and version: MacOS 11.4
  • Python version: 3.8.10
  • Type of virtual environment used: venv
  • Relevant/affected Python packages and their versions: Flask
  • Value of the python.languageServer setting: "Default"

Expected behaviour

"env" portion of launch.json doesn't seem to work.
When trying to launch a Flask application I have the following tree structure:

/.venv
/.vscode
/app
-- uwsgi.py ---> Flask Application

My launch.json file is:

{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "Python: Flask",
      "type": "python",
      "request": "launch",
      "module": "flask",
      "pythonPath": "${workspaceRoot}/.venv/bin/python",
      "env": {
        "FLASK_APP": "app/uwsgi.py"
      },
      "args": ["run"],
      "jinja": true
    }
  ]
}

When running the debugger Flask says it can't find application and the first line shows:

* Serving Flask app "application.py"

So from what I can understand it is as if the env var "FLASK_APP=app/uwsgi.py" is not being read, so it loads the default "application", and can't find the app.

Tried without the debugger and works fine (FLASK_APP=app/uwsgi.py; flask run).

Another weird thing is that when I set

      "env": {
        "FLASK_APP": "app/uwsgi.py",
        "FLASK_ENV": "development"
      },

it goes as for as running the server, but when trying to reach any endpoint it says flask.cli.NoAppException: Could not import "application"..
If I remove the FLASK_ENV line it doesn't even run the server and gives me an error: Error: Could not import "application".

Activity

  1. fabioz commented on Jul 29, 2021

    @fabioz

    @lowercase00

    The way you're defining things, I think your "cwd" must be at a pretty specific place for app/uwsgi.py to be picked up, yet, you're not defining it... can you try to specify the "cwd" in the launch config to be the folder which contains app/uwsgi.py to see if it works?

    Note: it did work for me when the cwd was properly specified...

  2. ccrvlh commented on Aug 2, 2021

    @ccrvlh
    Author

    Fabio Zadrozny (@fabioz) thanks, I did try with ${workspaceFolder} before the app/uwsgi.py also didn't work, I'll try setting cmd to set how it goes

  3. ccrvlh commented on Aug 3, 2021

    @ccrvlh
    Author

    Just tried with cmd and also didn't work.
    What I find weird is the application. Is not that is not finding the Flask App, it's not even looking for app/uwsgi.py is only looking for application.py as if the env var is not being read.

  4. ccrvlh commented on Aug 16, 2021

    @ccrvlh
    Author

    Weird enough, I can't replicate the issue on a newly create Flask project with exactly the same folder structure. I've already created and recreated the venv and everything else that I could (mypy, pyenv, vscode folder etc). Any tips ou troubleshooting this? I've got no messages at all at the developers console, so really no idea what could be the issue.

  5. int19h commented on Aug 16, 2021

    @int19h

    Are there any version differences in the packages between the two environments, if you look inside .venv/site-packages?

  6. ccrvlh commented on Aug 16, 2021

    @ccrvlh
    Author

    So finally found out the issue. I had an old FLASK_APP=application.py on my .env file, and it was overwriting the env vars I was passing in the launch.json. I'm not sure if it's possible for the extension to control the order in which env vars are loaded, so not sure if anything different could have been done. If it is, I would suggest that the env vars in the launch.json file to be top priority and overwrite other env vars, instead of the other way around.

  7. int19h commented on Aug 16, 2021

    @int19h

    From debugpy perspective, it doesn't actually know anything about .env - that's something the extension handles, basically, by reading the file and then injecting those variables into your debug config. So this would need to be handled in vscode-python.

    But I agree that launch.json should take priority - .env is supposed to define the default values for the entire folder/workspace, but individual debug configurations should be able to override that as needed.

  8. added
    bugIssue identified by VS Code Team member as probable bug
    regressionBug didn't exist in a previous release
    and removed
    triage-neededNeeds assignment to the proper sub-team
    on Aug 17, 2021
  9. added
    importantIssue identified as high-priority
    and removed
    importantIssue identified as high-priority
    on Sep 29, 2021
  10. changed the title [-]Flask debugger not reading the correct env var[/-] [+]Debug configuration env variables should overwrite env variables defined in .env file [/+] on Nov 29, 2021
  11. locked as resolved and limited conversation to collaborators on Dec 30, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

area-debuggingbugIssue identified by VS Code Team member as probable bugregressionBug didn't exist in a previous releaseverifiedVerification succeeded

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions