Wednesday, March 4, 2015

Installing local packages for Python


As a network admin without admin rights on my box, installing modules is a pain.
Corporate politics make it tough to get things done quickly, if at all-  getting sudo privs, or even having an administrator install packages for me can be a no-go.

Here's the shorthands for what I've done to start installing non-standard python packages, so I can continue to script in my environment.

Proxy!

To get to the internet from my box, I need to turn on a proxy.

export http_proxy://192.168.1.1:80
export https_proxy://192.168.1.1:80


I can do this every time I log in-  or I can write it into my .bashrc
There's  lots of scripting you can do within your .bashrc to turn proxies on and off as needed-  but there's no need to get fancy for my work. Because my jumpbox is always at the same place and uses the same proxy, I don't need to get fancy and start importing variables to determine which proxy to use.
I put this into my .bashrc:

# Proxy package to turn proxy on and off
function proxy_on() {
    export no_proxy="localhost,127.0.0.1,localaddress,.localdomain.com"

        export http_proxy="http://webproxy_name:80"
        export https_proxy=$http_proxy
        echo "Proxy environment variable set."
        return 0

}

function proxy_off(){
    unset http_proxy
    unset https_proxy
    echo -e "Proxy environment variable removed."
}



Now when I log on, I just type 'proxy_on' or 'proxy_off' in order to get to the internet from my shell.

Way fancier ways and means of accomplishing more complex settings:
https://wiki.archlinux.org/index.php/proxy_settings


Setting the local repository for installation

 Now that I can get to the internet, easy_install won't time out.  But easy_install won't let me install the files, due to my non-elevated privs.

[host] easy_install netaddr
/usr/lib64/python2.6/site-packages/setuptools/package_index.py:7: DeprecationWarning: the md5 module is deprecated; use hashlib instead
  from md5 import md5
error: can't create or remove files in install directory

The following error occurred while trying to add or remove files in the
installation directory:

    [Errno 2] No such file or directory: '/usr/local/lib64/python2.6/site-packages/test-easy-install-29320.pth'

The installation directory you specified (via --install-dir, --prefix, or
the distutils default setting) was:

    /usr/local/lib64/python2.6/site-packages/

This directory does not currently exist.  Please create it and try again, or
choose a different installation directory (using the -d or --install-dir
option).


That's the kind of message I get when I try to install.  I need to specify a local directory!
There is a key bit of information I'll want, there, however-  note that I'm using python2.6-  your install may be different.

I'll create a directory in which to store my stuff:

mkdir -p $home/local/lib/python2.6/site-packages

The -p is needed to create all the parent directories from my home directory.
Note that the 2.6 portion of this path may change, depending upon your current version of python- you can determine this from the original error message.

Now I need to tell python where to look for these things:
export PYTHONPATH=${PYTHONPATH}:$HOME/local/lib/python2.6/site-packages


I can also dump that export into my .bashrc so I don't have to do it every time I log in.


At this point, I can install netaddr-

easy_install -d $HOME/local/lib/python2.6/site-packages netaddr

And now we're off to the races!

Other libraries I've  imported for their usefulness:


netaddr  - net addressing library - calculate subnets, etc.
infobloxpy  - interface into the Inboblox WAPI
ciscoconfparse  - parser for non NXOS configs
paramiko  - ssh client
pyexpect  - expect for python
beautifulsoup  - XML parsing (great for NXOS!)

Any other suggestions welcome!