Please note, this is a STATIC archive of website developer.mozilla.org from 03 Nov 2016, cach3.com does not collect or store any user information, there is no "phishing" involved.

Revision 353161 of Python

  • Revision slug: Python
  • Revision title: Python
  • Revision id: 353161
  • Created:
  • Creator: [email protected]
  • Is current revision? No
  • Comment
Tags: 

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 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: https://k0s.org/toolbox/?language=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 https://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: https://k0s.org/portfolio/packaging.html

See also

Revision Source

<p><a class="external" href="https://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 id="Free_E-books">Free E-books</h3>
<p><a class="external" href="https://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="https://gnosis.cx/TPiP/">Text Processing In Python</a> is an excellent reference and tutorial.</p>
<p>Other free ebooks and online sources include:</p>
<ul style="margin-left: 40px; ">
  <li>The <a class="external" href="https://docs.python.org/tutorial/index.html" title="https://docs.python.org/tutorial/index.html">Python Tutorial</a> at <a href="https://docs.python.org" title="https://docs.python.org">docs.python.org</a></li>
  <li><a href="https://en.wikibooks.org/wiki/Non-Programmer%27s_Tutorial_for_Python_2.6" title="https://en.wikibooks.org/wiki/Non-Programmer%27s_Tutorial_for_Python_2.6">Non-Programmer's Tutorial for Python 2.6</a> at <a href="https://en.wikibooks.org/wiki/Main_Page" title="https://en.wikibooks.org/wiki/Main_Page">Wikibooks</a></li>
  <li><a href="https://www.greenteapress.com/thinkpython/" title="https://www.greenteapress.com/thinkpython/">Think Python</a>: How to Think Like a Computer Scientist by Allen B. Downey (free <a href="https://www.greenteapress.com/thinkpython/thinkpython.pdf" title="https://www.greenteapress.com/thinkpython/thinkpython.pdf">PDF</a> &amp; <a href="https://www.greenteapress.com/thinkpython/html/index.html" title="https://www.greenteapress.com/thinkpython/html/index.html">HTML</a> versions).
    <ul>
      <li><a href="https://greenteapress.com/complexity/index.html" title="https://greenteapress.com/complexity/index.html">Think Complexity</a> by Allen B. Downey "picks up where Think Python leaves off" (free <a href="https://greenteapress.com/complexity/thinkcomplexity.pdf" title="https://greenteapress.com/complexity/thinkcomplexity.pdf">PDF</a> &amp; <a href="https://greenteapress.com/complexity/html/index.html" title="https://greenteapress.com/complexity/html/index.html">HTML</a> versions)</li>
    </ul>
  </li>
  <li><a href="https://learnpythonthehardway.org" title="https://learnpythonthehardway.org">Learn Python The Hard Way</a> by Zed Shaw (<a href="https://learnpythonthehardway.org/book/" title="https://learnpythonthehardway.org/book/">free HTML verison</a>)</li>
  <li><a href="https://www.itmaybeahack.com/book/python-2.6/html/index.html" title="https://www.itmaybeahack.com/book/python-2.6/html/index.html">Building Skills in Python</a> by Steven F. Lott (<a href="https://www.itmaybeahack.com/book/python-2.6/latex/BuildingSkillsinPython.pdf" title="https://www.itmaybeahack.com/book/python-2.6/latex/BuildingSkillsinPython.pdf">also available as a PDF</a>)</li>
</ul>
<p>Once you've learned the basics of the language, <a href="https://python.net/~goodger/projects/pycon/2007/idiomatic/handout.html" title="https://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.</p>
<h3 id="Free_Online_Courses">Free Online Courses</h3>
<ul style="margin-left: 40px; ">
  <li><a href="https://developers.google.com/edu/python/" title="https://code.google.com/edu/languages/google-python-class/">Google's Python Class</a></li>
  <li>Learnstreet's Free <a href="https://www.learnstreet.com/lessons/languages/python" title="https://www.learnstreet.com/lessons/languages/python">Python Courses and Videos</a></li>
  <li><a href="https://www.codecademy.com/tracks/python" title="https://www.codecademy.com/tracks/python">Python</a> at Code Academy</li>
  <li><a href="https://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-189-a-gentle-introduction-to-programming-using-python-january-iap-2008/" title="https://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</li>
