- Where can I run Python code?
- Writing Python code in RoboFont
- Python in Terminal
- Python in SublimeText
Where can I run Python code?
Python can be used in many different environments. Some applications already come with an embedded Python interpreter, so you can use Python as a scripting language to automate things that you would otherwise have to make ‘by hand’ (clicking around in menus and icons).
Writing Python code in RoboFont
RoboFont comes with a handy Scripting Window, a simple code editor where you can write and run your Python code.
DrawBot is an enviroment to create 2D graphics with Python code. It was originally developed by Just van Rossum to teach programming to design students at the KABK.
DrawBot is also available as a RoboFont extension. This version is specially useful for typeface designers: because it lives inside the font editor, it has direct access to the full RoboFont API, so you can use the current font objects directly in your scripts.
Python in Terminal
Python can be used directly in a console or command-line interface, outside of an existing application.
On Mac OS X, this can be done via the Terminal, which can be found in your Applications / Utilities folder.
After launching Terminal, you will see something like this:
Last login: Wed Jul 23 05:01:17 on ttys000 username:~ username$
This is your default command-line prompt, with your own username.
There are two ways to use Python on the console: interactive mode, or running an existing script.
To enter interactive mode in Terminal, simply type
python in the command-line:
username:~ username$ python
You will probably see something like this:
>>> Python 2.7.5 (default, Aug 25 2013, 00:04:04) >>> [GCC 4.2.1 Compatible Apple LLVM 5.0 (clang-500.0.68)] on darwin >>> Type "help", "copyright", "credits" or "license" for more information. >>>
This command gives us some information about the current Python installation, and returns an interactive Python prompt.
We can type Python code in this prompt, and it gets executed as it goes.
Let’s start with a simple hello world:
>>> 'hello world'
The same goes for mathematical expressions, in fact for any Python expression:
1 + 1
In interactive mode, it is not necessary to use the
It is also possible to write multi-line code in the interactive mode. Notice how the prompt changes to indicate that it is waiting for input:
for i in range(7): ... print(i,) ...
>>> 0 1 2 3 4 5 6
Running existing scripts
The interactive mode is useful for writing quick tests and short scripts, but it is not really suitable for working with larger pieces of code. When that is the case, it is better to write scripts as separate
.py files, and use Terminal only to run them.
Let’s suppose we have the following Python script, which prints “hello world” as output:
This script is stored as a separate
To call this file from Terminal, we simply call the command
python, followed by the path to the script file. A tip: instead of typing out the full path, you can simply drag the file from Finder to the Terminal:
username:~ username$ python /Users/username/Desktop/hello.py
The output will be, as expected:
>>> hello world
Python in SublimeText
It is also possible to run Python scripts from inside SublimeText (our favorite code editor). To run a script, use the key combination
Cmd+B — this will execute your code and print the output to the console at the bottom of the editor.