Archive
creating a Python 3 virt. env. on Ubuntu
Problem
Python 3 on Ubuntu is sometimes a total mess. I wanted to create a Python 3 virt. env., but I got this error:
$ virtualenv -p python3 venv
Running virtualenv with interpreter /usr/bin/python3
Traceback (most recent call last):
File "/usr/local/lib/python2.7/dist-packages/virtualenv.py", line 8, in
import base64
File "/usr/lib/python3.4/base64.py", line 9, in
import re
File "/usr/lib/python3.4/re.py", line 324, in
import copyreg
File "/usr/local/lib/python2.7/dist-packages/copyreg.py", line 3, in
from copy_reg import *
ImportError: No module named 'copy_reg'
Error in sys.excepthook:
Traceback (most recent call last):
File "/usr/lib/python3/dist-packages/apport_python_hook.py", line 53, in apport_excepthook
if not enabled():
File "/usr/lib/python3/dist-packages/apport_python_hook.py", line 24, in enabled
import re
File "/usr/lib/python3.4/re.py", line 324, in
import copyreg
File "/usr/local/lib/python2.7/dist-packages/copyreg.py", line 3, in
from copy_reg import *
ImportError: No module named 'copy_reg'
Original exception was:
Traceback (most recent call last):
File "/usr/local/lib/python2.7/dist-packages/virtualenv.py", line 8, in
import base64
File "/usr/lib/python3.4/base64.py", line 9, in
import re
File "/usr/lib/python3.4/re.py", line 324, in
import copyreg
File "/usr/local/lib/python2.7/dist-packages/copyreg.py", line 3, in
from copy_reg import *
ImportError: No module named 'copy_reg'
Awesome! :(
Solution
I found a working solution here. The following command let me create a Python 3 virt. env.:
python3 -c 'import sys; del sys.argv[0]; s = sys.argv[0]; exec(open(s).read(), {"__file__": s, "__name__": "__main__"})' `which virtualenv` -p python3 venv
Life on Arch / Manjaro is easier. There Python 3 has been the default for years.
Install Python 3.2 and/or Python 2.5 on Ubuntu 11.04
Update (20121120): Google App Engine supports Python 2.7 so there is no need any more to install Python 2.5. I consider this post outdated.
Chad Lung has a nice post on installing Python 3.2 from source on Ubuntu 11.04. The future versions of Ubuntu will come with Python 3, so maybe it’s a good time to start to discover Python 3 a bit.
Notes
The package “python3.2” exists in the Ubuntu repositories but it’s not up-to-date. If you want the latest version of Python, you’ll have to install it from source.
If you install Python 3.2 from source, make sure to install it with “./configure; make; sudo make altinstall“, where the emphasis is on altinstall. This way Python 3.2 will be installed next to your existing 2.x version(s), so your system won’t be messed up.
To figure out your exact Python version, you can do this:
>>> import sys >>> print(sys.version_info[:]) (3, 2, 0, 'final', 0)
Install Python 2.5 (update, 20110926)
Edit (20111112): Google App Engine now supports Python 2.7! At the moment it’s experimental but it works. So it’s very likely you don’t need Python 2.5 at all.
I wanted to try Google App Engline but it runs on Python 2.5 on the production servers at Google. So it’s better to test your applications locally with Python 2.5 too, otherwise there is a good chance that you develop something that runs fine on your machine but breaks at Google.
So, how to install Python 2.5 keeping the newer Python versions too? I tried the method that I described above with version 3.2 but “make” failed. But in this post I found a PPA from where you can install Python 2.5 easily. Steps to follow:
sudo add-apt-repository ppa:fkrull/deadsnakes sudo apt-get update sudo apt-get install python2.5
Now if you want to use Python 2.5, just modify the first line of your scripts:
#!/usr/bin/env python2.5
Update: I figured out how to compile 2.5 from source. “make” produced the following error message: “/usr/include/sqlite3.h: version 3.7.4”. After “configure”, open Makefile and edit this line:
# before: # LDFLAGS= # after: LDFLAGS= -L/usr/lib/i386-linux-gnu
After this compilation was done successfully with “make”. This tip is from here.
Linux: Python text-to-speech
This is the Linux version of this post.
#!/usr/bin/env python
import os
from time import sleep
text = "text to speak"
cmd = 'espeak "{0}" 2>/dev/null'.format(text)
os.system(cmd)
sleep(1)
os.system(cmd)