</ul>
<h2 id="Python_in_Mozilla" name="Python_in_Mozilla">Python in Mozilla-based applications</h2>
<p><a href="/en-US/docs/XPCOM" title="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-US/docs/PyXPCOM" title="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 href="/en-US/docs/PyDOM" title="PyDOM">PyDOM</a>) bindings are implemented. This lets <a href="/en-US/docs/Chrome" title="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 id="Python-based_tools_for_Mozilla_development">Python-based tools for Mozilla development</h2>
<p>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 <a href="/en-US/docs/Python_Environment_and_Tools_for_Mozilla" title="Python_Environment_and_Tools_for_Mozilla">Python Environment and Tools for Mozilla</a>.</p>
<p>Tools are listed here: <a class="external" href="https://k0s.org/toolbox/?language=python">https://k0s.org/toolbox/?language=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="https://blog.mozilla.com/webdev/" title="https://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-US/docs/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="https://docs.python.org/distutils/index.html" title="https://docs.python.org/distutils/index.html">setup.py</a> files to record metadata and installation instructions for <a class="external" href="https://docs.python.org/tutorial/modules.html#packages" title="https://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="https://docs.python.org/tutorial/modules.html#the-module-search-path" title="https://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="https://docs.python.org/distutils/index.html" title="https://docs.python.org/distutils/index.html">distutils</a></code> is the only distribution package available in <a class="external" href="https://docs.python.org/library/" title="https://docs.python.org/library/">python's standard library</a>. <code>distutils</code> has ability to upload to the <a class="external" href="https://pypi.python.org/pypi" title="https://pypi.python.org/pypi">python package index</a> and to install python packages. See the <a class="external" href="https://docs.python.org/distutils/index.html" title="https://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="https://peak.telecommunity.com/DevCenter/setuptools" title="https://peak.telecommunity.com/DevCenter/setuptools">setuptools</a> is a third-party ad hoc standard for packaging and distribution. It is mostly compatible with <code>distutils</code>, but importantly adds the ability for packages to <a class="external" href="https://peak.telecommunity.com/DevCenter/setuptools#declaring-dependencies" title="https://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="https://packages.python.org/distribute/setuptools.html#development-mode" title="https://packages.python.org/distribute/setuptools.html#development-mode">development mode</a>. This allows the files to be edited in place via <a class="external" href="https://docs.python.org/library/site.html" title="https://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="https://packages.python.org/distribute/easy_install.html" title="https://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="https://pypi.python.org/pypi" title="https://pypi.python.org/pypi">PyPI</a>. For instance, in order to install the <a class="external" href="https://pyyaml.org/wiki/PyYAML" title="https://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="https://peak.telecommunity.com/dist/ez_setup.py" title="https://peak.telecommunity.com/dist/ez_setup.py">ez_setup.py</a></code> script. You can download and run it with python (with root/Administrator privileges), or if you're in a <a class="external" href="https://www.gnu.org/s/bash/" title="https://www.gnu.org/s/bash/">bash shell</a>, you can run</p>
<pre>
sudo python &lt;(curl https://peak.telecommunity.com/dist/ez_setup.py)
</pre>
<p><code>setuptools</code> is also provided with instances of <a href="/en-US/docs/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="https://packages.python.org/distribute/" title="https://packages.python.org/distribute/">distribute</a> is a fork of setuptools written by Mozilla's own <a class="external" href="https://ziade.org/" title="https://ziade.org/">Tarek Ziade</a>. 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-US/docs/Python/Virtualenv" title="Virtualenv">virtualenv</a> for development!</div>
<p>The <a class="external" href="https://pypi.python.org/pypi" title="https://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="https://k0s.org/portfolio/packaging.html">https://k0s.org/portfolio/packaging.html</a></p>
<h2 id="See_also">See also</h2>
<ul>
  <li><a class="external" href="https://docs.services.mozilla.com/server-devguide/release.html" title="https://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">https://wiki.mozilla.org/Auto-tools/Python101</a></li>
  <li><a href="https://www.learnstreet.com/cg/simple/projects/python" title="https://www.learnstreet.com/cg/simple/projects/python">Python Projects </a>at Code Garage</li>
</ul>
Revert to this revision