Processing.py on the command line
This tutorial is for Processing's Python Mode. If you see any errors or have comments, please let us know. This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.
Most of the material online about Processing.py is oriented to Python Mode, which allows you to write Processing sketches in the PDE using the Python programming language. But Processing.py also provides a way for you to write and launch sketches from the command line. Why use the command line? The PDE is great, of course, and many people use it for writing interactive applications from sketch to finished product. But there are some scenarios in which you might want to use the command line: for easier integration with collaborative version control tools; for more control over how your application gets packaged; for easier interoperability with other tools that live on the command-line; etc. Or maybe (like me) you just prefer working on the command line to working in a GUI. Whatever your reason, this tutorial will show you how to get up and running. These instructions are tested on OSX El Capitan, but should work on other platforms as well. RequirementsIn order to run Processing.py from the command line. For maximum compatability with processing, you'll need to install Java 8 -- specifically, we recommend JRE 8u202 from Oracle. From the Processing Build Instructions: Other versions of Java are not supported. It must be 8u202. Later versions of Java (9, 10, et cetera) don't work either. OpenJDK is also not supported. Really. Please don't use it (and then complain when things break). After installing, ensure that you can run Java by typing the following on the command line:
You should see some output that looks like this:
You'll also need to obtain the standalone version of Processing.py. Choose the appropriate link according to your platform: These files contain the Processing.py JAR file along with some example scripts and some utility programs for running Processing.py on its own, for various platforms. Decompress the file, making note of the location of the resulting directory. The basicsOnce you've
installed Java and decompressed the
Save the file in the same directory as the
The command above runs the Java interpreter ( After typing the above and hitting return, you should see a window appear
with the If something goes wrong...If the window did not appear, then something went wrong. Make sure that you
typed the command as specified, spaces and all (note that there's a space
between Any syntax errors or runtime exceptions in your program will be printed to the terminal. Java command-line parametersWhen you run The most important parameters for you to know about right from the beginning
are For example, to run Processing.py requesting a starting heap size of 256 megabytes and a maximum heap size of 1 gigabyte:
Included helper scriptsThe distribution you downloaded comes with a number of "helper scripts" for running Processing.py on the command line. These are:
The benefit of using these programs is that you can launch them directly
from your GUI's filesystem navigator without having to navigate to the
appropriate directory, etc. All of these helper scripts, by default, launch a
sketch called Loading media and other dataThe Processing language supports several functions, like
By default, functions like
... will first attempt to load An additional wrinkle with Processing.py is that Python's built-in
... will attempt to open a file named Note: For compatibility purposes, and to avoid any unusual behavior when
your sketch is bundled as a standalone library, we recommend keeping your
external media in the External librariesJust like Python Mode in the Processing IDE, Processing.py on the command line can make use of third-party libraries. Processing librariesProcessing (the vanilla Java version) has a rich library of third-party contributed libraries. The Processing IDE has a helpful "Add Library" feature that allows you to locate, download and use these libraries in your project using a GUI interface. Of course, on the command line, that option isn't available to you! So here's what you do:
If you're having trouble locating the right file to download for a
particular library, there's a helpful alternative technique. Use the Processing
IDE's "Add Library" feature to download and install the library for
use in the IDE. Then find the folder where the IDE keeps the libraries you've
downloaded by going to the IDE's Preferences panel and clicking the
"Browse" button next to the "Sketchook Location" field.
Take a look inside the Once you've copied the files to the correct location, use Processing.py's For example, to use the Processing Sound
library, download the ZIP file from the
repository's "releases" page. Unzipping the file should
result in a directory called
Python codeUse the It's important to note that Processing.py uses the pre-packaged Jython standard library, and (by default) can't see or use any of the Python libraries in your own system installation of Python. See this tutorial on using Python libraries in Processing.py for more information. Because Processing.py is built with Jython, it might also be helpful to consult Jython's documentation on modules and packages. Other Java librariesProcessing.py is built with Jython, which is built with Java, which means you can also regular Java libraries in your Processing.py sketches. (This is true whether you're using Python Mode in the IDE or Processing.py from the command line.) Just for starters, all of the Java standard API is available to you; here's a sample sketch that opens a Swing window:
Consult the Jython Book's chapter on modules and packages for more information. You can also use third-party Java libraries. The easiest way to do this is to download the library's JAR file and put it in the same directory as your Processing.py sketch. (The command-line launcher for Processing.py automatically adds whatever JARs it finds in the sketch's directory to Java's classpath.) A forthcoming tutorial will show a full example of using a Java library in a Processing.py sketch. Command-line parameters and environment variablesUnfortunately, the way that the Processing.py command-line launcher is
written makes it impossible to supply extra parameters to your script on the
command line. As a workaround, you can use environment variables. For example,
here's a sketch that reads the environment variables
On the OS X or Linux command line, you can set the environment variables and
run the sketch simultaneously by typing something like this (assuming you've
put the code above in a file named
Moving a project from the IDE to the command line (and vice versa)You can run any sketch you create in the Processing IDE on the command line.
To do this, you'll first need to find the directory on your computer where the
sketch files are using the "Show Sketch Folder" option in the
"Sketch" menu of the IDE. Copy the After you've done this, run the Java interpreter as you would for any
command-line Processing.py sketch. The filename to supply on the command line
is the main
If you have an existing Processing.py sketch that you wrote on the command line, it's possible to "import" it back into the Processing IDE. For short sketches, the easiest way to do this is probably to just copy and paste the source code into a new Processing IDE project. For more sophisticated sketches, you can create a sketch in the format the IDE expects "by hand" using the following steps:
Building a standalone applicationThe command-line version of Processing.py has had, in the past, a feature to create a standalone application bundle. Unfortunately, as of this writing this feature is broken on some platforms. For now, the easiest way to create a stand-alone application with a command-line Processing.py sketch is to import it into the Processing IDE (as outlined in the previous section) and use Python Mode's "Export Application" feature, which works just fine.
This tutorial is for Processing's Python Mode. If you see any errors or have comments, please let us know. This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License. |
This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License