How to Spur Python 3 Adoption
2013-01-26
Update: See the update below for a much easier way to quickly get Distribute and pip installed into a venv.
Recently I thought I’d manually install and try out Python 3 a bit (v3.3.0). After installing a few prereqs, I went through the usual configure/make steps mostly without issue.
For those curious about the details, I made sure I had these prerequisites installed:
libreadline6-dev libsqlite3-dev zlib1g-dev tk8.5-dev libncurses5-dev libexpat1-dev libbz2-dev libdb5.1-dev libgdbm-dev liblzma-dev libssl-dev
Then:
cd ~/opt mkdir py-3.3.0 cd src wget http://www.python.org/ftp/python/3.3.0/Python-3.3.0.tgz tar xzf Python-3.3.0.tgz cd Python-3.3.0 ./configure --prefix=/home/john/opt/py-3.3.0 make make test make install cd ~/opt ln -s py-3.3.0 py3 # Add $HOME/opt/py3/bin to my PATH # logout/login # ... # Quick check: python3 -V
and it checks out.
Had one test fail,
FAIL: test_urlwithfrag (test.test_urllib2net.OtherNetworkTests)
but looks like it’s been addressed for forthcoming releases (or else maybe I was supposed to preemptively have
python3
on my PATH? Hm.).
I then noticed that Python 3 comes with its own built-in pyvenv
program, an apparent replacement for virtualenv. Nice. Gave it a try:
cd ~/temp
mkdir my-proj
cd my-proj
touch foo.py
pyvenv my-env
. my-env/bin/activate
which pip
Eeek! It points to my system’s Python 2.x /usr/bin/pip. There’s no pip in my-env/bin. There’s no pip in ~/opt/py3/bin either. Well, that’s less than convenient. Ok. Deactivate the env:
deactivate
and let’s install pip.
The pip install instructions say I must have Distribute installed to use pip with Python 3.x. Ok, I’ll just leisurely make sure it’s already installed and … I don’t see it anywhere. Fine, will install it:
cd ~/temp
wget http://python-distribute.org/distribute_setup.py
python3 distribute_setup.py
and now install pip:
wget https://raw.github.com/pypa/pip/master/contrib/get-pip.py
python3 get-pip.py
OK! Now we should be ready to go! Let’s try again:
cd ~/temp/my-proj
rm -fr my-env
pyvenv my-env
ls -l my-env/bin
And pip
is still not in there! :|
This is a problem. The main point of creating an env for a project is so I can install whatever sundry modules I want for it without clogging up the main Python’s site-packages dir.
Maybe at this point I’m supposed to install virtualenv and ignore pyvenv. I don’t know. But my guess is that by now many prospective Python 3 users will just assume 3.x is not ready, and go back to 2.x.
My conclusion: If you want to spur Python 3 adoption, Distribute and pip should be included with it.
Update
2013-01-30
Vinay Sajip created a helpful little script for creating a virtual environment and installing Distribute & pip into it. You use it in place of the pyvenv
which comes with Python 3.3.
First, make sure you have Python 3.3.x installed.
Then download the pyvenvex.py script from https://gist.github.com/4673395. Put it into your ~/bin (which I expect is on your PATH). chmod +x
it. Edit it, adding a shebang line to the top pointing to your python3 (which must be version 3.3.x). Mine says:
#!/home/john/opt/py3/bin/python3
To use it:
cd ~/dev # or wherever
mkdir my-proj
cd my-proj
pyvenvex.py my-env
. my-env/bin/activate
which pip # should point to the one in my-env/bin
pip install WhateverYouNeed
# work work work
deactivate # when done
Very useful indeed! Thanks, Vinay!