Saturday, August 12, 2017

Migration to Python 3 and virtualenv

So far Python Power Electronics has been built on Python2. I have Python 2.7.9 on computer and most of the other versions of Python I have used are quite similar i.e Python 2.7.x. Python 2 is now legacy and support will be disabled from 2020. Though that is a couple of years away, I figured I should start migrating since Python 3 is now well established and almost every OS ships with Python 3 by default and it may be soon when installing Python 2 may actually become an issue.

To start this migration, I don't want to do a system wide installation of Python 3 as it will break all my projects. So I am starting with virtualenv. To begin with, I am installing Python 3.5.4 into a separate folder.

In my Linux system, the system wide packages are installed in /usr. So I am installing Python 3.5.4 in home:
/home/user/python_3_5_4
In the above user is the name of the user that you are logged in as. For that matter, you could install this in any other folder of your choice. I would strongly suggest that you install Python 3.5.4 in a user writable directory and not in a root directory.

So, download Python 3.5.4 from the Python website:
https://www.python.org/downloads/source/

So let us suppose, this is downloaded to:
/home/user/Downloads
Extract the zip file to:
/home/user/Python-3.5.4/
Change into this directory. In this directory you will find the entire source.

There will be some dependency issues - something is missing etc. I had a problem with libssl-dev being missing. I installed it using apt-get on my Debian system.

Anyway, first compile the source:
/home/user/Python-3.5.4/ $ ./confiure --prefix=/home/user/python_3_5_4

The prefix states that Python should be installed in a special folder and not in /usr/local/ which is the default.
If you get any errors and you might, Google each error and check which package is needed. Quite often it may say a package is missing when it is installed. What might actually missing is the development package. For example libssl may be installed, but libssl-dev may be missing. But that may cause it to exit with an error.

After that run the command:
/home/user/Python-3.5.4/ $ make

And if no errors:
/home/user/Python-3.5.4/ $  make install

The last command will create within /home/user/python_3_5_4 separate bin/, lib/ etc directories where Python will be installed.

Now, if you check:
python -V
You should get Python 2.7.x.

But if you run:
/home/user/python_3_5_4/bin/python3 -V
You should get Python 3.5.4.

So this means Python 3.5.4 has been installed in a separate directory. Now to begin using it.

Create a directory in home or anywhere else which will be the separate environment for Python 3.5.4. For example,
/home/user/python_3_5_4_virtual

Change into this directory. Now you need virtualenv installed. I didn't have it so I used apt-get again to install virtualenv.

Run the command:
/home/user/python_3_5_4_virtual $ virtualenv -p /home/user/python_3_5_4/bin/python3 python3_install

What this will do it create a virtual environment but will use this Python 3.5.4 installed in an isolated directory to create an install environment in the directory called python3_install. So python3_install will contain all software related to Python - django, matplotlib, numpy, scipy etc.

Once this is done, activate it:
/home/user/python_3_5_4_virtual $ source python3_install/bin/activate

Now a special session will start. If within this session, you run:
(python3_install) /home/user/python_3_5_4_virtual $ python -V
You will get Python 3.5.4. The (python3_install) in bracket means you are in a virtual environment.

If you exit this environment by running:
(python3_install) /home/user/python_3_5_4_virtual $ deactivate

You exit. Which means:
/home/user/python_3_5_4_virtual $ python -V
Gives Python 2.7.9. Notice that (python3_install) has disappeared meaning you have exited the virtual environment.

So, to perform the migration, I will enter this virtual environment, install dependencies with Python 3.5.4 as the base. I will check the operation of the simulator bit by bit.

No comments:

Post a Comment