Setup Sublime Text 3 for Python

- 2 mins

Packages and Setup


Type Checker: mypy

1. Install mypy

Documet

python -m pip install mypy-lang

Alternatively, you can

pip install mypy-lang

2. Test mypy

mypy test.py

Error pops up:

Traceback (most recent call last):
  File "//anaconda3/bin/mypy", line 10, in <module>
    sys.exit(console_entry())
  File "//anaconda3/lib/python3.7/site-packages/mypy/__main__.py", line 4, in console_entry
    main()
TypeError: main() missing required argument 'script_path' (pos 1)

Solution:

main(None, sys.stdout, sys.stderr)

3. Install Anaconda

Package Control -> Install Package -> Anaconda

4. Configure Anaconda

Preferences -> Package Settings -> Anaconda -> Settings-User

{

    "python_interpreter": "/usr/bin/python",
    "anaconda_linting_behaviour": "load-save",
    "anaconda_linter_phantoms": true,
    "mypy": true,
}

Disable Anaconda linting

5. SublimeLinter-contrib-mypy

Instruction:
(1) Package Control -> Install Package -> SublimeLinter
(2) Package Control -> Install Package -> SublimeLinter-contrib-mypy

6. mypy configuration file

When importing third party module, import numpy for instance, it would pop up an error:

mypy: errorNo library stub file for module 'numpy'
mypy: note(Stub files are from https://github.com/python/typeshed)

That’s because Numpy does not have stable type hints at this time.

Three ways to fix it:

import numpy as np  # type: ignore
mypy --ignore-missing-import test.py

This has the drawback that it will ignore mistakes as well (misspelling the package name, for example)

# mypy.ini
[mypy]
[mypy-numpy]
ignore_missing_imports = True

Run Python

Run Python3 on Sublime Text 3:
Tools -> Build System -> New Build System:

UNIX

{
 "cmd": ["/usr/local/bin/python3", "-u", "$file"],
 "file_regex": "^[ ]File \"(...?)\", line ([0-9]*)",
 "selector": "source.python"
}

WINDOWS

{
 "cmd":["C:/Users/<user>/AppData/Local/Programs/Python/Python37-32/python.exe", "-u", "$file"],
 "file_regex": "^[ ]File \"(...?)\", line ([0-9]*)",
 "selector": "source.python"
}

Save this file as new-python.sublime-build

Go to Tools -> Build System -> Python
Press CTRL + B to run the code on Sublime


Terminal

Terminus


Reference

Guide: Type Hinting in Python 3.5

Zhijian Liu

Zhijian Liu

A foodaholic

comments powered by Disqus
rss facebook twitter github gitlab youtube mail spotify lastfm instagram linkedin google google-plus pinterest medium vimeo stackoverflow reddit quora quora