Real-time linting of Python with VSCode - visual-studio-code

Recently I'm using VSCode as my Python IDE, and I install
DonJayamanne/pythonVSCode, which supports linting. However, the linter only works when saving, but what I want is real-time linting. The workaround suggested by the author is to set files.autoSave to on, so that the linter will work whenever the file is automatically saved. There's a relevant discussion on Github, for your reference.
Since I don't want to turn on auto-save function, is there any way to do real-time linting of Python with VSCode? Or is there any suggested extension?

If you use shift + cmd + P (or ^+ctrl+P for windows) or go to View > Command Palette and type "Lint"
The Command Palette allows you to execute different commands, from here you can enable/disable Linting and select which Linter you would like to use. Most popular is PyLint but you can select Flake8 or Pep8 or whatever you like.
I believe you need to do these things before the linter works in real-time.
To scan for problems with your code without saving first, use shift + cmd + M, you will receive an error code in the vscode Terminal.

EDIT
Update 4 Jul 2022
New version of python extension deprecates "python.pythonPath" and suggest to use this instead.
"python.defaultInterpreterPath": "/usr/bin/python3",
Put these lines in .vscode/settings.json
{
"python.linting.pylintEnabled": true,
"python.linting.enabled": true,
"python.pythonPath": "/usr/bin/python3",
"editor.formatOnSave": true
}
And install autopep8 package.
Use pip to install it
python3 -m pip install autopep8
Or in Debian based as python3-autopep8 exists in repo you can run
sudo apt install python3-autopep8
And then python linting on vscode will work.
Also, the Warning menu will activate.
Hint that I set run linter onsave.

Related

sudo command is not working in Vscode terminal

I am using vscode in Linux Mint.
"sudo, root, npm" commands are not working in Vscode bash terminal. I can only use cd, ls and install commands.
yearmfew#mySpace:~/jules/clr-6$ npm
bash: npm: command not found
It was because of flatpak. I have vscode from there installed. It is officially distributed in snap. I have downloaded from there and it is okay now.
This is explained in the documentation for VS Code : https://code.visualstudio.com/docs/editor/integrated-terminal#_configuration
(which has various options), but to avoid this being a link-only answer:
Set the "terminal.integrated.shell.windows" VS Code configuration variable to "C:\Windows\System32\bash.exe" and you should be good to go.
For convenience, there's also a Select Default Shell command in the command palette which sets this for you.

Wrong Python interpreter being used by VS Code

I am on Ubuntu 20.04 and have both Python2 and Python3 installed natively. I have also installed Python through miniforge, a variant of miniconda. In VSCode I have both the MS Python extension and Pylance installed.
I use the miniforge python for my coding. This works perfectly fine in PyCharm.
However in VSCode, when I try to execute the same file I get errors. After investigating it seems that VSCode is picking native Python2 - even though I have the miniforge Python selected. In this picture it can be seen that the status bar at the bottom states Python interpreter selected is Python3. But the output window shows that the python interpreter is Python2.
A more confusing thing is when I use VSCode for Jupyter notebook files then it picks up the interpreter correctly and I have no issues.
I have checked both User and Workspace settings, and they all point to Python3. How can I fix this for standard .py files?
I prefer VSCode to PyCharm, but will need to use PyCharm till this is resolved.
It seems that your system console cannot see python3. You need to put the python3 in the PATH variable, before python2. Like:
PATH=path/to/python3:path/to/python2:$PATH
Also, make sure that the environment containing python3 is activated before command prompt appears. It can be done in bash_profile by adding a line like
conda activate my_env_with_python3
Try changing the settings "Python:Python path", "Python:default interpreter path" and "Python:conda path" also.
I have just bumped into something similar. The Run code option resulted in the file being run with the default interpreter instead of the venv-based one with necessary packages installed.
The fix was simply to use "Run python file" instead:
The run-code behavior must be customizable, something is mentioned e.g. here: Run Code vs Run Python File in Terminal for VSCODE but I didn't bother.

VS Code - pylint is not running

I have a workspace setup in VS Code where I do python development. I have linting enabled, pylint enabled as the provider, and lint on save enabled, but I continue to see no errors in the Problems panel. When I run pylint via the command line in the virtual environment i see a bunch of issues - so I know pylint works. I am also using black formatting(on save) which works without issue. I have tried using both the default pylint path as well as updating it manually to the exact location and still no results. When I look at the Output panel for python it looks like pylint is never even running (i.e. I see the commands for black running there but nothing for pylint).
My pylint version is 2.4.4 and VS Code version 1.46
Any idea how to get this working?
This is due to a bug in the newer version of python extension see here.
For now you can either wait for the fix to arrive, use jedi language server or install previous version of the extension
Add
"python.linting.enabled" : true
"python.linting.lintOnSave" : true
to your settings.json
Uninstall Python Extension
Reinstall Python Extension
And with that there will will be one more extension of "Python Extension" named - "PYLANCE" don't forget to install that too.
Reload VS Code
DONE !!

