I want to run Pylint or any equivalent while using Jupyter-Notebook. Is there a way to install and run Pylint this way?
pycodestyle is an equivalent of pylint for Jupyter Notebook which is able to check your code against the PEP8 style guide.
First, you need to install the pycodestyle in jupyter notebook by typing this command,
!pip install pycodestyle pycodestyle_magic
Run this command in a cell of jupyter notebook.
After successful installation, you have to load the magic in a Jupyter Notebook cell like this,
%load_ext pycodestyle_magic
Then, you have to use pycodestyle in a cell in which you want to investigate your code against PEP8 standards.
Below are some examples for more and clear understanding,
%%pycodestyle
a=1
Output: pycodestyle will give you this message,
2:2: E225 missing whitespace around operator
Another example,
%%pycodestyle
def square_of_number(
num1, num2, num3,
num4):
return num1**2, num2**2, num3**2, num4**2
Output:
2:1: E302 expected 2 blank lines, found 0
3:23: W291 trailing whitespace
I suggest you consider the nbQA tool:
pip install nbqa pylint
nbqa pylint my_notebook.ipynb
Besides pylint, nbqa makes it easy to run several other formatter and linter tools and is easy to integrate into your development workflow via their dedicated pre-commit hooks.
To answer the question more specifically in regards to pylint. One relatively simple way to achieve that in a development / ci environment (i.e. command line) is to convert the notebook to Python and then run the linting.
Let's assume you have notebooks in the ./notebooks folder and you have the jupyter and pylint command in the path, you could run the following:
jupyter nbconvert \
--to=script \
--output-dir=/tmp/converted-notebooks/ \
./notebooks/*.ipynb
pylint /tmp/converted-notebooks/*.py
You might want to configure pylint, as the notebook style is slightly different to a general Python module.
Some rules that you might want to disable:
pointless-statement
expression-not-assigned
trailing-newlines
wrong-import-position
redefined-outer-name
invalid-name
It also appears that the maximum number of characters in a cell (before horizontal scrolling) is 116 but that might depend on other factors.
(These options can for example be configured using the --max-line-length and --disable pylint arguments, or via the .pylintrc file)
The JupyterLab extension jupyterlab-lsp supports pylint (but pylint is disabled by default):
https://github.com/python-lsp/python-lsp-server/issues/171#event-6236223765
pip install jupyterlab-lsp
pip install 'python-lsp-server[all]'
Here is my config for the LanguageServer tab under Settings => Advanced Settings Editor, enabling pylint:
{
"language_servers": {
"pyright": {
"serverSettings": {
"python.analysis.useLibraryCodeForTypes": true
}
},
"pylsp": {
"serverSettings": {
"pylsp": {
"plugins": {
"pydocstyle": {
"enabled": true
},
"pyflakes": {
"enabled": false
},
"flake8": {
"enabled": true
},
"pylint": {
"enabled": true
}
}
}
}
}
}
}
In case you opt for using nbqa but you happen to have notebooks using pyspark as framework (instead of python), the following command will fail to recognize your notebooks:
nbqa pylint **/*.ipynb
The reason is that .ipynb files using pyspark have this metadata in the body:
"metadata": {
"name": "pyspark"
}
which prevents nbqa for recognizing it as python type. The solution is to simply substitute the value of "pyspark" with "python", here is a shell script that does it (you might need to install jq):
for i in $(find . -type f -name "*.ipynb");
do jq '.metadata.language_info.name="python"' "$i" > "$i".ipynb;
nbqa pylint "$i".ipynb -f colorized --fail-under=7;
rm "$i".ipynb;
done;
Related
I have a python library in github I want to debug. The library has setup.py as well as a pip released package. So I can install this module using either pip install git-sim or python3 setup.py install.
This module adds a command to the path and I can execute in terminal with git-sim .
Main Problem: I want to clone the github repo and open vscode inside the cloned repo and debug the code getting executed while I am running the command from the terminal. The breakpoints should hit the file inside the repo.
Link to the github Repo: https://github.com/initialcommit-com/git-sim
I went searching with these requirements. Best I could come up so far is
An edit mode in setup.py which can be started by pip install -e . . While I ran this command and was successful I did not see a way to debug it.
How do we debug a module in Vscode? Give this code in launch.json
{
"name": "Python: Module",
"type": "python",
"request": "launch",
"module": "git-sim",
"justMyCode": false,
}
But while I am running this I am getting an error, no module named git-sim even though the same command works outside the debugger in the terminal. i.e both git-sim and python3 -m git_sim
Any suggestion of where I maybe going wrong? Or if there is some alternative option I have not considered
Found the issue. I made a mistake in assuming the terminal command git-sim and the actual python -m git_sim were same.
Module Name and Terminal commands are different. So if we modify launch config from "module":"git-sim" to "module":"git_sim" it works. Modified config given below:
{
"name": "Python: Module",
"type": "python",
"request": "launch",
"module": "git_sim",
"justMyCode": false,
}
I'm trying to compile a .sol with py-solc-x.
I downloaded the library with pip install py-solc-x but still, vscode doesn't recognize it.
please help me fix this.
The massage after pip install
The code
You create virtual environment and then install it. I get alot of imports error in pycahrm and vscode for packages.
python3 -m venv venv
source venv/bin/activate
pip3 install py-solc-x
Also the way you import package is not correct. this is how you should setp:
from solcx import compile_standard, install_solc
# whichever version u want to use
install_solc("0.8.0")
You have installed the solcx in the global python310 environment, please check which python interpreter you have selected from the bottom-left on the VSCode.
I'm working on the same tutorial and ran into the same issue as you. I followed #Steven-MSFT's tip and got the code to work. Take a look:
from solcx import compile_standard, install_solc
_solc_version = "0.6.0"
install_solc(_solc_version)
with open("./SimpleStorage.sol", "r") as file:
simple_storage_file = file.read()
# Compile our solidity
compiled_sol = compile_standard(
{
"language": "Solidity",
"sources": {"SimpleStorage.sol": {"content": simple_storage_file}},
"settings": {
"outputSelection": {
"*": {"*": ["metadata", "evm.bytecode", "evm.sourceMap"]}
}
},
},
solc_version=_solc_version,
)
print(compiled_sol)
Then I ran python3 deploy.py and it successfully outputted the ABI JSON object!
I also founded this issue. The problem is Python is install globally but site package is in user appdata, in your case "C:\users\kin...\python310\site-packages".
i had uninstalled python and deleted python310 folder in user appdata then installed python and packages again, this time in vscode python extnsion setting page tick "Global module installation".
i hope this work for you too.
I've installed most popular Python extensions in vscode. So I can do shift+enter to execute some selected code into a python terminal.
This uses /usr/bin/python3 as default. I would like to use IPython3 instead. However, I don't find such settings in settings.json.
How can I configure the default Python terminal in vscode?
1.The reason for "I can do shift+enter to execute some selected code into a python terminal." is that "shift+enter" is the default shortcut key of VSCode, and the command it executes is "python.execSelectionInTerminal":
{ "key": "shift+enter", "command": "python.execSelectionInTerminal",
"when": "editorTextFocus && !findInputFocussed && !jupyter.ownsSelection && !replaceInputFocussed && editorLangId == 'python'" },
2.Set the default terminal to ipython in VSCode, please use "ipython.exe" to execute the file in settings.json: for example:
"terminal.integrated.shell.windows": "D:\\Users\\...\\AppData\\Local\\Programs\\Python\\Python38\\Scripts\\ipython.exe",
Use:
Open a file with the suffix .ipynb and vs code will do the rest
I have C:\Users\user\AppData\Roaming\Python\Python37\site-packages directory in sys.path
And while I run code via cmd it works fine.
However, when i try run it via vscode debugger, I get this:
No module named request
So, how can I fix this?
According to your description, it is recommended that you could try the following methods:
Check the python interpreter for the current VSCode.
When running python files in the cmd window, the system uses the python set by the environment variable.
About it can run in cmd, but not in VSCode.This will happen if the Python interpreter used in VSCode is different from the cmd. Reference:python-interpreter.
Try using pip to install the required module again. Use 'pip --version'(Linux and Mac:'pip -v') at the terminal to check whether the version of pip comes from the current interpreter.Use 'pip install <modules name>' to install it. Then, check whether the module is in the list by 'pip list'.
Attempt to reload VSCode.
Above, I could import the required modules from similar locations. My environment:python:3.8.3; VSCode:1.47.2; Win10.
Update:
launch.json:
{
"version": "0.2.0",
"configurations": [
{
"name": "Python: Current File",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "integratedTerminal"
}
]
}
When I open my Python files in Sublime and delete simple things like : at the end of def myFunction():, then I expect the linter to add a red circle in the gutter. But I see nothing in the gutter.
I notice at the bottom of the Sublime window it says "pyflakes(erred)"
Installing the dependencies
# Sublime v 3.1.1
# Sublime package install SublimeLinter
# Sublime package install SublimeLinter-pyflakes
pip3 install pyflakes
which pyflakes
# /Users/macbook/Desktop/my_virtual_env/bin/pyflakes
Trying to tell my linter the path to use in "MyProj.sublime-project" settings
{
"folders":[
{
"path": "."
}
],
"SublimeLinter.linters.pyflakes.python": "/Users/macbook/Desktop/my_virtual_env/bin/python3"
}
UPDATE: Sublime console output upon loading views.py
SublimeLinter: #10 linter.py:907: 'pyflakes' is linting 'views.py'
SublimeLinter: #10 python_linter.py:42: pyflakes: wanted python is 'None'
SublimeLinter: #10 python_linter.py:93: pyflakes: trying to use globally installed pyflakes
SublimeLinter: #10 python_linter.py:101: WARNING: cannot locate 'pyflakes'. Fill in the 'python' or 'executable' setting.
These settings go under the key settings. E.g.
{
"folders": [
{
"path": "."
},
],
"settings": {
"SublimeLinter.linters.pyflakes.python": "..."
}
}