Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 ?

like image 239
user3541631 Avatar asked Oct 20 '25 15:10

user3541631


2 Answers

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.

like image 177
Steven-MSFT Avatar answered Oct 23 '25 05:10

Steven-MSFT


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

like image 27
EM789 Avatar answered Oct 23 '25 03:10

EM789



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!