Configurando múltiplos ambientes Python
by Felipe 'chronos' Prenholato on Domingo, 30 Outubro/2011, under Dicas, Django, Gentoo, python
Nota: Olá meus caros leitores, apesar do ChronosBox ser um ótimo Blog, eu estou focando meus esforços em um blog conjunto com meu amigo Handrus Nogueira, portanto não deixem de visitar o Dev With Passion! Todos os posts do ChronosBox estarão no Dev With Passion, exceto por alguns comentários novos. Abraços!
Múltiplos ambientes python
Trabalhar com várias versões de ambientes python tem se tornado algo comum em minha vida, e acredito que um meio fácil de ‘setar’ estes ambientes seja algo útil e não tão simples para a maioria das pessoas.
Eu criei um simples bash script que seta este ‘ambiente’, trata-se de uma função que você cria no seu .bash_profile. Executa-se ela com a versão do python em seguida e pronto, seu ambiente esta configurado. Vamos ver no código como eu fiz, o script está com comentários em inglês:
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 aplications 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__);" }
Agora crie uma estrutura de diretórios com o comando:
$ mkdir -pv ~/.py/{2.4,2.5,2.6}
E cada vez que for usar uma versão diferente do python, carregue com:
$ loadpyenv VERSÃO
Onde VERSÃO é a versão escolhida do python.
Várias versões do Python no Gentoo
O Gentoo Linux, minha distro do dia a dia, me dá um bom bonus quanto a múltiplas versões do python, configure no /etc/make.conf a seguinte linha:
USE_PYTHON="2.4 2.5 2.6"
Instale os pythons nas versões escolhidas e execute o seguinte comando para re-compilar seus pacotes:
emerge $(eix -I dev-python/* | grep "\[I\]" | cut -d' ' -f2)
O eix é uma aplicação de indexação do portage, muito comum nas máquinas Gentoo.
Cada pacote python é recompilado para as 3 versões (no caso), mas tive problemas em compilar alguns pacotes para a 2.4, fiquem avisados
Bom, é isso, até mais!
3 Comments for this entry
1 Trackback or Pingback for this entry
-
Tweets that mention Setting up multiple python environments - Chronosbox -- Topsy.com
quarta, 27 em janeiro/2010 on 14:16[...] 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 [...]
domingo, 18 em abril/2010 on 17:31
De uma olhada no virtualenv[1] e no virtualenvwrapper [2]
[1] – http://pypi.python.org/pypi/virtualenv
[2] – http://www.doughellmann.com/projects/virtualenvwrapper/
terça, 28 em junho/2011 on 13:24
For your gentoo example, I suggest to use python-updater instead of eix+emerge.
It will do the job for packages outside dev-python/* too
quinta, 30 em junho/2011 on 14:15
Very very nice tip! I’m not using gentoo for a while (unhappily) but I remember about this python-updater and used sometimes.