Rushing Labs

Setting up Python, Flask, VSCode Environment

Getting Started§

Ref: https://www.tutlinks.com/setup-visual-studio-code-for-python-development/

Start here. It's a good explanation for starting from zero.

  • Install Python extension (see note at end of this page for a specific bug)
  • (optional) install GitLens, GitHistory extensions
  • Select interpreter
  • Select linter

This also covers creating a settings.json for the ./vscode directory. This is where VSCode stores the project-specific configurations for your Python project.

It's very important for consistency, and (if you're using git) can help other project contributors get setup easily. So, it's best to commit it to the git repo.

Setup Python environment, packages§

Ref: https://www.tutlinks.com/debugging-flask-app-with-vs-code-made-easy/

For setting up a Python venv (virtual environment) in your project:

# cd [to-your-project-directory]
python -m venv env
env\Scripts\activate
python -m pip install --upgrade pip
pip install -r requirements.txt

Note: If you're building a service-based web app (web client + API), and you have both pieces in the same repo...make sure you are in the /api directory where your Python code exists before executing those instructions.

We need a debug configuration, too. Ctrl+Shift+D in VSCode is a shortcut to the debug panel. This will guide you through creating a launch.json, in the ./vscode directory. See? That directory is extra important to be included in the git repo.

launch.json holds the debug config for your Python Flask project. More debug configs can be added here if, for some reason, you need multiple debug configs on the same project.

Make sure you change the FLASK_APP variable to match what you've named the entry point for your Python project. Default is app.py, but we're building an API.

"env": {
        "FLASK_APP": "api\\api.py",
        "FLASK_ENV": "development"
    },

Writing a requirements.txt§

Here's an example requirements.txt:

###### Requirements without Version Specifiers ######
nose
nose-cov
beautifulsoup4

###### Requirements with Version Specifiers ######
docopt == 0.6.1             # Version Matching. Must be version 0.6.1
keyring >= 4.1.1            # Minimum version 4.1.1
coverage != 3.5             # Version Exclusion. Anything except version 3.5
Mopidy-Dirble ~= 1.1        # Compatible release. Same as >= 1.1, == 1.*

Choosing Environments (Python virtual environment)§

Specific to debugging a Python script/app within VSCode, it can be confusing to set the environment properly. Sometimes you get weird errors like "cannot import modules" when all modules seemingly have been installed after running pip install -r requirements.txt.

After a virtual environment, relative to your project, has been created.

Ref: https://code.visualstudio.com/docs/python/environments#_work-with-python-interpreters

In VSCode:

  • Open the command pallette, Python: Select Interpreter
  • From here, you should be able to choose the environment within the project. Not elsewhere on the system.

Also, make sure to pay attention to setting the environment env: {...} variables in launch.json as mentioned above.

For the example project I was struggling with, the Python project was not at the root of the project. It is under an api/ directory (i.e. /api/app.py). So, relative to VSCode, the FLASK_APP variable needs to reflect this, hence: "FLASK_APP": "api\\app.py".

"env": {
    "FLASK_APP": "api\\app.py",
    "FLASK_ENV": "development"
},

Ref: https://stackoverflow.com/questions/57517874/flask-tutorial-could-not-import-app-in-visual-studio-code-1-37-1

Special Note: Problem with Python extension for VSCode§

At some point in the whole setup process, my Python environment started acting weird. The debugger would start and then immediately stop. Somehow flask would no longer be detected for import. Yet enabling the python env, and installing from pip would result in all packages returning saying they were already installed.

I downgraded the VSCode Python extension to the latest release from 2021, and restarted VSCode, and this seemed to be enough to get things going again. The two links listed above also described similar behavior, and stated this solution worked. Unsure why the bug exists.

  • VSCode extension: Python (v2021.12.1559732655)

Further Reading§

RushingLabs - Analytics