personal web log written by izabeera and dryobates

python freebsd

Multiple Python versions in FreeBSD

by dryobates

It’s easy to install multiple versions of Python with FreeBSD ports. Less obvious is how to install libraries for given Python version.

FreeBSD port collection is constructed in a way that we have:

  • one main Python port lang/python, which installs default Python for given system
  • ports for every main Python line (currently lang/python2 and lang/python3), which installs most up to date Python version in given line
  • ports for every currently supported versions of Python ( lang/python27, lang/python32, lang/python33, lang/python34)

My system default python version is 2.7, but I need to test some of my python libraries in more version. So I simply installed all of them:

$ cd /usr/ports/lang/python32
$ make install clean
$ cd /usr/ports/lang/python33
$ make install clean
$ cd /usr/ports/lang/python34
$ make install clean

I can use all of them at the same time:

$ python2.7 -V -c "exit()"
Python 2.7.9
$ python3.2 -V -c "exit()"
Python 3.2.5
$ python3.3 -V -c "exit()"
Python 3.3.5
$ python3.4 -V -c "exit()"
Python 3.4.3

I had a problem with sqlite3:

$ python3.2
Python 3.2.5 (default, Nov  6 2014, 10:29:28)
[GCC 4.2.1 Compatible FreeBSD Clang 3.3 (tags/RELEASE_33/final 183502)] on freebsd10
Type "help", "copyright", "credits" or "license" for more information.
>>> import sqlite3
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python3.2/sqlite3/__init__.py", line 23, in <module>
    from sqlite3.dbapi2 import *
  File "/usr/local/lib/python3.2/sqlite3/dbapi2.py", line 26, in <module>
    from _sqlite3 import *
ImportError: No module named _sqlite3

With default Python version I can install it from "databases/py-sqlite3" doing "make install" like above. How to install it for Python3.x? There's no py34-sqlite3 port.

With python2.x I could install a replacement pysqlite through pip, but with python3.x that package is not supported any more:

$ pip3.4 install pysqlite
Downloading/unpacking pysqlite
  Downloading pysqlite-2.6.3.tar.gz (76kB): 76kB downloaded
  Running setup.py (path:/var/tmp/pip_build_jstolarski/pysqlite/setup.py) egg_info for package pysqlite
    Traceback (most recent call last):
      File "<string>", line 17, in <module>
      File "/var/tmp/pip_build_jstolarski/pysqlite/setup.py", line 85
        print "Is sphinx installed? If not, try 'sudo easy_install sphinx'."
                                                                           ^
    SyntaxError: Missing parentheses in call to 'print'
    Complete output from command python setup.py egg_info:
    Traceback (most recent call last):

  File "<string>", line 17, in <module>

  File "/var/tmp/pip_build_jstolarski/pysqlite/setup.py", line 85

    print "Is sphinx installed? If not, try 'sudo easy_install sphinx'."

                                                                       ^

SyntaxError: Missing parentheses in call to 'print'

----------------------------------------
Cleaning up...
Command python setup.py egg_info failed with error code 1 in /var/tmp/pip_build_jstolarski/pysqlite
Storing debug log for failure in /home/jstolarski/.pip/pip.log

The key is to set PYTHON_VERSION environment variable:

$ cd /usr/ports/databases/py-sqlite3
$ make PYTHON_VERSION=3.2 install clean
$ make PYTHON_VERSION=3.3 install clean
$ make PYTHON_VERSION=3.4 install clean
$ python3.2
Python 3.2.5 (default, Nov  6 2014, 10:29:28)
[GCC 4.2.1 Compatible FreeBSD Clang 3.3 (tags/RELEASE_33/final 183502)] on freebsd10
Type "help", "copyright", "credits" or "license" for more information.
>>> import sqlite3
>>> sqlite3.version
'2.6.0'
dryobates
dryobates
Jakub Stolarski. Software engineer. I work professionally as programmer since 2005. Speeding up software development with Test Driven Development, task automation and optimization for performance are things that focus my mind from my early career up to now. If you ask me for my religion: Python, Vim and FreeBSD are my trinity ;) Email: jakub@stolarscy.com

Archive

Tag cloud