skip to Main Content

I am developing a Python application (app), which relies on an internal Python library (lib) that is developed as a separate project. The main application depends on the library via a Git reference (pip install -e git+https://… or equivalent for Poetry). Both app and lib are developed in parallel by the same team.

I am using a multi-root workspace in VS Code for development, and I have created a parent folder in which both projects are checked out in subfolders, and with a product.code-workspace definition file as follows:

{
    "folders": [
        { "path": "app" },
        { "path": "lib" }
    ]
}

lib contains a single file lib/__init__.py:

def hello() -> str:
    return "hello world"

app contains the following app/__init__.py:

import lib


def main():
    print(lib.hello())


if __name__ == '__main__':
    main()

Both projects are set up in their own virtual environment.

When debugging app, how can I tell VS Code to load the actual, live code from the lib subfolder rather than the dependency installed in the virtual environment of app?

In other words, if I change the lib/__init__.py file above to:

def hello() -> str:
    return "goodbye"

And run app in VS Code, how do I get it to print the output goodbye instead of hello world without having to commit & push the code changes in lib and reinstalling the dependency in app?

A workaround is to set the PYTHONPATH environment variable in app’s launch.json configuration ("${workspaceFolder}/../lib"). However, I cannot use this because I have multiple project dependencies (lib1, lib2, …) so I would have to assign multiple directories to PYTHONPATH. But unfortunately the syntax for this differs across platform (Windows uses ; as path separator, whereas Linux and macOS use :), and I would prefer having a cross-platform launch configuration that can be fully committed to version control.

I know that you can have joint launch configurations in the workspace (and in fact I am using them!) but I couldn’t find an option here to specify that VS Code should use “live”, local dependencies rather than installed dependencies when launching the application.


For completeness, I have uploaded an MCVE for the two projects to GitHub. To set it up, run:

mkdir test-app && cd $_
git clone [email protected]:klmr/py-test-app app
git clone [email protected]:klmr/py-test-lib lib

cd app
python3 -m venv .venv
.venv/bin/pip install -r requirements.txt

Next, add the product.code-workspace configuration from above to the main folder, and launch it in VS Code. Now navigate to lib/lib/__init__.py, edit the function hello to return a different value, and save.

Launch app via F5. It prints hello world, not the changed value.

2

Answers


  1. But unfortunately the syntax for this differs across platform (Windows uses ; as path separator, whereas Linux and macOS use :

    Pretty sure you can just do this in your launch config:

    "windows": {
        "env": {
            "PYTHONPATH": "foo;bar",
        },
    },
    "osx": {
        "env": {
            "PYTHONPATH": "foo:bar",
        },
    },
    "linux": {
        "env": {
            "PYTHONPATH": "foo:bar",
        },
    },
    

    Note that if you use the play button at the top right, that doesn’t use launch configs and you need to use the terminal.integrated.env.<platform> VS Code settings instead.

    Login or Signup to reply.
  2. I’m probably missing something here, because your question sounds like its more complicated than this, but… install the actual lib working copy as editable for the app‘s interpreter or venv, i.e.

    pip install -e wherever/youve/put/lib
    

    (Then, forget about lib‘s venv, it doesn’t matter (aside from maybe running tests in lib‘s directory).)

    The requirements in your MCVE have an editable requirement pointing to Github, which means Pip will clone the repo into a temporary directory, so that doesn’t reflect your actual situation.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search