giovedì 15 novembre 2012

Python 3.3 and pyvenv, a (hackish) solution

EDIT: it seems to be a bug in debian experimental, if you install python3.3 from the upstream tarball, you will not find this issue, the resolution of this bug is tracked in both Debian BTS and python BTS .

EDIT2: I could confirm that in debian unstable as of July 2013 is fixed. In this case you will be done with:


$ pyvenv-3.3 ~/.pyvenv-3.3
$ . ~/.pyvenv-3.3/bin/activate
(.pyvenv-3.3)$ curl -O http://python-distribute.org/distribute_setup.py
(.pyvenv-3.3)$ python distribute_setup.py
(.pyvenv-3.3)$ easy_install pip




In the previous blog post I got stuck in properly creating a virtualenv in python 3.3, now I came with this hacky script, which naively wraps pyvenv-3.3 and create a couple of missing links which makes everything works fine. In fact I saw that virtualenvs created with virtualenv .27 tools makes links from directories in <virtualenv>/local/{bin,lib,include} to <virtualenv>, those links are missing in venv created by pyvenv-3.3.

So here it is the hackish wrapper script:


#!/bin/sh

VENV_PATH="$1"
PROGRAM="$(basename $0)"

if [ -z "${VENV_PATH}" ]; then
    echo "Usage:" 1>&2
    echo "${PROGRAM} VENV_PATH" 1>&2
    exit 1
fi

shift 1

echo pyvenv-3.3 "${VENV_PATH}" "${@}"
pyvenv-3.3 "${VENV_PATH}" "${@}"

mkdir -p "${VENV_PATH}/local"

for dir in lib bin include ; do
    ln -vs ../${dir} "${VENV_PATH}/local/${dir}"
done


Save this script somewhere in your PATH (e.g. /usr/local/bin/mk_pyvenv or maybe ~/bin/mk_pyvenv) and then issue:

$ mk_pyvenv ~/.pyvenv-3.3
$ . ~/.pyvenv-3.3/bin/activate
(.pyvenv-3.3)$ curl -O http://python-distribute.org/distribute_setup.py
(.pyvenv-3.3)$ python distribute_setup.py
(.pyvenv-3.3)$ easy_install pip

And now you finally have a working virtualenv with python-3.3 VENV!

mercoledì 14 novembre 2012

python 3.3 and virtualenv

Tech

As in pep-0405 Python 3 new virtualenv specs are now implemented in Python 3.3.

Install python3.3 and venv it 

 

Python 3.3 has those bleeding edge venvs, lets try them and bleed!



#!/bin/sh
# please add the source if needed
# it could need a package from experimental

set -e # exit in case of errors 

if grep -vqs "experimental main" ; then
    echo "deb http://ftp.de.debian.org/debian/ experimental main" | \
    sudo tee -a /etc/apt/sources.list > /dev/null
fi

sudo apt-get install python3.3
pyvenv-3.3 .pyvenv-3.3
. .pyvenv-3.3/bin/activate 
 
 

Install pyglet 

# pip install pyglet # if lucky and in the future
 
# maybe check for something >= 1.2 on http://www.pyglet.org/download.html#unstable
wget http://pyglet.googlecode.com/files/pyglet-1.2alpha1.tar.gz
cd pyglet-1.2alpha11
python setup.py install
 

  Install pip

curl -O http://python-distribute.org/distribute_setup.py
python distribute_setup.py
I dreamed now for

easy_install pip
 
but I had to do that uglyness below, I'm missing a prefix probably (I hope!)


mv .pyvenv-3.3/local/lib/python3.3/dist-packages/ .pyvenv-3.3/lib/
easy_install-3.3 pip

Is there something wrong here?

lunedì 15 ottobre 2012

Python: "source" a shell script and read variables

It comes handy to "source" a shell script and read variables from there in python, maybe to share configuration between different scripts.

Here is my* take:

from subprocess import Popen, PIPE
from os import environ

def source(script, update=True, clean=True):
    """
    Source variables from a shell script
    import them in the environment (if update==True)
    and report only the script variables (if clean==True)
    """

    global environ
    if clean:
        environ_back = dict(environ)
        environ.clear()

    pipe = Popen(". %s; env" % script, stdout=PIPE, shell=True)
    data = pipe.communicate()[0]

    env = dict((line.split("=", 1) for line in data.splitlines()))

    if clean:
        # remove unwanted minimal vars
        env.pop('LINES', None)
        env.pop('COLUMNS', None)
        environ = dict(environ_back)

    if update:
        environ.update(env)

    return env

Also available as a github gist.

* Heavily inspired from this pythonwise blog post