Introduction to Python

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.

IDLE

Figure 2 - Script Editor

Figure 3 - Command 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.

Figure 4 - A Simple Python Script

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.txt
Another 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. Figure 5 - Running a Script from IDLE

Shebang Lines

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:

$ myFirstPythonScript.py

To use a shebang line, just add the following to the first lines of your script:

#!/usr/bin/env/ python

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.

Figure 6 - Calling a Script from Maya

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.

Figure 7 - maya.env File

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:

Figure 8 - Re-importing a Module

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:

Figure 9 - Reloading a Module

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.

Python Modules

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.

6 thoughts on “Introduction to Python”

Hazy June 13, 2011 at 4:45 am

Hey Chad,

I’m a complete novice and find this introduction pure gold in my faltering steps towards python fluency.

I really like the script above but am struggling to understand how to define which directory it will work on.

Should I be extending the script, importing sys and using argv to pass in a path argument when I run the program? Thanks in advance
Hazy

Hazy June 13, 2011 at 4:48 am

Sorry Chad,
I meant to post the above comment on the sample script page.
Hazy

Mambo4 April 24, 2012 at 12:25 pm

“To interface with Maya, you import the maya.cmds module”

In my script editor (Maya2012 64) I get this:
import(maya.cmds)
# Error: SyntaxError: invalid syntax #

Mambo4 April 24, 2012 at 12:57 pm

duh… no parenthesis…

import maya.cmds

Darragh August 21, 2013 at 11:03 am

Great page this!

Just out of interest do you use an external script editor and if so which one?

Chad August 21, 2013 at 11:52 am

I usually just use gvim. Nothing fancy.

Leave A Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

<code> For inline code.
[sourcecode] For multiline code.