Sublime Forum

Check for Python NameErrors... [tutorial on how to set up linter]

#1

Is there a plugin that I can use to check that the variables I use are defined? Python catches some of these upon import, but not all of them.

I have SublimeLinter installed.

for column_nam in columns:
    column_name = str(column_name)

    print(f"{col_n}")

image

Here column_name and col_n should be undefined.

0 Likes

#2

You don’t need a plugin for such a thing, just cach and handle the NameError exception which is raised if a variable is undefined.

try
  ...
except NameError:
  # handle error

If you want error outputs while editing sources, you might want to try LSP with LSP-pyright or pyls.

SublimeLinter should do the same if the proper python linter is configured though. Not sure if it is out of the box though. Haven’t used the package for ages.

3 Likes

#3

Yes, I am trying to get my text editor to highlight these errors for me since import the module is not catching it.

It does not look like SublimeLinter catches this even when ShowAllErrors or Lint This View commands are ran on a .py file. I’ll check it’s settings/ config.
image

0 Likes

#4

SOLVED

Turns out SublimeLinter is just an API into the Sublime editor for other linters (or something like that). You need to install specific linters for your language. I installed “SublimeLinter-pyflakes” plugin via command palette, then bash pip instal pyflakes on my system’s python (not virtualenv), ran bash which pyflakes which output “/Users/me/.pyenv/shims”, and I added that to SublimeLinter.sublime_settings -- User[‘paths’][‘osx’][], and finally ran via command palette “SublimeLinter: Reload SublimeLinter and its Plugins”

{
    "paths": {
        "osx": ["/Users/me/.pyenv/shims"],
    },
}

yikes.

0 Likes