Python

Python is an interpreted scripting language available on a wide variety of platforms, including Linux, Mac OS X, and Microsoft Windows.

Learning Python

Free E-books

Dive Into Python is considered to be one of the best free introductions available, although it was last updated in 2004. Nevertheless, It provides good coverage on all the language basics, and common tasks such as web requests and file operations. For more advanced topics, Text Processing In Python is an excellent reference and tutorial.

Other free ebooks and online sources include:

Once you've learned the basics of the language, Code Like a Pythonista: Idiomatic Python is a tutorial that will help you acclimate to specific aspects of Python that differentiate it from other programming languages.

Free Online Courses

Python in Mozilla-based applications

XPCOM in Mozilla is used to support inter-language communication. Out-of-box it only supports C++ <-> JavaScript communication. The Python XPCOM package (also called PyXPCOM) is the low-level glue that ties Python and Mozilla together, letting XPCOM components written in JavaScript or C++ to be used from Python and vice versa. PyXPCOM is not included in the default Firefox build, so you'll need to use a third-party build or build yourself to use it. The most known consumer of PyXPCOM is the Komodo family of products.

Starting with Mozilla 1.9, Python DOM (PyDOM) bindings are implemented. This lets chrome XUL and HTML authors use Python in their <script> tags (again, not in the official Firefox/Thunderbird builds).

Python-based tools for Mozilla development

Python is used by Mozillians for tools that do various things with Mozilla apps and infrastructure. It would be useful to have a document on Python Environment and Tools for Mozilla.

Tools are listed here: http://k0s.org/toolbox/?language=python

Use of Python at Mozilla

Mozilla has considerable infrastructure based on python:

Python in Mozilla-Central

[Stub: see https://bugzilla.mozilla.org/show_bug.cgi?id=835553]

Much of the build system, the testing system, and other tools and infrastructure in mozilla-central are python.

  • python/ contains common python code including third party packages mirrored from (e.g.) pypi.python.org
  • testing/mozbase/ contains a mirror of Mozbase packages as used in mozilla-central

A virtualenv is made in the objdir on invocation of the build at $OBJDIR/_virtualenv . To add packages to the virtualenv, edit build/virtualenv_packages.txt . These are installed on build via build/virtualenv/populate_virtualenv.py .

Python packaging

Python uses setup.py files to record metadata and installation instructions for python packages. Running (e.g.) python setup.py install will install the package, making its modules available on python's import path. For python 2.x, several distribution/installation modules exist. distutils is the only distribution package available in python's standard library. distutils has ability to upload to the python package index and to install python packages. See the Python documentation on distutils for details.

While distutils is built in to python's standard library, setuptools is a third-party ad hoc standard for packaging and distribution. It is mostly compatible with distutils, but importantly adds the ability for packages to include dependencies that are installed as prerequisites at the time setup.py is invoked as well as the ability to install python packages in development mode. This allows the files to be edited in place via .pth files which is handy if you are actively working on a project. setuptools also provides an easy_install script for installing packages and their dependencies through the web from PyPI. For instance, in order to install the PyYAML package, just run

easy_install PyYAML

Since setuptools is not included with python, you will need to install it in order to use it. You may install it from the setuptools PyPI page by downloading, extracting, and running python setup.py install. Or you can use the ez_setup.py script. You can download and run it with python (with root/Administrator privileges), or if you're in a bash shell, you can run

sudo python <(curl http://peak.telecommunity.com/dist/ez_setup.py)

setuptools is also provided with instances of virtualenv, so if you use virtualenvs for developing you may not need to install setuptools globally. distribute is a fork of setuptools written by Mozilla's own Tarek Ziade. It is compatible with setuptools and fixes a few bugs there.

Note: It's highly recommended that you use virtualenv for development!

The Python Package Index (PyPI) is the standard distribution point for python packages. If you need some functionality in python, it is a good place to look!

See also: http://k0s.org/portfolio/packaging.html

See also

Document Tags and Contributors

Last updated by: mcomella,