What is Python?
Python is a general purpose scripting language used in many different industries. It is a relatively easy to use and easy to learn language. Python is used in web services, hardware testing, game development, animation production, interface development, database programming, and many other domains.
The Python Interpreter
How does the computer run a Python program? How can you get the computer to understand the Python commands you write in a text file? The Python Interpreter is the software package that takes your code and translates it into a form that the computer can understand in order to execute the commands. This translated form is called byte code.
The Python Interpreter can be downloaded and installed for free from the Python website (http://www.python.org/download/). Linux, Unix, and OSX platforms usually ship with a Python Interpreter already installed. Linux users can also use yum or apt-get to install Python. OSX users can use homebrew or macports to install Python. Windows users can either use Chocolatey to install Python or download and install the interpreter if they want to use Python outside of programs that come with an Interpreter like Maya. Maya 8.5 and later has a Python Interpreter built in so you could learn to use Python inside the script editor of Maya.
There are different versions of the Python Interpreter. At the time of this writing, the latest version is 3.5.1. Maya 2016 uses Python 2.7. There are two main flavors of Python: the 3.x series and the 2.x series. The 3.x series is the latest and greatest but the 2.x series is the most widely supported. You can search online about the differences but Python 2.x is the what you’ll mostly encounter in the CG industry.
What is a Python Script?
A Python script is simply a series of commands written in a file with a .py extension. This text file is also called a Python module. This .py file can be written in any text editor like Note Pad, Word Pad, vi, emacs, Scite, etc. However, you do not always have to write your code in a .py file. For quick tests and scripts, you can run code directly from the Maya script editor or from the interactive prompt.
The Interactive Prompt
Interactive prompts allow you to execute code on the fly and get an immediate result. This is a good way to experiment with chunks of code when you are just learning Python. When you download and install Python, you will see that Python ships with its own editor and interactive prompt called IDLE. IDLE is a convenient editor and prompt with syntax highlighting and is all you really need in order to learn Python when just starting out. The following 3 figures show different ways of accessing an interactive prompt.
Running Python Scripts From a Command Prompt
Most of the code you write will be in external .py files like the one shown below.
To run this script from a command prompt or terminal window, you call the script with the python command (which is on your system once you install Python).
D:\>C:\Python26\python myFirstPythonScript.py You are running a python script. 8 You can add text together.
If you have the PATH environment variable (Google search “path environment variable”) set to include Python (which should happen by default with the installer), you don’t need to specify the path to the Python executable.
D:\>python myFirstPythonScript.py You are running a python script. 8 You can add text together.
You can also route the output of your script to a file to save it for later use:
D:\>python myFirstPythonScript.py > output.txtAnother way to run a Python script is to open it in IDLE and run it from within the editor as shown below. This is a great way to quickly experiment with Python code as you learn and write new scripts and applications.
On Linux and OSX, you can use something called a shebang line to allow you to run a Python script without having to specify python. For example, you could just run:
To use a shebang line, just add the following to the first lines of your script:
Running Scripts in Maya and within a Python Session
When we are in Maya, we cannot call a script with “python myScript.py”. The python command starts up the Python interpreter and runs the given script with the interpreter. When we are in Maya, the Python interpreter is already running. To call a script from inside Maya and other scripts, you need to import it.
When you import a Python module, you leave off the .py extension. How does Python know where to find the script? Python uses an environment variable called PYTHONPATH to find scripts. The PYTHONPATH variable contains all the directories Python should search in order to find an imported module. By default, inside Maya your scripts directories are added to the PYTHONPATH. Many studios these days take advantage of Maya’s module system which handles adding script directories to the PYTHONPATH. You can read about Maya’s module system in the documentation here.
Another way to add directories to the PYTHONPATH is to create a Maya.env file. The Maya.env file is a file that modifies your Maya environment each time you open Maya. Place the Maya.env file in your “My Documents\maya” folder.
Consult the Maya documentation for all the other variables you can set in the maya.env file. If you have multiple scripts with the same name in different directories, Python will use the first one it finds.
Notice when I import the module again, the script does not run:
Importing a module only works once per Python session. This is because when you import a module, Python searches for the file, compiles it to byte code, then runs the code, which is an expensive process to execute multiple times. Once a module is imported, it is stored in memory. To run a script again or if you’ve updated a script and wish to have access to the updates, you need to reload it:
Notice that when I reload the Python module, the result states it read the module from a .pyc file. A .pyc file is a compiled Python file. When you import a Python module, Python compiles the code and generates a .pyc file. You could distribute these .pyc files if you do not want people looking at your code. Note however there are tools available on the internet that will easily decompile a pyc back into source code. Import statements will work with .pyc files.
As you can see, Python modules are simply Python scripts that contain specific functionality. Since Python is so widely used, you can find thousands of free Python modules on the internet that implement various tasks such as networking, image manipulation, file handling, scientific computing, etc. To interface with Maya, you import the maya.cmds module, which is the module that ships with Maya to implement all the Maya commands.