Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>For the most part, this is an attempt to look at the packaging/distribution side, not the mechanics of <code>import</code>. Unfortunately, packaging is the place where Python provides way more than one way to do it. I'm just trying to get the ball rolling, hopefully others will help fill what I miss or point out mistakes. </p> <p>First of all there's some messy terminology here. A directory containing an <code>__init__.py</code> file is a package. However, most of what we're talking about here are specific versions of packages published on PyPI, one of it's mirrors, or in a vendor specific package management system like Debian's Apt, Redhat's Yum, Fink, Macports, Homebrew, or ActiveState's pypm.</p> <p>These published packages are what folks are trying to call "Distributions" going forward in an attempt to use "Package" only as the Python language construct. You can see some of that usage in <a href="http://www.python.org/dev/peps/pep-0376/#how-distributions-are-installed" rel="noreferrer">PEP-376</a> <a href="http://www.python.org/dev/peps/pep-0376/#how-distributions-are-installed" rel="noreferrer">PEP-376</a>. </p> <p>Now, your list of keywords relate to several different aspects of the Python Ecosystem:</p> <h2>Finding and publishing python distributions:</h2> <ul> <li>PyPI (aka the cheese shop)</li> <li>PyPI Mirrors</li> <li>Various package management tools / systems: apt, yum, fink, macports, homebrew</li> <li>pypm (ActiveState's alternative to PyPI)</li> </ul> <p>The above are all services that provide a place to publish Python distributions in various formats. Some, like PyPI mirrors and apt / yum repositories can be run on your local machine or within your companies network but folks typically use the official ones. Most, if not all provide a tool (or multiple tools in the case of PyPI) to help find and download distributions.</p> <h2>Libraries used to create and install distributions:</h2> <ul> <li><code>setuptools</code> / Distribute</li> <li><code>distutils</code></li> </ul> <p>Distutils is the standard infrastructure on which Python packages are compiled and built into distributions. There's a ton of functionality in <code>distutils</code> but most folks just know:</p> <pre><code>from distutils.core import setup setup(name='Distutils', version='1.0', description='Python Distribution Utilities', author='Greg Ward', author_email='gward@python.net', url='http://www.python.org/sigs/distutils-sig/', packages=['distutils', 'distutils.command'], ) </code></pre> <p>And to some extent that's a most of what you need. With the prior 9 lines of code you have enough information to install a pure Python package and also the minimal metadata required to publish that package a distribution on PyPI.</p> <p>Setuptools provides the hooks necessary to support the Egg format and all of it's features and foibles. Distribute is an alternative to Setuptools that adds some features while trying to be mostly backwards compatible. I believe Distribute is going to be included in Python 3 as the successor to Distutil's <code>from distutils.core import setup</code>.</p> <p>Both Setuptools and Distribute provide a custom version of the <code>distutils</code> setup command that does useful things like support the Egg format.</p> <h2>Python Distribution Formats:</h2> <ul> <li>source</li> <li><a href="http://peak.telecommunity.com/DevCenter/PythonEggs" rel="noreferrer">eggs</a></li> </ul> <p>Distributions are typically provided either as source archives (tarball or zipfile). The standard way to install a source distribution is by downloading and uncompressing the archive and then running the <code>setup.py</code> file inside.</p> <p>For example, the following will download, build, and install the Pygments syntax highlighting library:</p> <pre><code>curl -O -G http://pypi.python.org/packages/source/P/Pygments/Pygments-1.4.tar.gz tar -zxvf Pygments-1.4.tar.gz cd Pygments-1.4 python setup.py build sudo python setup.py install </code></pre> <p>Alternatively you can download the Egg file and install it. Typically this is accomplished by using easy_install or pip:</p> <pre><code>sudo easy_install pygments or sudo pip install pygments </code></pre> <p><a href="http://peak.telecommunity.com/DevCenter/PythonEggs" rel="noreferrer">Eggs</a> were inspired by Java's Jarfiles and they have quite a few features you should read about <a href="http://peak.telecommunity.com/DevCenter/PythonEggs" rel="noreferrer">here</a></p> <h2>Python Package Formats:</h2> <ul> <li>uncompressed directories</li> <li>zipimport (zip compressed directories)</li> </ul> <p>A normal python package is just a directory containing an <code>__init__.py</code> file and an arbitrary number of additional modules or sub-packages. Python also has support for finding and loading source code within *.zip files as long as they are included on the <code>PYTHONPATH</code> (<code>sys.path</code>).</p> <h2>Installing Python Packages:</h2> <ul> <li><code>easy_install</code>: the original egg installation tool, depends on <code>setuptools</code></li> <li><code>pip</code>: currently the most popular way to install python packages. Similar to <code>easy_install</code> but more flexible and has some nice features like requirements files to help document dependencies and reproduce deployments.</li> <li><code>pypm</code>, <code>apt</code>, <code>yum</code>, fink, etc</li> </ul> <h2>Environment Management / Automated Deployment:</h2> <ul> <li><code>bento</code></li> <li><code>buildout</code></li> <li><code>virtualenv</code> (and <code>virtualenvwrapper</code>)</li> </ul> <p>The above tools are used to help automate and manage dependencies for a Python project. Basically they give you tools to describe what distributions your application requires and automate the installation of those specific versions of your dependencies.</p> <h2>Locations of Packages / Distributions:</h2> <ul> <li>site-packages</li> <li><code>PYTHONPATH</code></li> <li>the current working directory (depends on your OS and environment settings)</li> </ul> <p>By default, installing a python distribution is going to drop it into the site-packages directory. That directory is usually something like <code>/usr/lib/pythonX.Y/site-packages</code>.</p> <p>A simple programmatic way to find your site-packages directory:</p> <pre><code>from distuils import sysconfig print sysconfig.get_python_lib() </code></pre> <h2>Ways to modify your PYTHONPATH:</h2> <p>Python's import statement will only find packages that are located in one of the directories included in your <code>PYTHONPATH</code>.</p> <p>You can inspect and change your path from within Python by accessing:</p> <pre><code>import sys print sys.path sys.path.append("/home/myname/lib") </code></pre> <p>Besides that, you can set the <code>PYTHONPATH</code> environment variable like you would any other environment variable on your OS or you could use:</p> <ul> <li>.pth files: *.pth files located in directories that are already on your <code>PYTHONPATH</code> are read and each line of the *.pth file is added to your <code>PYTHONPATH</code>. Basically any time you would copy a package into a directory on your <code>PYTHONPATH</code> you could instead create a <code>mypackages.pth</code>. Read more about *.pth files: <a href="http://docs.python.org/library/site.html" rel="noreferrer">site module</a></li> <li>egg-link files: <a href="http://svn.python.org/projects/sandbox/trunk/setuptools/doc/formats.txt" rel="noreferrer">Internal structure of python eggs</a> they are a cross platform alternative to symbolic links. Creating an egg link file is similar to creating a pth file.</li> <li><code>site.py</code> modifications</li> </ul> <p>To add the above <code>/home/myname/lib</code> to site-packages with a *.pth file you'd create a *.pth file. The name of the file doesn't matter but you should still probably choose something sensible.</p> <p>Let's create <code>myname.pth</code>:</p> <pre><code># myname.pth /home/myname/lib </code></pre> <p>That's it. Drop that into <code>sysconfig.get_python_lib()</code> on your system or any other directory in your <code>PYTHONPATH</code> and <code>/home/myname/lib</code> will be added to the path.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload