Steps are:
Click on settings icon present at bottom left of vscode screen

In the top right of screen, you should see
Open Settings (JSON)option
In the next screen, within double quotes start typing
python.p, you should start seeing all the suggestions & options available
Steps are:
Click on settings icon present at bottom left of vscode screen

In the top right of screen, you should see
Open Settings (JSON)option
In the next screen, within double quotes start typing
python.p, you should start seeing all the suggestions & options available
its a problem of vscode version, newer releases indicate python.pythonPath is being deprecated in favor of python.defaultInterpreterPath. So if you try to set your variable "python.defaultInterpreterPath" to point to your desired interpreter, this will solve your problem.
Opening settings.json in VSCode - Python - Code with Mosh Forum
Selecting a python interpreter doesn't update settings.json
Best settings.json configurations and must-have extensions?
How to correctly set PYTHONPATH for Visual Studio Code - Stack Overflow
Videos
I open a folder in vscode, this folder does not have a settings.json and I run ctrl + shift p, and select an interpreter. This does not create a settings.json file and add settings to it.
I have also tried opening file -> settings, editing workspace settings so I have a settings.json and selecting interpreter but still nothing is added.
I have also tried creating a ${folder}.code-workspace settings file and selecting interpreter but nothing is added.
How do I make my folder a valid workspace so I can select an interpreter and the settings.json will update?
Hey everyone,
I’ve been using VS Code for a while, but I feel like I’m not taking full advantage of its customization options. I want to optimize my setup, so I’m looking for recommendations on:
-
Best
settings.jsonconfigurations – What tweaks have significantly improved your workflow, performance, or overall experience? -
Must-have extensions – Which extensions do you consider essential for productivity, debugging, or specific workflows?
I mostly work with TypeScript, JavaScript, React, Next.js, Node.js, Express, NestJS, Python, C++, .NET, and React Native, so any suggestions to those would be great. But I’m also open to general improvements that could benefit any workflow.
Would love to see what works for you all. Thanks!
Thanks in advance!
I have a situation that I believe is relatively common. I want a script to import a module from another directory. My python project is laid out as follows:
~/project/
|
|---modules/
|
|---mod.py
|---scripts/
|---script.py
in script.py, I have from modules import mod. So my PYTHONPATH needs to be set to ~/project/ (something that PyCharm does automatically).
VSCode is a great editor, but everywhere else, it falls short, in my opinion. This is a perfect example of that.
I create a default launch.json file to "run the current file". A "cwd": "${fileDirname}" line has to be added to make things work like they do in PyCharm (FYI, a list of the built-in variables can be found here).
Debugging
For debugging (the "play" button on the sidebar, or the F5 key), the PYTHONPATH set in launch.json or your .env file takes effect. Note that in the .env file, you cannot use variables such as ${workspaceRoot}, but you can easily append or insert to the path by using the proper separator for your platform (; for Windows and : for everyone else).
Because I want to take advantage of that variable, I put this in my launch.json:
"env": {"PYTHONPATH": "${workspaceFolder}${pathSeparator}${env:PYTHONPATH}"}
(Thanks to @someonr for the suggestion to use ${pathSeparator}.)
It appears that you can prepend/append to whatever is inherited from the environment (this is not true for settings.json; see below).
This will also work for the hotkey Ctrl+F5 (run without debugging).
For reference, here's the full file, which replicates what PyCharm does automatically:
{
"version": "0.2.0",
"configurations": [
{
"name": "Python: Current File",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "integratedTerminal",
"cwd": "${fileDirname}",
"env": {"PYTHONPATH": "${workspaceFolder}${pathSeparator}${env:PYTHONPATH}"}
}
]
}
Run in terminal
If I hit the "play" button that appears on the top right of the editor window (when a python file is the active tab), it will not work. This runs the current file in a terminal, which doesn't pay attention to launch.json at all. To make that work, you have to define PYTHONPATH in a settings.json file, by adding this:
"terminal.integrated.env.osx": {"PYTHONPATH": "${workspaceFolder}"}
(Note there are different values for each platform.) If you've selected a python interpreter (e.g. from a virtual environment), you will already have a settings.json file in the .vscode directory. Mine looks like this:
{
"python.pythonPath": "/Users/me/project/venv/bin/python3",
"terminal.integrated.env.osx": {"PYTHONPATH": "${workspaceFolder}"}
}
You can't append or insert values into the inherited PYTHONPATH via the settings.json file. It will only take one string, and it will not parse separators. So even though you could get the value using ${env:PYTHONPATH}, you won't be able to do anything with it.
Moreover, you can't set the current working directory. Even though it would seem you could set it with "terminal.integrated.cwd": "${workspaceFolder}", it doesn't work. So if any of your scripts do anything with paths relative to their location in the tree, they won't work. The working directory will be your project root.
Note that any changes to the settings.json file will require that you exit the integrated terminal and restart it.
Linting
Nothing I do to launch.json regarding PYTHONPATH makes any difference to pylint, which will red-underline from modules import mod, despite the fact I can put the cursor on mod, hit F12, and the file opens. Snooping around linting settings, the defaults for mypy include --ignore-missing-imports. To replicate this behavior with pylint, add this to your settings.json:
"python.linting.pylintArgs": [
"--disable=F0401"
]
Shame that we just have to work around this, but the autocomplete helps a lot when writing the import statements to begin with.
Conclusion
There are many layers to VSCode and it's hard to get things to work together. It seems multiple environments are floating around. In the end:
- I cannot "run in terminal" because I can't set the current working directory to be the path containing the current file.
- I cannot set
PYTHONPATHforpylintas that runs in some environment different than the integrated terminal and whatever is controlled bylaunch.json, so I can only tellpylintto ignore import errors. - Running with F5 works if you set
PYTHONPATHeither via an.envfile or inlaunch.json
The documentation is missing some important details.
Example
Suppose your project layout is like this
myproject/
.vscode/
settings.json
.env
src/
a_module.py
tests/
test_a.py
Open the settings.json file and insert these lines
// On linux use "terminal.integrated.env.linux": {
"terminal.integrated.env.windows": {
"PYTHONPATH": "${workspaceFolder}/src;${workspaceFolder}/tests"
},
// The next line can be omitted, unless you've modified the global default
"python.envFile": "${workspaceFolder}/.env",
Note that ${workspaceFolder} evaluates to myproject, it is not to the .vscode folder.
In the .env file enter this
PYTHONPATH=src;test
# Paths are relative to the workspace folder, so above is equivalent to
# WORKSPACE_FOLDER=C:/full/path/to/myproject
# PYTHONPATH=${WORKSPACE_FOLDER}/src;${WORKSPACE_FOLDER}/tests
Note that on all platforms, including Windows, the slashes in the path lean forward, like so /. Different paths are separated with a ; on Windows, on other platforms with a :.
Final step, restart VS Code.
This blog was helpful.