Python

  • Revision slug: Python
  • Revision title: Python
  • Revision id: 321143
  • Created:
  • Creator: Hanleybrand
  • Is current revision? No
  • Comment

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

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

Use of Python at Mozilla

Mozilla has considerable infrastructure based on 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. 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

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 id="Learning_Python" name="Learning_Python">Learning Python</h2>
<h3>Free E-books</h3>
<p><a class="external" href="http://www.diveintopython.net/toc/index.html">Dive Into Python</a> 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, <a class="external" href="http://gnosis.cx/TPiP/">Text Processing In Python</a> is an excellent reference and tutorial.&nbsp;</p>
<p>Other free ebooks and online sources include:</p>
<ul style="margin-left: 40px; ">
  <li>
    <p>The&nbsp;<a class="external" href="http://docs.python.org/tutorial/index.html" title="http://docs.python.org/tutorial/index.html">Python Tutorial</a>&nbsp;at&nbsp;<a href="http://docs.python.org" title="http://docs.python.org">docs.python.org</a></p>
  </li>
  <li>
    <p><a href="http://en.wikibooks.org/wiki/Non-Programmer%27s_Tutorial_for_Python_2.6" title="http://en.wikibooks.org/wiki/Non-Programmer%27s_Tutorial_for_Python_2.6">Non-Programmer's Tutorial for Python 2.6</a> at <a href="http://en.wikibooks.org/wiki/Main_Page" title="http://en.wikibooks.org/wiki/Main_Page">Wikibooks</a></p>
  </li>
  <li>
    <p><a href="http://www.greenteapress.com/thinkpython/" title="http://www.greenteapress.com/thinkpython/">Think Python</a>: How to Think Like a Computer Scientist&nbsp;by Allen B. Downey (free <a href="http://www.greenteapress.com/thinkpython/thinkpython.pdf" title="http://www.greenteapress.com/thinkpython/thinkpython.pdf">PDF</a> &amp; <a href="http://www.greenteapress.com/thinkpython/html/index.html" title="http://www.greenteapress.com/thinkpython/html/index.html">HTML</a> versions). &nbsp;</p>
    <ul>
      <li>
        <p><a href="http://greenteapress.com/complexity/index.html" title="http://greenteapress.com/complexity/index.html">Think Complexity</a>&nbsp;by Allen B. Downey "picks up where Think Python leaves off"&nbsp;(free <a href="http://greenteapress.com/complexity/thinkcomplexity.pdf" title="http://greenteapress.com/complexity/thinkcomplexity.pdf">PDF</a> &amp; <a href="http://greenteapress.com/complexity/html/index.html" title="http://greenteapress.com/complexity/html/index.html">HTML</a> versions)</p>
      </li>
    </ul>
  </li>
  <li>
    <p><a href="http://learnpythonthehardway.org" title="http://learnpythonthehardway.org">Learn Python The Hard Way</a>&nbsp;by Zed Shaw (<a href="http://learnpythonthehardway.org/book/" title="http://learnpythonthehardway.org/book/">free HTML verison</a>)</p>
  </li>
  <li>
    <p><a href="http://www.itmaybeahack.com/book/python-2.6/html/index.html" title="http://www.itmaybeahack.com/book/python-2.6/html/index.html">Building Skills in Python</a> by Steven F. Lott (<a href="http://www.itmaybeahack.com/book/python-2.6/latex/BuildingSkillsinPython.pdf" title="http://www.itmaybeahack.com/book/python-2.6/latex/BuildingSkillsinPython.pdf">also available as a PDF</a>)</p>
  </li>
</ul>
<p>Once you've learned the basics of the language,&nbsp;<a href="http://python.net/~goodger/projects/pycon/2007/idiomatic/handout.html" title="http://python.net/~goodger/projects/pycon/2007/idiomatic/handout.html">Code Like a Pythonista: Idiomatic Python</a> is a tutorial that will help you acclimate to specific aspects of Python that differentiate it from other programming languages.&nbsp;</p>
<h3>Free Online Courses</h3>
<ul style="margin-left: 40px; ">
  <li>
    <p><a href="http://code.google.com/edu/languages/google-python-class/" title="http://code.google.com/edu/languages/google-python-class/">Google's Python Class</a></p>
  </li>
  <li>
    <p><a href="http://www.codecademy.com/tracks/python" title="http://www.codecademy.com/tracks/python">Python</a> at Code Academy</p>
  </li>
  <li>
    <p><a href="http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-189-a-gentle-introduction-to-programming-using-python-january-iap-2008/" title="http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-189-a-gentle-introduction-to-programming-using-python-january-iap-2008/">A Gentle Introduction to Programming Using Python</a> at MIT</p>
  </li>
