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 internet 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 Mac platforms usually ship with a Python Interpreter already installed. Windows users will need to download and install the interpreter if they want to use Python outside of programs such as the ones listed above. 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.0. Maya 8.5 uses Python 2.4. Maya 2008 uses Python 2.5. Linux and Mac users could have older versions shipped with their machines so they’ll need to upgrade if they want access to newer Python functionality.
Python is available in two download types: installer and source code. The installer (an .msi file or .dmg file) is what most people will download to get Python on their machine. Source code is used by Linux users and super dorks. Python is a scripting language that was created with another programming language and the source code contains the files necessary to build and install it on Linux machines. Ignore the source download and just get the installer for now.
What is a Python Script?
A Python script is simply a bunch 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.

Figure 1 - 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, 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
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. An easy way to add directories to your Maya scripts directories 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. 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.

