Using pylint with Visual Studio Code - python

I downloaded Visual Studio Code the other day and I decided to use it with pylint.
For some odd reason I couldn't set the python interpreter to python 3 from the palette (Shift + Ctrl + P) but I set the path to it from the settings and it seems to have done the job. However, pylint underlines almost everything. All of my imports are underlined with the error message:
[pylint] E0401:Unable to import 'my.import'
I read a couple of threads on this topic and the main suggestions are to set the correct python path and path to pylint, which I have done with 0 success. I tried removing it pip3 remove pylint and reinstalling it, however, it still did not fix my issue.

You may try setting "python.linting.pylintPath" key separately.
In settings.json you can set like this;
"python.linting.pylintPath": "/home/user/.local/bin/pylint"
user will be your user name of course
if you use linux and want to find exact location of pylint you can run which pylint command in terminal

To solve this issue, you can set the path to your python installation in preferences-> settings python.pythonPath.

Related

Visual Studio Code Pylance (report Missing Imports )

I keep getting error "X" could not be resolved Pylance(reportMissingImports) [ln 1, Col8]
I AM in fact a beginner, the basic youtube "fixes" are NOT working
View Command Pallet ... NOT working
Terminal pip install .... NOT working
I am running the Zip install on my work computer and im guessing it has something to do with a directory. but i cant seem to figure it out. the bottom left corner shows the python version which is ( Pyhton 3.110a7 64-bit(windows store)
Pylance requires you to set the Python PATH:
If you're in Mac/linux, make use of :
which python3
And in windows:
where python
So that the path in which you're python is installed is returned
Copy that path.
Go to your vscode and open the settings.json file (CTRL + SHIFT + P, and type "settings.json" at search bar)
Add the following key to the json file
"python.defaultInterpreterPath": "/Users/YOURUSERNAME/opt/anaconda3/bin/python3"
This was just an example, the PATH could be something more like
"C:/users/YOURUSERNAME/anaconda3/bin/python3" in case you're using windows.
The following documentation from python for vscode provides more information about how to configure Python for Visual Studio Code:
https://code.visualstudio.com/docs/python/settings-reference
There’s a very comprehensive discussion on settings in VS Code here: https://stackoverflow.com/a/63211678/5709144.
In summary and with regard to this specific case, it’s better to change settings by going to (on a Mac) Code > Preferences > Settings.
Enter python.defaultInterpreterPath in the search box at the top of screen. The current path is shown in an editable text box.
Enter any path you like here - foobar, mother, whatever you like. It doesn’t matter as, if the path isn’t recognised by VS Code, VS Code lists those that are. The only reason you enter the path is to get the list of potential paths. Click on one of these accepted paths and you’re all set up.
As asked, your question doesn't specify whether or not your imported module is correctly installed. If it isn't, then this answer will not apply. However, if your code works as expected and you're getting a false warning, then you can ignore the warning by doing the following.
Create the file .vscode/settings.json in your current directory and then add the following:
"python.analysis.diagnosticsSeverityOverrides": {
"reportMissingImports": "none",
}
Be warned however that this will ignore all missing import warnings, not just the one you're trying to get rid of. Therefore, if you have any imports that are legitimately missing, the warning will not be there.
Also, on some occasions, you might have configured your environment by adding custom paths that Pylance can not detect.
In that case, you can use the python.analysis.extraPaths parameter to add more paths to your project, such as :
"python.analysis.extraPaths": ["app", "another/path/etc"]
(Source: https://dev.to/climentea/how-to-solve-pylance-missing-imports-in-vscode-359b)
If you are using the VS, Please go to the settings .. search for Advance path and then ADD it (/.source) it should solve the problem.
Hope you have installed the Pylance in your system correctly.

"ERROR conda.cli.main_run:execute(33): Subprocess for 'conda run xxx' " in 1.65.00 VSCode

Today I run my python(.py) file as usual,but the terminal shows a new problem which I cannot solve for a long time.
Explicitly,I used to click Run Python File,
but the terminal shows that like this:
I then tried another two ways to run my python,such as "run code"(code runner provides),debug(python plugin together with "run python file"),and it shows that:
.
It is obvious that except the "debug",other two both fail to run,showing "ModuleNot..."(while actually the module exists in the vir env).
All codes are easy:
print('Hello World')
import os
print(os.path.abspath(__file__))
import numpy as np
go to vscode Extensions market and search "python". right click on "Python"(published by microsoft) and click on "Install another version". select a version that previously worked for you - usually just the one back before the current one.
Faced with the same issue.Solved by adding the following line
python.terminal.activateEnvironment" : false
to the settings.json file of the visual code
In the new version of Python extension in VSCode you have to add the Path for the conda by yourself. Go to the Python extension settings and add the path for the conda.exe.
Screenshot of the needed setting
I felt so lost; what a strange error.
Yes, just install an earlier version of the vscode python extension.
I cannot understand how such a bug has been introduced into the extension.

Jupyter commands work only with a dash (e.g. jupyter-kernelspec instead of jupyter kernelspec)

I am using Jupyter with Anaconda3.
My Anaconda3\ and Anaconda3\Scripts\ folders have been added to the %PATH% variable.
Eventhough the jupyter.exe is in the Scripts folder above, Jupyter related commands don't work without a dash.
For example:
jupyter kernelspec --version
Error executing Jupyter command 'kernelspec': [Errno 'jupyter-kernelspec' not found] 2
The same command with a dash works:
jupyter-kernelspec --version
5.2.2
And the same goes for jupyter-notebook, etc.
Do I have to add anything else to my %PATH%? Am I missing something?
I have opened an issue for this point on Github as well, but it didn't get any attention unfortunately: https://github.com/jupyter/jupyter/issues/381
Well, I figured out what's wrong. Using the shutil module, in some Windows versions which('jupyter-kernelspec') returns None, because of the missing .exe, although the PATHEXT environment variable contains both .exe and .EXE.
(This seems to be linked to this: shutil.which() not finding programs without appending file extension although I am not convinced because which(jupyter-kernelespec.EXE) using shutil works fine for me...)
So, one has to add the .exe to the argument of jupyter like this:
jupyter kernelspec.exe list
Because this kind of command is used by most Jupyter kernel installers, you won't always be able to go debug and check where you need to add it.
The fix consists in adding this:
if cmd[-4:] != '.exe':
cmd = cmd + '.exe'
right before this line:
https://github.com/jupyter/jupyter_core/blob/f1e18b8a52cd526c0cd1402b6041778dd60f20dc/jupyter_core/command.py#L102
I'll try to raise this point with shutil module people.
I've updated also the github issue and closed it.
https://github.com/jupyter/jupyter/issues/381
Does your PATHEXT environment variable have a trailing semicolon? If so, it may be the same issue described in the similar issue you mentioned in your answer: shutil.which() not finding programs without appending file extension
Remove any trailing semicolon from PATHEXT and try again.

How to run Pylint with PyCharm

I want to configure Pylint as an external tool in my entire project directory for a Python project that I'm working on. I've tried to use the repository as a module with __init__.py and without, and it's not working either way.
I'm having difficulty setting up Pylint to run with PyCharm. I know that I should be running it as an external tool, however the settings confuse me.
The authoritative source on their documentation is broken, so I can't check that up either.
You can set up Pylint to work with PyCharm by following the following steps:
Install pylint:
$ pip install pylint
Locate your pylint installation folder:
$ which pylint # MacOS/Linux
/usr/local/bin/pylint # This is just a possible output - check yours
<!-->
$ where pylint # Windows
%LocalAppData%\Programs\Python\Python36-32\Scripts\pylint.exe # Possible location
Open the PyCharm settings window with menu File → Settings, then navigate to menu Tools → External Tools in the sidebar. (Or search "external tools")
Set up an external tool by clicking on the + sign and filling in the fields accordingly. In Program use the path you got when running which pylint. For the other values, you can use the same from the image.
Run pylint from menu Tools → External Tools → pylint:
Look at your output in the PyCharm terminal
For more details, refer to Pylinting with PyCharm.
If you want to use Pylint to check your whole project or a particular file or directory, you can right click on your project root, file or directory, then activate External Tools → pylint as shown below.
Because I didn't find a working ready-made setup, these are the settings I use in PyCharm CE 2018.1 on macOS:
1 - pip install pylint in your project virtualenv or globally
2 - Add new external tool and configure:
Program: pylint
Arguments: "--msg-template='{abspath}:{line:5d},{column:2d}: {msg} ({symbol})'" --output-format=colorized "$FilePath$"
Working directory: $ProjectFileDir$
Output filters: $FILE_PATH$:\s*$LINE$\,\s*$COLUMN$:
Notice the required double quotes for the msg-template, and the escape chars for the output filters. The output filter allows to click on the file path and open the location in the IDE source editor.
Only missing feature would be the output filters to plot the lint descriptions directly into the source view, as is done with the builtin linter. No such feature at this time though.
You can try this Pylint PyCharm plugin:
It provides both real-time and on-demand scanning of Python files with Pylint from within PyCharm/IntelliJ IDEA.
Once you have it installed, the real-time inspection works automatically.
For the on-demand you have several options that go from just checking the current open file to scan the entire project:
(Just for the sake of transparency, I am the developer.)
I now use and recommend the PyCharm plugin which didn't exist when I first wrote this answer.**
A colleague pointed me towards pylint-pycharm on GitHub. It's a wrapper around Pylint with output formatted for PyCharm.
Here's how I set it up:
git clone https://github.com/perses76/pylint-pycharm.git
cd pylint-pycharm
python setup.py build
This creates build/scripts-2.7/pylint-pycharm
Then, in PyCharm, create a new External Tool with these settings:
Program: path to your installation of pylint-pycharm
Arguments: --virtualenv=$PyInterpreterDirectory$/.. $FileName$
Working directory: $FileDir$
Output filters: $FILE_PATH$\:$LINE$\:$COLUMN$\:.*
Now run it from menu Tools* → External Tools → PyLintPyCharm. Each line of output will be hyperlinked to the relevant position in the source code.
A note on the previous answers.
I was searching for a method to make PyCharm aware of the output syntax so I can directly jump to the file locations. That works without using additional tools.
Pylint can be configured to output messages in a specific format using the msg-template option in the pylintrc file or the CLI option --msg-template.
I set it to:
msg-template='{abspath}:{line}:{column}: {msg_id} {msg}'
In the External Tools settings, the Output filters: can be set to
$FILE_PATH$:$LINE$:$COLUMN$: .*
so PyCharm shows links to directly jump to the reported locations.
This can be combined with output-format=colorized so I get this:
PyCharm does not recognize the column despite having it configured. But having the file and line is enough for me.
At first install Pylint with pip:
pip install pylint
You have to open “Settings → Tools → External Tools“ and press the “+” button at PyCharm.
Here are an example with good settings.
Roberto Leinardi has created a Pylint plugin for PyCharm which really works and integrates well into the IDE:
Easy to install from the repositories, full instructions under:
pylint-pycharm
I have a short, yet a happy, experience with it so far! Kudos to Roberto.
Thanks to information here, and updated documentation from PyCharm, I've been able to get this to work nicely to also use the virtual environment for the project (ensuring that any packages can be deployed within the virtual environment and do not need to be deployed globally).
Taking what lkraider provided earlier, but with slight modifications:
Ensure you install Pylint within the virtual environment. Note, make sure that when you created the virtual environment you did not select "Inherit global site-packages". If you do then Pylint will end up being globally and this will not work.
Add a new external tool and configure. This is slightly different compared to what lkraider provided. For one I wanted it to look more like normal Pylint output, hence my msg-template (and Output filter) is a bit different. Each to their own.
The second change is the more critical one for executing Pylint based on the virtual environment. That is the program parameter where I use $PyInterpreterDirectory$.
Program: $PyInterpreterDirectory$/pylint
Arguments: "--msg-template='{abspath}:{line:5d}:{column}: {msg_id}: {msg} ({symbol})'" --output-format=colorized "$FilePath$"
Working directory: $ProjectFileDir$
Output filters: $FILE_PATH$:\s*$LINE$\:\s*$COLUMN$:
External tool for Pylint

PyCharm cannot find library

I am using PyCharm 5 to run a Python 2.7 (Anaconda) script in Ubuntu. My script imports a module with import tensorflow, but this causes the error ImportError: libcudart.so.7.0: cannot open shared object file: No such file or directory. So, it seems that the library libcudart.so.7.0 is needed by this module, but it cannot be found.
Now, I have seen that this library is on my machine in /usr/local/cuda-7.0/targets/x86_64-linux/lib. So, in PyCharm, I went to Settings->Project Interpreters->Interpreter Paths. This had a list of paths, such as /home/karnivaurus/Libraries/Anaconda/python2.7. I then added to this list, the path mentioned above which contains the required library.
However, this did not fix the problem. I still get an error telling me that libcudart.so.7.0 cannot be found. If I run my script from the shell though (python myfile.py), then it runs fine.
How can I tell PyCharm where to find this library?
I have noticed that if I have print sys.path in my script, the paths it prints out are entirely different to those in Settings->Project Interpreters->Interpreter Paths... should they be the same?
I came across this problem just recently using a remote debugger, however I believe it's still the same solution. I just added the following to the Environment Variables section in the Run/Debug Configuration options found in Run > Edit Configurations... dialog: LD_LIBRARY_PATH=/usr/local/cuda/lib64:$LD_LIBRARY_PATH
The path to your cuda library seems strange to me. I would expect it to be /usr/local/cuda-7.0/lib64 or /usr/local/cuda-7.0/lib.
Did you follow all of the cuda installation procedure?
If you type env on the command line, do you see a path to cuda in your LD_LIBRARY_PATH?
Update from comments below:
The issue is that PyCharm was invoked from the desktop, and wasn't getting the right environment variables. Solution is to either:
invoke from the command line,
create a script to set environment and then invoke, and make a link to that script on the desktop,
or set environment variables on the desktop item
While some of these answers are correct, and could work, I haven't seen what the OP specifically asked for, and that is where to set environments for the python console. This can be accomplished inside pycharm at:
File > Settings > Build,Execution,Deployment > Console > Python Console
In the options there, you'll find a place to define Environment Variables. Set LD_LIBRARY_PATH there.
Edit your pycharm.desktop, specify the environment variable in exec, like below:
[Desktop Entry]
Version=1.0
Type=Application
Name=Pycharm
Exec=env LD_LIBRARY_PATH=:/usr/local/cuda/lib64:/usr/local/cuda/lib64 /home/cwh/software/pycharm-2016.1.4/bin/pycharm.sh
Icon=/home/cwh/software/pycharm-2016.1.4/bin/pycharm.png
Name[zh_CN]=Pycharm
so pycharm will find cuda
Have you selected the right python interpreter in your project's settings?
See here.
I had a similar issue and changing the interpreter solved it without having to create a new icon.
The following works for me on Community edition 2019.3
To set globally for a project:
Open File/Settings/Project/Project Interpreter
click on the cog icon next to the interpreter
choose show all
click on the little folder with tree icon bottom right
add the path to "Interpreter Paths"

Categories