For the following code
"""Test pylint on undefined variable"""
import random
def main():
"""Use undefined variable"""
if random.randint(0, 10) == 6:
thing = "hi"
print(thing)
if __name__ == '__main__':
main()
PyCharm correctly reports the problem.
pylint (2.0.0, Python 3.6.6) however does not recognize it:
Your code has been rated at 10.00/10 (previous run: 10.00/10, +0.00)
But I would like it to find it, in order to let my CI fail in these cases.
So in fact I have two questions:
Is there an option for pylint to enable so that it can find this kindf of error?
What linting is PyCharm using by default? (I always thought it's pylint under the hood.)
Is there an option for pylint to enable so that it can find this kind of error?
Pylint is currently not able to detect possibly-undefined variables in a conditional or control flow block. A future version of Pylint may be able to recognize these kinds of errors. As of the time of your question, there is an open issue to add support for recognizing possible undefined variables inside of control flow blocks like your example.
Pylint does recognize variables that are definitely undefined before they are used, as in this example
print(x)
x = "Hello, world"
or this one
print(y)
if random.randint(0,10) == 3:
y = "ok"
What linting is PyCharm using by default? (I always thought it's pylint under the hood.)
PyCharm uses its own internal inspection library by default. PyCharm is implemented in Java and its inspection library is as well.
It is possible to use Pylint with PyCharm, but it is not built in or used by default. A solution for configuring Pylint as an external tool is shown here, and there is also a Pylint plugin for PyCharm available.
Related
The Summary
I have a python import that works when run from the VS Code terminal, but that VS Code's editor is giving warnings about. Also, "Go to Definition" doesn't work.
The Problem
I have created a docker container from the image tensorflow/tensorflow:1.15.2-py3, then attach to it using VS Code's "Remote- Containers" extension. Then I've created the following file in the container.
main.py:
import tensorflow.compat.v1 as tf
print(tf.__version__)
This runs fine in the VS Code terminal, but the Editor and the Problems pane both give me an unresolved import 'tensorflow.compat' warning. Also "Go to Definition" doesn't work on tf.__version__.
I'm using several extensions but I believe the relevant ones are the Microsoft Python extension (installed in the container), as well as the Remote - Containers extension, and now the Pylance extension (installed in the container).
The Things I've Tried
I've tried this with the default pylint, and then also after installing pylance with similar results. I've also seen some docs about similar issues, but they were related to setting the correct source folder location for modules that were part of a project. In contrast, my code within my project seems to work fine with imports/go-to-definition. It's external libraries that don't seem to work.
Also, for the sake of this minimal example, I've attached to the container as root, so I am guessing it's not an issue of elevated permissions.
I've also tried disabling all the extensions except the following, but got the same results:
Remote - Containers (local)
Remote - WSL (local)
Python (on container)
Jupyter (on container, required by Python for some reason)
All the extensions above are on the latest versions.
I've also fiddled around with setting python.autocomplete.extraPaths, but I'm not sure what the right path is. It also seems like the wrong thing to have to add libraries to the path that are installed in the global python installation, especially since I'm not using a virtual environment (it being in a docker container and all).
The Question
How do I fix VS Code so that it recognizes this import and I can use "Go to Definition" to explore these tensorflow functions/classes/etc?
tldr;
TensorFlow defines some of its modules in a way that pylint & pylance aren't able to recognize. These errors don't necessarily indicate an incorrect setup.
To Fix:
pylint: The pylint warnings are safely ignored.
Intellisense: The best way I know of at the moment to fix Intellisense is to replace the imports with the modules they are aliasing (found by importing alias in a repl as x then running help(x)). Because the target of the alias in my case is an internal name, you probably don't want to check in these changes to source control. Not ideal.
Details
Regarding the linting: It seems that tensorflow defines its modules in a way that the tools can't understand. Also, it appears that the package is an alias of some kind to another package. For example:
import tensorflow.compat.v1 as tf
tf.estimator.RunConfig()
The above code gives the pylint warning and breaks intellisense. But if you manually import the above in a REPL and run help(tf), it shows you the below package, which you can use instead:
import tensorflow_core._api.v1.compat.v1 as tf
tf.estimator.RunConfig()
This second example does not cause the pylint warning. Also the Intellisense features (Go to definition, Ctrl+Click, etc) work with this second example.
However, based on the _api, it looks like that second package name is an internal namespace, so I'm guessing it is probably best to only use this internal name for local debugging.
Confirmation/Tickets
pylint: I've found a ticket about pylint having issues with a couple tensorflow imports that looks related.
Intellisense: I've opened a ticket with pylance.
So for me I was trying to
import pandas as pd
but I got the error
"pd" is not accessedPylance (module) pd
SO what I did was reload the extension Python IntelliSense(Pylance) and that solved my issue.
I had the same problem but with all kinds of packages.
My solution was to go to the VSCode settings and search for "python.analysis.extraPaths", and add the path to your site-packages.
In my case, I added C:\Code\Python39\Lib\site-packages, and now it's working fine.
What, usually, solves the pylance issues for me is pointing my Python interpreter to the virtualenv one.
Open the command palette Ctrl + Shift + P
Type: Python: Select Interpreter
It will show a list of all the python Interpreters it actually detects:
Select Enter interpreter path
Type in the path to your local venv/bin folder or click find to navigate using the file explorer.
Your path should look something like:
venv/bin/python3.9
i changed "import tensorflow as tf" to "from tensorflow import compat as tf"
it ll even work for tf.gfile.Gfile()
I use VS Code to write and test python scripts.
Is it possible to make the editor aware of imported modules
to avoid problems listed like
Module 'numpy' has no 'divmod' member
You would update the Python interpreter settings according to where modules have been installed. (bottom right of VS Code)
The IDE being used isn't really relevant because you could invoke /path/to/bin/python, start a REPL, import the same module, and get the same error
Regarding, "numpy has no ... member", based on searching, that is a PyLint issue, not an import issue
How do I get PyLint to recognize numpy members?
I have the following method
def foo(bar):
print("hello world")
Edit: Running pylint in VSCode
[expected behavior]
pylint highlights 'bar' as an unused variable
[Actual Behaviour]
From VScode output
##########Linting Output - pylint##########
--------------------------------------------------------------------
Your code has been rated at 10.00/10 (previous run: 10.00/10, +0.00)
I have no additional pylint config setup
It turns out there is a setting in VSCode that enforces minimal checkers
Python › Linting: Pylint Use Minimal Checkers
I found this in pylint documentation:
possibly-unused-variable (W0641):
Possibly unused variable %r Used when a variable is defined but might not be used. The possibility comes from the fact that locals() might be used, which could consume or not the said variable
In your case, probably because you haven't use the bar argument inside your function. If you are not using it, just remove it for a cleaning code.
Check the oficial VS Code documentation about linting if you want to change this settings.
I am getting a unresolved import 'sys' linter warning in my VSCode Python program. I have other imports (json, numpy, etc.) that I don't have any issue with. I am using all the latest versions of Python, VSCode, and the extensions. It doesn't matter which linter I use, they all give me the same error. However, when I run the file it runs with no problem. Why is this happening and how can I fix it?
EDIT:
At the bottom in the PROBLEMS pane it says unresolved import 'sys' Python(unresolved-import)
Here is what pops up in VSCode when I put my cursor over sys:
(module) "/root/.vscode-server/extensions/ms-pyright.pyright-1.0.57/typeshed-fallback/stdlib/3/sys.pyi"
unresolved import 'sys'Python(unresolved-import)
Peek Problem
No quick fixes available```
Based on your description my suspicion is the warning is coming from the Microsoft language server (unfortunately you didn't provide the exact problem output which specifies what tool is providing the warning). If it is the language server then it's fixed in the beta release and thus will get fixed in stable eventually.
You may be able to find a solution in an answer like this one on setting up your Python Path in VS Code.
Another solution is to add your desired python interpreter installation folder to the PYTHONPATH environment variable.
That said, it does appear when you do not have a virtual environment setup (venv) this is a bug that will be fixed, per Brett's answer.
I use VS Code Version 1.19.3 with Python 2.7 on Windows.
Recently pylint (code analyzer) shown an error message
"E1601:print statement used"
But I don't know why! Can someone help me?
The print statement is correct as per my knowledge!
Is it a bug or a feature is missing?
Greetings niesel
The warning originates from Pylint, which is a very helpful tool for a dynamic language with loose syntax like Python. Since you are programming in Python 2.x where print is perfectly valid, I suggest you put a file in the root of your repo named .pylintrc and use it to configure Pylint.
To disable the print warning and leave everything else to the default, enter these two lines in your .pylintrc file:
[MESSAGES CONTROL]
disable=print-statement
You will also need to tell Visual Studio Code to use your configuration file by opening your workspace or user settings and add this:
{
"python.linting.enabled": true,
"python.linting.pylintEnabled": true,
"python.linting.pylintArgs": [
"--rcfile=/path/to/.pylintrc"
]
}
More options
To get a good idea of available configuration options open a terminal/prompt and run this command to generate a sample configuration file:
pylint --generate-rcfile > sample_pylintrc
The problem is, that changing from print statement to print function doesn't help much. So it seems, that it is some bug in VS Code Python module (2018.1 (01 Feb 2018)), as after this update I've found the same problem in my VS Code within my old projects
I've found reffered bug on their github
PS: vscode-python has changed pylint options since 2018.1. In order to return old behavior you may disable python.linting.pylintUseMinimalCheckers option for the workspace or for the userspace.
it's not an error per-se, it's just PyLint complaining about those legacy statements. PyLint will also complain about missing spaces before commas, those kind of style errors.
PyLint is there to warn you about possible problems. Your code will break when running python 3, so it warns you before it happens.
Note that print is a statement in python 2.x (which explains the message), and became a function in python 3.x.
Fix it by changing to:
print("test")
Since it's not a tuple, it works fine and does exactly the same for all versions of python, and PyLint will stop complaining.
you can also get rid of PyLint altogether: Windows 10 - Visual Studio Code - removing pylint (not sure if it's a good idea)