skip to Main Content

I would like to import methods from a directory one level above my current working directory. One way to do this is to manually append the directory to sys.path. I found a better solution for VSCode on this post. The solution described there involves creating a launch.json file with a configuration line that looks like "env": {"PYTHONPATH": "/tmp/a:/tmp/b"}. This method is documented here. Note that defining the environment variable in .env file works, but I would like to use the VSCode variable $workspaceFolder, which is not possible in .env.

Using launch.json, I am not able to access the modified PYTHONPATH. In both interactive mode and Run mode, the path doesn’t reflect the desired changes.

Following is my directory structure:

project/
  - .vscode
     - launch.json
  - conf/
     - file1.py
  - src/
     - file2.py
  - main.py
  - example.py

The file launch.json looks like this:

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Python: main",
            "type": "python",
            "request": "launch",
            "module": "main",
            "justMyCode": true,
            "env": {"PYTHONPATH": "${workspaceFolder}/conf/"}
        },
        {
            "name": "Python: example",
            "type": "python",
            "request": "launch",
            "module": "example",
            "justMyCode": true,
            "env": {"PYTHONPATH": "${workspaceFolder}/conf/"}
        },
    ]
}

When I run main.py or example.py, I don’t see the conf directory in sys.path. What’s the correct way to do this?

Example to print sys.path:

import sys
print(sys.path)

2

Answers


  1. Your launch.json is not matched with your structure. main.py and example.pyis not under the src folder in your file structure.

    enter image description here

    Modify launch.json as follows:

    {
        "version": "0.2.0",
        "configurations": [
            {
                "name": "Python: Module: main",
                "type": "python",
                "request": "launch",
                "module": "main",
                "justMyCode": true,
                "env": {"PYTHONPATH": "${workspaceFolder}/conf/"}
            },
            {
                "name": "Python: Module: example",
                "type": "python",
                "request": "launch",
                "module": "example",
                "justMyCode": true,
                "env": {"PYTHONPATH": "${workspaceFolder}/conf/"}
            },
        ]
    }
    

    Use the green triangle button to debug main.py at Run and Debug panel, or use Run --> Start Debugging/Run without Debugging.

    enter image description here

    You better modify the two configurations in Launch to different names, so that it is easier to distinguish to choose from.

    enter image description here

    Login or Signup to reply.
  2. Using launch.json, I am not able to access the modified PYTHONPATH. In both interactive mode and Run mode, the path doesn’t reflect the desired changes.

    If you’re running the file using the play button at the top right or its dropdown entries, or using the Python: Run Python File in Terminal or Jupyter: Run Current File in Interactive Window commands in the command palette, those don’t pick on things you put in your launch.json configs as far as I know. You need to actually run the launch config from the Run and Debug View, select the launch config you want to run, and then click the play button in the Run and Debug View.

    If you insist on using the Python: Run Python File in Terminal command, or actions based on that command, you can look into using the terminal.integrated.env.* VS Code settings- either in your user or workspace settings.json file. Ex.

    "terminal.integrated.env.linux": {
        "PYTHONPATH": "${workspaceFolder}/conf/",
    }
    

    I’m not sure about workarounds for Jupyter notebooks though.

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