user3541631
user3541631

Reputation: 4008

How can I set in VS Code the PYTHONPATH per project?

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 ?

Upvotes: 4

Views: 16702

Answers (4)

Ani
Ani

Reputation: 10906

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!

Upvotes: 1

Steven-MSFT
Steven-MSFT

Reputation: 8431

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.

Upvotes: 12

Leonardo
Leonardo

Reputation: 318

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

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

Upvotes: 1

EM789
EM789

Reputation: 56

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

Upvotes: 1

Related Questions