Harsh J

Memoirs of a QWERTY Keyboard

Install Python Packages locally

8 comments

There’s a great feature in Python versions 2.6 and up that I hardly see being used; it’s the ability to install modules and packages in a per-user local directory. I like this feature since it doesn’t have any super-user power requirements and lets me install packages, modules and even scripts in my own Home directory and use it just as normally as the other global files.

To do so, one must first create the local site-packages directory, and then place the required package or module file or folder under it. The following commands are all for UNIX. ~/ expands to the user’s $HOME automatically.

# To create the required directory
mkdir -p ~/.local/lib/python2.6/site-packages

Now place the module or package folder under this directory, or link to it for achieving the same installation effect as you would with a global site-packages directory. For example, for my django copy from svn I’d do:

ln -s ~/.django-svn/django-trunk/django ~/.local/lib/python2.6/site-packages/django

Running the Python interpreter (in the same user account) will show this working:

Python 2.6.2 (release26-maint, Apr 19 2009, 01:56:41)
[GCC 4.3.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import django
>>> 

So there you have it, when you want a package just for your user account while developing just put it under the local site-packages directory. Python automatically adds this to its path (PYTHONPATH).

References: PEP 370 (Has notes for OS X and Windows users)

Written by Harsh

May 21st, 2009 at 11:00 am

Posted in Linux,Software

Tagged with ,

8 Responses to 'Install Python Packages locally'

Subscribe to comments with RSS or TrackBack to 'Install Python Packages locally'.

  1. Hmm, I’ve never used this before, but I’ve never had to. I just have a ~/include/python directory which has been put into my PYTHONPATH, and I have links in there for every module I want. I even have a script called pylink to automatically create symlinks into that directory.

    That said, python packaging could use a lot of ideas, especially from places like CPAN/CTAN, etc.

    sykora

    21 May 09 at 11:25 am

  2. The script idea is great, I never thought about that! :)

    This feature is more Unix-ish I think, since binaries/scripts sent to ~/.local/bin work too, much like the /usr/local/ directory, which applies to all users.

    Do you like having that include folder visible on your $HOME? I like them hidden usually..

    Harsh

    21 May 09 at 11:51 am

  3. My home folder over time has come to mimic standard unix layout: I have a ~/bin, ~/tmp, ~/lib, ~/etc, and so on. In some cases it was a concious decision, others not so.

    Actually I got the script idea from the django lot, who regularly use the svn version of django for normal work, so they needed a way to keep it separate from the usual directories.

    And I don’t mind having them visible, many of my projects are located in that directory under git, and I access them regularly, so why not?

    If you want I can post the script, but it’s not like it’s difficult to come up with in the first place anyway.

    sykora

    21 May 09 at 11:55 am

  4. Ah ok, I have more dumb-ish names like Docs, Programs and such; All capitalised ones. I guess that does waste some keystrokes of mine..

    I’ll write the script, it does sound easy :)

    Harsh

    21 May 09 at 12:01 pm

  5. ^ n3rd5.

    Pathik

    23 May 09 at 8:30 pm

  6. Am glad you wrote that in l33t ;)

    Harsh

    23 May 09 at 8:36 pm

  7. Its almost been a month since you posted. Busy with your academics eh? :)

    Mani S

    16 Jun 09 at 6:52 pm

  8. I had to revisit this problem, because my method wasn’t working out with multiple versions of python like I have now — 2.5 which comes with debian, and the 3.1 I’ve installed. This probably means I’ll have to use the local site-packages, since there’s no other reliable way to distinguish versions.

    Ah well, it was good while it lasted.

    sykora

    23 Aug 09 at 11:29 am

Leave a Reply