Python

  • Revision slug: Python
  • Revision title: Python
  • Revision id: 62301
  • Created:
  • Creator: jhammel
  • Is current revision? No
  • Comment one or more formatting changes

Revision Content

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

Learning Python

Dive Into Python is the best introduction available. 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.

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 also beginning to be used more for scripts that do various things with Mozilla apps. It would be useful to have a document on Python Environment and Tools for Mozilla.

Tools are listed here: http://brasstacks.mozilla.com/toolbo...anguage=python

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.  More details can be found here: http://docs.python.org/distutils/index.html .

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 developer 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.

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

Links

- http://docs.services.mozilla.com/ser...e/release.html

Revision Source

<p><a class="external" href="http://www.python.org">Python</a> is an interpreted scripting language available on a wide variety of platforms, including Linux, Mac OS X, and Microsoft Windows.</p>
<h2 name="Learning_Python">Learning Python</h2>
<p><a class="external" href="http://www.diveintopython.org/toc/index.html">Dive Into Python</a> is the best introduction available. It provides good coverage on all the language basics, and common tasks such as web requests and file operations. For more advanced topics, <a class="external" href="http://gnosis.cx/TPiP/">Text Processing In Python</a> is an excellent reference and tutorial.</p>
<h2 name="Python_in_Mozilla">Python in Mozilla-based applications</h2>
<p><a href="/en/XPCOM" title="en/XPCOM">XPCOM</a> in Mozilla is used to support inter-language communication. Out-of-box it only supports C++ &lt;-&gt; JavaScript communication. The <a href="/en/PyXPCOM" title="en/PyXPCOM">Python XPCOM package</a> (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<strong> not</strong> 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.</p>
<p>Starting with Mozilla 1.9, Python DOM (<a class="internal" href="/en/PyDOM" title="En/PyDOM">PyDOM</a>) bindings are implemented. This lets <a class="internal" href="/en/Chrome" title="En/Chrome">chrome</a> XUL and HTML authors use Python in their &lt;script&gt; tags (again, not in the official Firefox/Thunderbird builds).</p>
<h2>Python-based tools for Mozilla development</h2>
<p>Python is also beginning to be used more for scripts that do various things with Mozilla apps. It would be useful to have a document on <a href="/en/Python_Environment_and_Tools_for_Mozilla" title="en/Python_Environment_and_Tools_for_Mozilla">Python Environment and Tools for Mozilla</a>.</p>
<p>Tools are listed here: <a class=" external" href="http://brasstacks.mozilla.com/toolbox/?language=python" rel="freelink">http://brasstacks.mozilla.com/toolbo...anguage=python</a></p>
<h2>Python Packaging</h2>
<p>Python uses <a class="external" href="http://docs.python.org/distutils/index.html" title="http://docs.python.org/distutils/index.html">setup.py</a> files to record metadata and installation instructions for <a class="external" href="http://docs.python.org/tutorial/modules.html#packages" title="http://docs.python.org/tutorial/modules.html#packages">python packages</a>. Running (e.g.) <code>python setup.py install</code> will install the package, making its modules available on <a class="external" href="http://docs.python.org/tutorial/modules.html#the-module-search-path" title="http://docs.python.org/tutorial/modules.html#the-module-search-path">python's import path</a>. For python 2.x, several distribution/installation modules exist. <a class="external" href="http://docs.python.org/distutils/index.html" title="http://docs.python.org/distutils/index.html">distutils</a> is the only distribution package available in <a class="external" href="http://docs.python.org/library/" title="http://docs.python.org/library/">python's standard library</a>. distutils has ability to upload to the <a class="external" href="http://pypi.python.org/pypi" title="http://pypi.python.org/pypi">python package index</a> and to install python packages.  More details can be found here: <a class=" external" href="http://docs.python.org/distutils/index.html" rel="freelink">http://docs.python.org/distutils/index.html</a> .</p>
<p>While distutils is built in to python's standard library, <a class="external" href="http://peak.telecommunity.com/DevCenter/setuptools" title="http://peak.telecommunity.com/DevCenter/setuptools">setuptools</a> 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 <a class="external" href="http://peak.telecommunity.com/DevCenter/setuptools#declaring-dependencies" title="http://peak.telecommunity.com/DevCenter/setuptools#declaring-dependencies">include dependencies</a> that are installed as prerequisites at the time <code>setup.py</code> is invoked as well as the ability to install python packages in developer mode.  This allows the files to be edited in place via <a class="external" href="http://docs.python.org/library/site.html" title="http://docs.python.org/library/site.html">.pth files</a> which is handy if you are actively working on a project. setuptools also provides an <a class="external" href="http://packages.python.org/distribute/easy_install.html" title="http://packages.python.org/distribute/easy_install.html">easy_install</a> script for installing packages and their dependencies through the web from <a class="external" href="http://pypi.python.org/pypi" title="http://pypi.python.org/pypi">PyPI</a>. For instance, in order to install the <a class="external" href="http://pyyaml.org/wiki/PyYAML" title="http://pyyaml.org/wiki/PyYAML">PyYAML</a> package, just run</p>
<pre>easy_install PyYAML
</pre>
<p>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 <a class="external" href="http://peak.telecommunity.com/dist/ez_setup.py" title="http://peak.telecommunity.com/dist/ez_setup.py">ez_setup.py</a> script.  You can download and run it with python (with root/Administrator privileges), or if you're in a <a class="external" href="http://www.gnu.org/s/bash/" title="http://www.gnu.org/s/bash/">bash shell</a>, you can run</p>
<pre>sudo python &lt;(curl http://peak.telecommunity.com/dist/ez_setup.py)
</pre>
<p>setuptools is also provided with instances of virtualenv, so if you use virtualenvs for developing you may not need to install setuptools globally. <a class="external" href="http://packages.python.org/distribute/" title="http://packages.python.org/distribute/">distribute</a> is a fork of setuptools written by Mozilla's own Tarek Ziade.  It is compatible with setuptools and fixes a few bugs there.</p>
<p>The <a class=" external" href="http://pypi.python.org/pypi" title="http://pypi.python.org/pypi">Python Package Index (PyPI)</a> is the standard distribution point for python packages.  If you need some functionality in python, it is a good place to look!</p>
<p>See also: <a class=" external" href="http://k0s.org/portfolio/packaging.html" rel="freelink">http://k0s.org/portfolio/packaging.html</a></p><h2>Links</h2>
<p>- <a class=" external" href="http://docs.services.mozilla.com/server-devguide/release.html" rel="freelink">http://docs.services.mozilla.com/ser...e/release.html</a></p>
Revert to this revision