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).
Python can also be used directly in the Terminal, to run existing scripts or in interactive mode.
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.
The current version of DrawBot is developed in collaboration with Frederik Berlaen, who is also the author of RoboFont (see below). To learn more about DrawBot, have a look at the DrawBot documentation. For the brave, DrawBot’s source-code is available on Github.
Similar applications have been developed for other enviroments, such as ShoeBot for Linux and Spryte for Windows. But the syntax used by these applications is sometimes different, breaking compatibility with DrawBot scripts.
RoboFont has a Python scripting environment, and is itself written in Python. Have a look at the RoboFont documentation to learn more.
Many aspects of the program’s interface and behaviour can be customized with code. Documentation about the RoboFont API can be found here.
In FontLab, Python scripts can be written and run with help of the ‘Edit Macro’ window.
FontLab itself is not written in Python, but it has a Python API which gives access to most of its functions. However, this API is not very well documented, and is not very pythonic.
As an alternative, FontLab can also be scripted with help of the RoboFab library, which first needs to be downloaded and installed. RoboFab offers a more natural API, and also makes it possible to write scripts that work outside of FontLab (NoneLab).
Python in Terminal¶
Python can be used directly in a console or command-line interface, outside of an existing application.
On MacOSX, 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" 'hello world'
The same goes for mathematical expressions, in fact for any Python expression:
>>> 1 + 1 2
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:
print 'hello world'
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:
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 start your file in the console at the bottom of the editor.