5

Is there a simple way, preferably in PyCharm (2017.1) but via command-line python (3.5) if necessary, to detect all code places where a statement is referring to an unresolved reference, e.g. because an import statement is missing?

I am new to Python/PyCharm. More generally, any syntax errors or anything in a similar vein would be a bonus. All I am looking for is the kind of errors I would get if I were "compiling" and "linking" in another language.

I have looked at Can PyCharm list all of Python errors in a project? and PyCharm's "Inspect Code". It is way more complex than I had in mind (and takes ages to run). I see that Python Rope: How to Find all missing imports and errors in all sub modules refactoring recommends pylint, but I wasn't looking for lint-like. I just want darn-obvious errors!

I am tasked with porting a fair-size (32K lines) application, which (apparently) runs under Windows, to Linux. The first thing I want to do is get rid of some of the imports all over the place. If my application executes a line which then has an unresolved reference I get a runtime error, but I want to pick them all up at edit-time. And there will be paths of code which are Windows-only, but I still want to know of any errors like this.

Community
  • 1
  • 1
JonBrave
  • 4,045
  • 3
  • 38
  • 115
  • I doubt it's doable all at once, given that Python's more or less an interpreted language and it's automatically given that if there's an unknown reference, the interpreter would immediately abort the process. – LFlare Mar 30 '17 at 17:43
  • I'm pretty sure PyCharm already does this inspection on the fly, although I don't know if there's a way to run it in batch mode over your whole codebase and get a list of all detections. – user2357112 Mar 30 '17 at 17:46

2 Answers2

6

To answer my own question:

From Can PyCharm list all of Python errors in a project?, you can indeed use Code|Inspect Code to get all these errors/warnings in PyCharm as a list where you can click to get to the code. It does take a long time, but at least it's built into PyCharm, and the errors reported correspond to what you see in the editor window.

JonBrave
  • 4,045
  • 3
  • 38
  • 115
1

Pycharm will list all errors and 'warnings' for each source file at the right-hand side of the editor window.

They are represented as short lines or small blocks, depending on the size of the error or 'warning'. Errors are shown in red.

You click on them to take you to the place of the problem in the source.

Warnings are mostly Python style-guide violations (PEP 8).

Another option is to use pylint. This is lint for Python. This will detect missing imports.

You can integrate it into PyCharm by following the instructions in https://stackoverflow.com/a/46409649/4459346

NZD
  • 1,780
  • 2
  • 20
  • 29
  • 1
    Yes, I am aware of this, and believe it is the only way. However, this relies on visually scrolling through every line of every file to spot them, across hundreds of files and tens of thousands of lines. I was looking for a *list* of such errors. – JonBrave Apr 10 '18 at 07:17
  • 1
    @JonBrave Added another solution: Use pylint inside PyCharm – NZD Apr 12 '18 at 00:50
  • Thank you for that link. I'd rather it had been against the actual checks & blocks PyCharm itself is generating, but beggars can't be choosers! I was about to moan that I wouldn't be able to click on each reported warning to get to the code, but I notice in the same thread as your link that answer https://stackoverflow.com/a/48549144/489865 claims you will get hyperlinks for these. I will try when I next have time. – JonBrave Apr 12 '18 at 07:58