You've found my spot on the internet that's all about me. I put together this little site to show off my work and keep track of my various projects.

Upgrading It All. Getting Python and Django Running in a Virtual Environment on CentOS 6, part 1


At my day job, one of the biggest projects that got me into Django was a hardware manager app that helped me with inventory and access to the servers and components that I work with. The internal website showed IP addresses, firmware levels, and other VPD data and was easy to use. The one catch was that the information was regularly updated only twice a day so I later added a way for a user to manually update the information if they wanted to.

This helped make the site snappy and fast. It was a huge time saver when I, or others, had to dive into a new server environment. Just add a few IP addresses and credentials, let the app scan the environment, and you were staring at all the hardware in your browser. I even used graphics from the web to make it look a bit polished.

The project is called Overview and not only was I starting my first Django app, I was also diving into hosting files on a Linux server using Apache. Almost two years have gone by and the app has grown considerably, running well on Django 1.6.

Then I started to feel that I was drifting away from the latest versions. My problem? I was running Django on the OS Python of 2.6.6. It’s been years and my Linux skills have vastly improved since I set up this Django installation so the mission today is to do the following:

  • Install the latest Python 2.7. (2.7.9 at the time of this writing)
  • Install the latest version of Django. (1.8 time of writing)

To accomplish all this, I will use virtual environments using virtualenv. This allows you to create your own environment of packages that don’t mix with the packages running on the operating system. It’s good practice to use virtual environments for python programming so that your apps use a different python installation (2.7.9) versus the python (2.6.6) that the operating system is using. You shouldn’t mess with the OS Python cause of special needs of the operating system.

1. Install Python 2.7.9
Let’s start with some commands and then the explanation.

[leo@overview ~]# yum groupinstall "Development tools"
[leo@overview ~]# yum install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel xz-devel httpd-devel
[leo@overview ~]# wget https://www.python.org/ftp/python/2.7.9/Python-2.7.9.tar.xz
[leo@overview ~]# tar xf Python-2.7.9.tar.xz
[leo@overview ~]# cd Python-2.7.9
[leo@overview Python-2.7.9]# ./configure --prefix=/usr/local --enable-unicode=ucs4 --enable-shared LDFLAGS="-Wl,-rpath /usr/local/lib"
[leo@overview Python-2.7.9]# make && make altinstall
[leo@overview Python-2.7.9]# /usr/local/bin/python2.7 -V
Python 2.7.9
[leo@overview Python-2.7.9]# python -V
Python 2.6.6

Credit must be given. I followed Daniel Eriksson’s instructions on this wonderful blog post.

In this guide I will show you how to install Python 2.7 and 3.3 on CentOS 6. The examples below are for Python 2.7.6 and Python 3.3.5, but the procedure is the same for any modern version of Python including the upcoming Python 3.4.0.

How to install Python 2.7 and Python 3.3 on CentOS 6 via Too Much Data.

In the commands above, I install some recommended packages first before doing the python install.

Next, download the python file with wget and start the installation. Eriksson gives a great explanation of the ./configurecommand and here’s the one that I used. It is very important to use altinstall with your second make command. This will keep the Python 2.7.9 installation sitting nicely alongside the OS Python of 2.6.6.

Lastly, I’m showing here that you can run either version of Python.

2. Set up virtualenv
Next, we want to get our virtual environment created so that we have a nice little repository of packages that are segmented from the operating system. The commands:

[leo@overview Python-2.7.9]# /usr/local/bin/python2.7 -m ensurepip
Ignoring indexes: https://pypi.python.org/simple/
Downloading/unpacking setuptools
Downloading/unpacking pip
Installing collected packages: setuptools, pip
Successfully installed setuptools pip
Cleaning up...
[leo@overview Python-2.7.9]# pip install virtualenv
Downloading/unpacking virtualenv
   Downloading virtualenv-12.1.1-py2.py3-none-any.whl (1.6MB): 1.6MB downloaded
Installing collected packages: virtualenv
Successfully installed virtualenv
Cleaning up...
[leo@overview Python-2.7.9]# mkdir ~/virtualenvs
[leo@overview Python-2.7.9]# cd ~/virtualenvs
[leo@overview virtualenvs]# virtualenv --python=/usr/local/bin/python2.7 my_django_env
[leo@overview virtualenvs]# source ~/virtualenvs/my_django_env/bin/activate
(my_django_env)[leo@overview virtualenvs]# python -V
Python 2.7.9

In this step, we will install the package installer Pip, which comes bundled with Python 2.7.9. ensurepip will install it and then you can use it to install virtualenv.

I’m trying to stay organized so I created a ‘virtualenvs’ directory and will create any future ones here. Notice that I am specifying the python that I want ‘my_django_env’ to use.

