Boost Productivity: Run Python in Sublime, View Images in iTerm2, and Leverage Python typing
This tutorial shares three handy tools—a Sublime Text build system for running Python scripts, an iTerm2 imgcat setup for displaying images directly in the terminal, and a concise guide to Python's typing module for better type safety and code readability.
This article introduces three useful tools to improve development efficiency: running Python code directly in Sublime Text, displaying images in iTerm2 on macOS, and using Python's typing module for type annotations.
Run Python in Sublime Text
View images in iTerm2
Python typing module overview
Running Python in Sublime Text
Instead of opening a full IDE for quick scripts, you can configure Sublime Text to execute Python files. Open Tools → Build System → New Build System… and replace the default content with the following JSON, adjusting the path to your Python interpreter.
{
"cmd": ["D:/Anaconda3/python3.7.exe", "-u", "$file"]
}Save the file (e.g., Python3.7.sublime-build) in the default location. The new build system will appear in the Tools menu; select it and press Ctrl+B (or Build ) to run the current script.
Example script test.py:
import numpy as np
matrix = np.array([[0, 1, 2],
[2, 4, 5]])
print(matrix[1, 2])
print("Hello world from Sublime Text.")Running the script with Ctrl+B produces the expected output, as shown below.
The same configuration works on Windows, providing a lightweight way to test short Python snippets.
Viewing Images Directly in iTerm2 on macOS
iTerm2 supports inline image display via the imgcat utility. Create a shell script imgcat.sh following the instructions at iTerm2 imgcat , then make it executable with chmod u+x imgcat.sh. Running the script with an image path will render the picture inside the terminal.
Introducing Python’s typing Module
The typing module adds optional type hints to Python code, enabling static type checking, better documentation, and improved readability without affecting runtime behavior.
Example: a function that sums the digits of a numeric string.
from typing import *
# Create function
def digits_sum(num: str) -> int:
digits_arr = map(lambda x: int(x), num)
return sum(digits_arr)
# Test
num = "352"
result = digits_sum(num=num)
print(result)Output: 10. Another example demonstrates type‑annotated dictionaries.
from typing import Dict, Any
# Create function
def dict_multipy(d: Dict[str, Any]) -> Dict[str, float or int]:
new_dict = {}
for k, v in d.items():
if isinstance(v, (float, int)):
new_dict[k] = v * 2
return new_dict
# Test
d = {"no": "100", "age": 12, "work_year": 3, "name": "JC"}
new_d = dict_multipy(d=d)
print(new_d)Output: {'age': 24, 'work_year': 6}. You can also create type aliases, such as defining Vector = List[float] and using it in functions.
from typing import List
# Alias List[float] as Vector
Vector = List[float]
def scale(scalar: float, vector: Vector) -> Vector:
return [scalar * num for num in vector]
new_vector = scale(2.0, [1.0, -4.2, 5.4])These examples illustrate how typing can make code clearer and help catch type‑related errors early.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
MaGe Linux Operations
Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
