Blog Support FAQs Contact Login
NEED TO TALK?
1.877.426.6834
  • Business Needs
  • Solutions
  • Why Contegix
  • Company
  • Customers
  • Resources



Knowledge  ::  Tech Tip! Shell Function for Python

Tech Tip! Shell Function for Python


30
Jul 2010
0
Comments



By: Andrew Williams, Technical Support Engineeer

When working with an open source library, sometimes the best documentation is the source code itself. Here’s a quick shell function for finding the code you’re looking for in the Python scripting language. Simply add these three lines to your .bash_profile or .bashrc file.

**

function pycd () {

cd `python -c “import os.path, $1; print os.path.dirname($1.__file__)”`

}

**

Now you can you use the ‘pycd’ command to change to the directory the module lives inside. Simply specify the module you’re looking for as an argument. Here is an example using the popular “lxml” module.

**

contegix@AJW-Contegix-MBP:~$ pycd lxml

contegix@AJW-Contegix-MBP:lxml$ pwd

/Library/Python/2.6/site-packages/lxml-2.2.6-py2.6-macosx-10.6-universal.egg/lxml

andrew@andrew-mbp:lxml$ ls

ElementInclude.py  _elementpath.py    cssselect.py       etree.py           objectify.py       pyclasslookup.pyc  usedoctest.pyc

ElementInclude.pyc _elementpath.pyc   cssselect.pyc      etree.pyc          objectify.pyc      sax.py

__init__.py        builder.py         doctestcompare.py  etree.so           objectify.so       sax.pyc

__init__.pyc       builder.pyc        doctestcompare.pyc html               pyclasslookup.py   usedoctest.py

**

The Python one-liner is pretty simple. It first imports the ‘os.path’ module and then the module that you specified on the command line (We’ll call this the “example” module). It then finds the parent directory of the example module by using the built-in attribute ‘__file__’. This special attribute is a string of the pathname of the file from which the module was loaded. The example module’s parent directory is then written to standard out where the shell expands it for use with the ‘cd’ command.

This command works with both modules in the standard library and with third party libraries. The only caveat is that this will not work with code that is still contained inside a “.egg” package as it’s not actually a directory on the file system.



 anne.ellegood      Posted in: Knowledge, Tech    

Leave a Reply

Your email address will not be published. Required fields are marked *





  Cancel Reply

  • Categories

    • Culture
    • Employees
    • Events
    • Knowledge
    • News
    • Staff Development
    • Tech
    • Technology
    • Uncategorized
  • Archives

  • January 2012
  • December 2011
  • November 2011
  • October 2011
  • September 2011
  • July 2011
  •    More »

    Popular Posts

    Contegix Announces Our New Controller

    Contegix is happy to announce the newest addition to our…

    January 19, 2012

    Best & Brightest Wanted Here!

    Here at Contegix we are once again looking to add…

    January 16, 2012

    Welcome to the Team!

    We are pleased to announce the addition of Matt Steiner…

    December 20, 2011

    Hyper Wars: IT Industry Disrupted by Multihypervisor Approach

    For the past month, InformationWeek has featured several pieces covering…

    December 14, 2011

  • NEED SOME ASSISTANCE?
    We’re here to help you, 24/7/365. Give us a call at 877.4.CONTEGIX or drop us a line.
    1.877.426.6834
    sales@contegix.com




  • Contegix Headquarters
    900 Walnut Street, Suite 110
    Saint Louis, MO 63102

    Phone: 314.622.6200
    Toll-Free: 877.4.CONTEGIX
    or 877.426.6834
    Fax: 314.621.4422

  • Locations

    Contegix Data Center – STL1
    900 Walnut Street
    Saint Louis, MO 63102

    Contegix Data Center – STL2
    1111 Olive Street

    Saint Louis, MO 63101

    Contegix Data Center – LDN1
    11 Hanbury Street
    London
    E1 6QR
    England

    ©2011 Contegix LLC, All Rights Reserved.

  • Recent Tweets

    • RT @dzhu: Really impressed by @contegix support's response time and expertise today.
    • @dzhu thanks! Let us know if you need anything!
    • Happy New Year!
    • RT @appfusions: Smooth website upgrades & data imports in process w/ @Contegix today - Good stuff! #atlassian #Skilled #ManagedHosting