Finally, when activating the virtual environment, notice the new prompt that you get. You are now working with the virtual environment and simply typing the python -V command shows you that you are using the Python 2.7.9 installation instead of the operating system’s 2.6.6.

To further show this, I can deactivate the virtual env and see what python version I’m running.

(my_django_env)[leo@overview virtualenvs]# deactivate
[leo@overview virtualenvs]# python -V
Python 2.6.6
[leo@overview virtualenvs]# source my_django_env/bin/activate
(my_django_env)[leo@overview virtualenvs]# python -V
Python 2.7.9

Hopefully that concept is now clear for you.

3. Install Django 1.8

Now, I’ll install the latest version of Django inside of my virtual environment.

(my_django_env)[leo@overview virtualenvs]# pip install django
(my_django_env)[leo@overview virtualenvs]# python
Python 2.7.9 (default, Apr 23 2015, 09:46:30)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-11)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import django
>>> django.VERSION
(1, 8, 0, 'final', 0)

Simple. Just use pip to install Django and then you can verify the version by going into python and checking it.

So at this point, I still have my Django 1.6 installation but it is sitting on the operating system compared to this new Django 1.8 installation which is running in the virtual environment.

4. Migrate apps to Django 1.8
We’re now in great shape to start building Django apps for Django 1.8. When starting new, just jump into your virtual environment and run the apps there. However, what should we do about the apps that were already built for Django 1.6?

I think this depends on your apps but a few tweaks needed to be made for Overview.

First, install all the packages that your app needs inside your virtual environment. Pip should make this super easy.

Next, I wanted to make the python manage.py validate command happy.

(my_django_env)[leo@overview Overview]# python manage.py validate
/home/leo/virtualenvs/my_django_env/lib/python2.7/site-packages/django/core/management/commands/validate.py:15: RemovedInDjango19Warning: "validate" has been deprecated in favor of "check".

/home/leo/virtualenvs/my_django_env/lib/python2.7/site-packages/django/db/utils.py:358: RemovedInDjango19Warning: Router.allow_syncdb has been deprecated and will stop working in Django 1.9. Rename the method to allow_migrate.

System check identified no issues (0 silenced).

Looks like I need to change my thinking. The validate command needs to be check instead.

(my_django_env)[leo@overview Overview]# python manage.py check
/home/leo/virtualenvs/my_django_env/lib/python2.7/site-packages/django/db/utils.py:358: RemovedInDjango19Warning: Router.allow_syncdb has been deprecated and will stop working in Django 1.9. Rename the method to allow_migrate.

System check identified no issues (0 silenced).

I had a database router in my Django project so renaming allow_syncdb to allow_migrate should fix this warning.

(my_django_env)[leo@overview Overview]# python manage.py check
System check identified no issues (0 silenced).

Looks like the check command is now happy. What about syncdb?

(my_django_env)[leo@overview Overview]# python manage.py syncdb
/home/leo/virtualenvs/my_django_env/lib/python2.7/site-packages/django/core/management/commands/syncdb.py:24: RemovedInDjango19Warning: The syncdb command will be removed in Django 1.9
  warnings.warn("The syncdb command will be removed in Django 1.9", RemovedInDjango19Warning)

Looks like it is time to learn about migrations. I highly recommend this section of the Django docs.

Wrapping up here. Note, the project is named Overview but the app is called rsl.

(my_django_env)[leo@overview Overview]# python manage.py makemigrations rsl
Migrations for 'rsl':
    - Create model Chassis
    - Create model CMM
    - Create model FanPack
    - Create model ImportantLink
    - Create model IOModule
    - Create model ITE
    - Create model jumpbox
    - Create model Lab
    - Create model MezzCard
    - Create model PowerSupply
    - Create model Rack
    - Create model rsluser
    - Add field racks to lab
    - Add field users to lab
    - Add field lab to importantlink
(my_django_env)[leo@overview Overview]# python manage.py migrate --fake-initial
Operations to perform:
  Synchronize unmigrated apps: staticfiles, messages
  Apply all migrations: admin, rsl, contenttypes, auth, sessions
Synchronizing apps without migrations:
  Creating tables...
    Running deferred SQL...
  Installing custom SQL...
Running migrations:
  Rendering model states... DONE
  Applying rsl.0001_initial... OK
(my_django_env)[leo@overview Overview]# python manage.py migrate rsl
Operations to perform:
  Apply all migrations: rsl
Running migrations:
  No migrations to apply.

This now adds the ability to make new migrations on an already active database.

Next step, is to get Apache running with virtualenv. That’s a post for the future.

Leave a Reply

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

%d bloggers like this: