4

I installed different python version from source, and created virtual environment for them, and Iset them in settings per project.. The OS (SuSe Leap) is coming with python 3.6.

How can I set in VS Code the PYTHONPATH per project ? , I don't want to modify the general path.

With the virtual environment set in settings work with python builtins, but doesn't look for third-party packages in the correct path.

I already have set pythonPath to the interpreter in the python environment, but I still have some issues with packages import, and I don't find a variable for PYTHONPATH ?

4 Answers 4

12

You have three methods to configure PYTHONPATH:

1.terminal.integrated.env.* in the settings.json file. For example:

  "terminal.integrated.env.windows": {
    "PYTHONPATH": "${workspaceFolder}"
  }

But it only affects the actions routed through the terminal such as debugging.

When the terminal settings are used, PYTHONPATH affects any tools that are run within the terminal by a user, as well as any action the extension performs for a user that is routed through the terminal such as debugging. However, in this case when the extension is performing an action that isn't routed through the terminal, such as the use of a linter or formatter, then this setting will not have an effect on module look-up.

2.Configure PYTHONPATH in .env file:

By default, the Python extension looks for and loads a file named .env in the current workspace folder. The file is identified by the default entry "python.envFile": "${workspaceFolder}/.env" in your user settings.

But it will not affect the actions run in the terminal.

When PYTHONPATH is set using an .env file, it will affect anything the extension does on your behalf and actions performed by the debugger, but it will not affect tools run in the terminal.

3.Configure the "env" entry in the launch.json file, such as:

{
  "name": "Python: Current File",
  "type": "python",
  "request": "launch",
  "program": "${file}",
  "console": "integratedTerminal",
  "envFile": "${workspaceFolder}/.env",
  "env": {
    "PYTHONPATH": "${workspaceFolder}"
  }
}

Obviously, It only affects the debugging actions.

Sign up to request clarification or add additional context in comments.

Comments

1

You should be able to edit your settings.json for each project

https://code.visualstudio.com/docs/python/environments

Additional info

https://code.visualstudio.com/docs/python/settings-reference

1 Comment

I already have the environment set but I don't find in docs a specific variable for PYTHONPATH, where to search/set thord-party packages.
1

You can also set PYTHONPATH in .venv/bin/activate

export PYTHONPATH="${PYTHONPATH}:/your_home/your_user/project_root"

Comments

1

To add extra hint paths for non-debugging purposes, for example to search for imports in a non-standard location, you can set it in settings.json using

{
    ...
    "python.analysis.extraPaths": [
        "./build"
    ]
    ...
}

Now reload your VS Code window and imports and autocomplete will work fine!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.