JonBrave
JonBrave

Reputation: 4280

Detect all missing imports in PyCharm/Python

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.

Upvotes: 5

Views: 7194

Answers (2)

JonBrave
JonBrave

Reputation: 4280

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.

Upvotes: 6

NZD
NZD

Reputation: 1970

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

Upvotes: 1

Related Questions