Configurando múltiplos ambientes Python

27 Jan

Multiple python environments

Work with various python versions with multiple versions of python environments has become something common in my life, and, I believe that an easy way of setting this environments are something very useful, and not simple for most of people.

I created a simple shell script that set this environment, that is a function that you add in your .bash_profile. Run this function in your shell with python version as argument and all is set. Let’s take a look on my code:

export ORIGINALPATH=$PATH
loadpyenv(){
    # $PV is our control variable, get from $1, first argument on shell
    # anything that you need to put here and depend for python version ${PV} is your var 😉
    export PV=$1
    echo -e "n:: Setting environ to use python $PV"

    # Put here various aliases that you can use
    # put a python alias
    alias python="python${PV}"
    # m and runserver a kick alias for Django common actions
    alias m="python${PV} manage.py"
    alias runserver="python${PV} manage.py runserver :8000"
    # easy and fast install of python packages, in right path with localpyinstall
    alias localpyinstall="python${PV} setup.py install --prefix=~/.py/${PV}/"
    echo -e "n:: New aliases"
    # print aliases 🙂
    alias python m runserver localpyinstall

    echo -e "n:: New PATH"
    # setup applications path, be careful about not use $ORIGINALPATH.
    #
    # I use a path structure like that:
    # ~/.py/
    # |-- 2.4
    # |-- 2.5
    # `-- 2.6
    export PATH="${HOME}/.py/${PV}/bin/:${ORIGINALPATH}"
    echo $PATH
    echo -e "n:: New PYTHON PATH"
    # setup new python path, don't need to add system wide path, just our custom paths
    export PYTHONPATH="${HOME}/.py/${PV}/lib/python${PV}/site-packages/"
    # print more some info
    python$PV -c "import sys;print sys.path"
    echo -e "n:: Django version & file"
    python$PV -c "import django;print 'version %s ... file %s' % (django.get_version(),django.__file__);"
}

Now you create a directory structure like my own with command:

$ mkdir -pv ~/.py/{2.4,2.5,2.6}

And any time that you need to use a diferent version of python, load with:

$ loadpyenv VERSION

Where VERSION is python version that you choose.

Multiple python versions in Gentoo

The Gentoo Linux, my preferred distro, give me a bonus about multiple python versions, configure in your /etc/make.conf the following line:

USE_PYTHON="2.4 2.5 2.6"

Now install all python versions and run following command to re-compile your installed packages:

emerge $(eix -I dev-python/* | grep "[I]" | cut -d' ' -f2)

The eix is a simple app that index portage, very common in Gentoo machines.
Each python package are recompiled for all tree versions (in this case), but, I have some problems with some packages and python 2.4 🙂

Well, that’s it, see ya!

VN:F [1.9.22_1171]
Rating: 0.0/5 (0 votes cast)

2 Responses to “Configurando múltiplos ambientes Python”

  1. thiago April 18, 2010 at %I:%M %p #

    De uma olhada no virtualenv[1] e no virtualenvwrapper [2]

    [1] – http://pypi.python.org/pypi/virtualenv
    [2] – http://www.doughellmann.com/projects/virtualenvwrapper/

    VA:F [1.9.22_1171]
    Rating: 0.0/5 (0 votes cast)

Trackbacks and Pingbacks

  1. Tweets that mention Setting up multiple python environments - Chronosbox -- Topsy.com - January 27, 2010

    […] This post was mentioned on Twitter by Felipe 'chronos', Felipe 'chronos'. Felipe 'chronos' said: New post on http://chronosbox.org/: Setting up multiple python environments http://u.nu/22gt4 #djano […]

Leave a Reply