</ul>
<h2 id="Python_in_Mozilla" 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;&nbsp;JavaScript communication. The <a href="/en/PyXPCOM" title="en/PyXPCOM">Python XPCOM package</a> (also called PyXPCOM)&nbsp;is the low-level glue that ties Python and Mozilla together, letting XPCOM components written in JavaScript or C++&nbsp;to be used from Python and vice versa. PyXPCOM&nbsp;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&nbsp;(<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&nbsp;authors use Python in their &lt;script&gt;&nbsp;tags (again, not in the official&nbsp;Firefox/Thunderbird builds).</p>
<h2 id="Python-based_tools_for_Mozilla_development">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 id="Use_of_Python_at_Mozilla">Use of Python at Mozilla</h2>
<p>Mozilla has considerable infrastructure based on python:</p>
<ul>
  <li>django for <a class="external" href="http://blog.mozilla.com/webdev/" title="http://blog.mozilla.com/webdev/">webdev</a></li>
  <li><a class="link-https" href="https://wiki.mozilla.org/Buildbot" title="https://wiki.mozilla.org/Buildbot">buildbot</a> for continuous integration</li>
  <li>many of our <a href="/en/Mozilla_automated_testing" title="Mozilla automated testing">test harnesses</a></li>
  <li><a class="link-https" href="https://wiki.mozilla.org/Auto-tools/Projects/MozBase" title="https://wiki.mozilla.org/Auto-tools/Projects/MozBase">mozbase</a></li>
</ul>
<h2 id="Python_packaging">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. <code><a class="external" href="http://docs.python.org/distutils/index.html" title="http://docs.python.org/distutils/index.html">distutils</a></code> 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>. <code>distutils</code> 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. See the <a class="external" href="http://docs.python.org/distutils/index.html" title="http://docs.python.org/distutils/index.html">Python documentation on <code>distutils</code></a> for details.</p>
<p>While <code>distutils</code> 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.&nbsp; It is mostly compatible with <code>distutils</code>, 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 <a class="external" href="http://packages.python.org/distribute/setuptools.html#development-mode" title="http://packages.python.org/distribute/setuptools.html#development-mode">development mode</a>.&nbsp; 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. <code>setuptools</code> also provides an <code><a class="external" href="http://packages.python.org/distribute/easy_install.html" title="http://packages.python.org/distribute/easy_install.html">easy_install</a></code> 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 <code>setuptools</code> is not included with python, you will need to install it in order to use it. You may install it from the <code>setuptools</code> PyPI page by downloading, extracting, and running <code>python setup.py install</code>. Or you can use the <code><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></code> script.&nbsp; 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><code>setuptools</code> is also provided with instances of <a href="/en/Python/Virtualenv" title="Virtualenv">virtualenv</a>, so if you use virtualenvs for developing you may not need to install <code>setuptools</code> 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 <a class="external" href="http://ziade.org/" title="http://ziade.org/">Tarek Ziade</a>.&nbsp; It is compatible with <code>setuptools</code> and fixes a few bugs there.</p>
<div class="note">
  <strong>Note:</strong> It's <strong>highly</strong> recommended that you use <a href="/en/Python/Virtualenv" title="Virtualenv">virtualenv</a> for development!</div>
<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.&nbsp; 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 id="See_also">See also</h2>
<ul>
  <li><a class="external" href="http://docs.services.mozilla.com/server-devguide/release.html" title="http://docs.services.mozilla.com/server-devguide/release.html">Releasing an application</a> (Mozilla Services documentation)</li>
  <li><a class="link-https" href="https://wiki.mozilla.org/Auto-tools/Python101" rel="freelink">https://wiki.mozilla.org/Auto-tools/Python101</a></li>
</ul>
Revert to this revision