Activate conda environment stopped working in vscode

I have noticed that conda activate environment stopped working in vscode integrated terminal with no obvious reason (I certainly did not install any new extensions, nor updated conda or vscode in the last few days).
Expected behaviour
After running conda activate environment I want the environment to be activated and be capable of using the environment python interpreter. Essentially it should look like this:
(base) lpravda-ml:~ lpravda$ conda activate rdkit-env
(rdkit-env) lpravda-ml:~ lpravda$ which python
/Users/lpravda/anaconda3/envs/rdkit-env/bin/python
This is what I can achieve using non-vscode terminal and work with conda as usual.
Actual behaviour
This happens when I type in the same set of commands in vscode integrated terminal:
(base) lpravda-ml:~ lpravda$ conda activate rdkit-env
(rdkit-env) lpravda-ml:~ lpravda$ which python
/usr/local/bin/python
The environment seems to be switched on, however python still points to the system install python 2.7 and I have no idea why.
It used to be working like for years and now it broke down.
Note: When I run debugger in vscode the correct conda environment is selected and executed.
My setup:
OS: Mac OS X: 10.14.3
vscode: 1.31.1
conda: 4.6.7
Update
ongoing issue with the following update:
OS: Mac OS X: 10.14.3
vscode: 1.33.3
conda: 4.6.8
Any idea what could possibly broke down?
it is issue which you can read about it here, but I find this solution
which solve the issue in this version. Also, as community said the problem with python interpreter and env gonna fix in next major update.
Create an workspace folder
Inside the create file post the following code:
{
"folders": [
{
"path": "."
}
],
"settings": {
"python.condaPath": "/home/pcuser/anaconda3/condabin/conda",
"python.pythonPath": "/home/pcuser/anaconda3/envs/my_env/bin/python",
"python.terminal.activateEnvironment": true,
"python.terminal.activateEnvInCurrentTerminal": true,
}
}
Save configuration and close vscode
Open vscode.
It will select automatically the conda enviroment (Should appear at the bottom left of vscode). Inside terminal 'my_env' will also be selected. If vscode doesn't select the 'Python Selected Interpreter' before you open the terminal the my_env inside terminal won't be selected. Open the terminal after the my_env is selected by vscode

Highlight typos in the jupyter notebook markdown

When I write something in the jupyter notebook markdown field, the typos are not highlighted and often I ended up with something like this:
In almost all IDEs I have used so far, the typos are highlighted with a curly underline which was very convenient for me. Something like this:
Up till now I have not found anything that allows me to see this type of highlights. Does it exist?
The popular Jupyter Notebook bundle extension from Jupyter-contrib contains a spell checker. You can install and enable this (with admin privileges) like so:
pip install jupyter_contrib_nbextensions
jupyter contrib nbextension install --user
jupyter nbextension enable spellchecker/main
This may be the most popular spell checker for Jupyter Notebooks, but note that it simply highlights words not in its dictionary, and does not offer corrections.
If the extension installed properly, you will see this message in the command line:
Now, in the browser, after opening Jupyter, you will see the button labelled "abc" beside the keyboard button, which you can toggle to enable/disable spell check:
The jupyter-contrib library has many other useful modules such as code folding and table of contents.
Run the following in a terminal:
ipython install-nbextension https://bitbucket.org/ipre/calico/downloads/calico-spell-check-1.0.zip
ipython install-nbextension https://bitbucket.org/ipre/calico/downloads/calico-document-tools-1.0.zip
ipython install-nbextension https://bitbucket.org/ipre/calico/downloads/calico-cell-tools-1.0.zip
jupyter nbextension enable calico-spell-check
You can see typos like
Find out more
Afterwards, you need to activate the spell check in the Jupyter Notebook:
%%javascript
IPython.notebook.config.update({
"load_extensions": {"calico-spell-check":true,
"calico-document-tools":true,
"calico-cell-tools":true
}
})
The spellchecker and some other extensions from jupyter_contrib_nbextensions is incompatible with the ipython version 5.8.0. and later. (Found that it works in some cases for the version 5.0.0 [refer issue page of the same]. I'm not aware of its compatibility for the ipython versions between 5.0.0. to 5.8.0., please do comment if anyone knows).
The calico's nbextension 'spellchecker' available at https://bitbucket.org/ipre/calico/downloads/calico-spell-check-1.0.zip is an outdated version [dated 2015]. But calico had updated their nbextensions and made avaliable at the github repository [latest dated 2018]. It works fine for the ipython version 5.8.0.
Installation
git clone https://github.com/Calysto/notebook-extensions.git
cd notebook-extensions
jupyter nbextension install calysto --user
jupyter nbextension enable calysto/spell-check/main
Check its status:
jupyter nbextension list
When you now open or reload a notebook, there would be a new button visible with a check mark icon next to the button to open the command palette. You may click on it to check the spelling mistakes in the markdown cell.