Setup Sublime Text 3 for Python
- 2 minsPackages and Setup
- Sublime Text 3 Setup - Most Important Packages
- Setting Up Sublime Text 3 for Full Stack Python Development
Type Checker: mypy
1. Install mypy
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)
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,
}
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:
- Append
# type: ignore
after the module name:
import numpy as np # type: ignore
- On the command line:
mypy --ignore-missing-import test.py
This has the drawback that it will ignore mistakes as well (misspelling the package name, for example)
- We can create a
.mypy.ini
configuration file in our home folder~
(or amypy.ini
in the folder where our project is) with the following content:
# 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
Reference
Guide: Type Hinting in Python 3.5