All About Your Python(s)

Here I present a script that provides diagnostics about the current Python execution context, or the Python environment of the interpreter passed as an argument. As a Python developer, I have multiple Python versions side-by-side for testing purposes, using scripts that munge my $PATH variable to “import” and “unimport” different versions of Python as I need them. While “which python” is always available, many times I want to know things like, “what is the version of the current default Python?” or “where is the current Python’s ‘site-packages’ directory?“. Though all this information is usually one or two commands away, it can be pretty tedious jump through these hoops every time this information is required. Also, often mature systems that have gone through various systems administrators and upgrades have both legacy and current stuff scattered all over the place, and it can be a hassle trying to figure out where the site packages and other components of a particular installation of Python are located. Running it without arguments provides a summary of the shell’s current default Python environment:

$ describe-python.py
[Python 2.6.1]
            Executable: /usr/bin/python
            Version: 2.6.1.final.0
Installation Prefix: /System/Library/Frameworks/Python.framework/Versions/2.6
        Site Packages: /Library/Python/2.6/site-packages
        Implementation: CPython
                Branch: tags/r261
            Revision: 67515
            Compiler: GCC 4.2.1 (Apple Inc. build 5646)
            Build Date: Jul  7 2009 23:51:51

You can also pass in an explicit path to a specific Python interpreter:

$ describe-python.py /opt/python-2.4.5/bin/python
[Python 2.4.5]
            Executable: /opt/python-2.4.5/bin/python
            Version: 2.4.5.final.0
Installation Prefix: /opt/python-2.4.5
        Site Packages: /opt/python-2.4.5/lib/python2.4/site-packages
        Implementation: ???
                Branch: ???
            Revision: ???
            Compiler: GCC 4.0.1 (Apple Inc. build 5465)
            Build Date: Sep 29 2008 21:20:46

$ describe-python.py /opt/pypy-1.2-osx/bin/pypy
[Python 2.5.2 (72096, Mar 11 2010, 12:33:31) [PyPy 1.2.0] ]
            Executable: /opt/pypy-1.2-osx/bin/pypy
            Version: 2.5.2.beta.42
Installation Prefix:
        Site Packages: /opt/pypy-1.2-osx/site-packages
        Implementation: ???
                Branch: ???
            Revision: ???
            Compiler: PyPy 1.2.0
            Build Date: Mar 11 2010 12:33:31

$ describe-python.py /opt/jython2.5.0/bin/jython
[Jython 2.5.0]
            Executable: /opt/jython2.5.0/bin/jython
            Version: 2.5.0.final.0
Installation Prefix: /opt/jython2.5.0
        Site Packages: /opt/jython2.5.0/Lib/site-packages
        Implementation: ???
                Branch: ???
            Revision: ???
            Compiler: Java HotSpot(TM) 64-Bit Server VM (Apple Inc.)
            Build Date: Jun 16 2009 13:33:26

The “-s” or “--site-packages” flag, which will result in the full path of the site packages directory of the Python environment being written to the standard output:

[~]$ describe-python.py -s
/Library/Python/2.6/site-packages

[~]$ describe-python.py -s /usr/local/bin/python2.6/
/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages

[~]$ describe-python.py -s /opt/python-2.4.5/bin/python
/opt/python-2.4.5/lib/python2.4/site-packages

This makes hopping to the site packages directory of a particular Python installation as simple as:

$ cd $(describe-python.py -s)

or:

$ cd `describe-python.py -s`

The script itself lives as a Gist, and is embedded here: [gist id=“7949150”]

Share