Installing another Python version into virtualenv

The idea is that we do not want to install a new version of Python on the system (e.g., for testing purpose), which may interfere with your entire distribution.

Instead, we want to create a virtual environment with virtualenv tool.

Let us assume you have python 2.7 and you want to play with Python 3.4

1. First, the Python executable

cd
mkdir Software
cd Software
wget  https://www.python.org/ftp/python/3.4.1/Python-3.4.1.tar.xz
unxz Python-3.4.1.tar.xz
tar xvf Python-3.4.1.tar
cd Python-3.4.1
./configure --prefix=/home/user/Software/Python-3.4.1/mybuild
make
make install

2. VirtualEnv setup

If you decide to install python3.4 (latest version), make sure you also have the latest version of virtualenv (1.11). I used 1.9 and got this kind of errors:

ImportError: No module named '_collections_abc'
...
ERROR: The executable py34/bin/python3 is not functioning
ERROR: It thinks sys.prefix is ....
ERROR: virtualenv is not compatible with this system or executable

updating virtualenv with

pip install --upgrade virtualenv

fixed the issue.

On another computer, I got this error message:

ImportError: cannot import name 'HTTPSHandler'

which was due to a missing openssl library:

sudo yum install openssl openssl-devel

openssl-devel is the package to install under Fedora. maybe differently named on other distributions.

Let us now create the virtualenv, which is just like a normal directory (let us call it python3)

virtualenv --python=/home/<user>/Software/Python3.4/mybuild/bin/python3.4 virtualenv3
cd virtualenv3

3. Initialise the virtualenv and testing

cd virtualenv3
source bin/activate
python

You should now be able to check the python version to be 3.41

Python 3.4.1 (default, Aug 16 2014, 18:35:43)
[GCC 4.7.2 20120921 (Red Hat 4.7.2-2)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>

Please follow and like us:
This entry was posted in Python, Software development and tagged . Bookmark the permalink.

6 Responses to Installing another Python version into virtualenv

Leave a Reply

Your email address will not be published. Required fields are marked *