Django Framework on Python’s Virtualenv

I have been interested in Python programming language and Django framework for website development, for a long time now. I have no formal education in programming and development. The learning curve for me at least, has been very high, but patiently I plod along in the hopes that one day with lots of practice it will all click for me.

The best way to play with and learn Python Django is to use Python Virtualenv

Python’s Virtualenv is very easy to install and use. For example you find a tutorial online and want to try it out as a learning exprience, and the problem is that it’s based on an older version of Python Django framework. With Virtualenv you create a new folder activate it and download which ever version of Django the tutorials is based on. You follow the tutorial and see your results, then deactivate the Virtualenv and delete the folder, so that you don’t take up unneeded space on your hard drive. so let’s walk through the steps

Please note that I stopped using Windows and any other proprietary software many years ago. I am very much into Open Source Softwares. If you’re still using Windows then I am sorry, I won’t be able to answer your questions about it.

My operating system is Debian-Jessie, one of many flavors Linux comes in. Python is included in all Linux operating systems, Virtualenv you will need to install from your Linux package system.

First let’s see if django is already installed in your system. Type:

$ Python -c “import django; print(django.get_version())”

This is the command to see which version Django is installed in your environment if any.

There are few ways to implement Python Virtualenv, I am only showing the ones I got in habit of using.

1. Virtualenv command plus a directory name and which python versions to use.

$ virtualenv env1 -p /usr/bin/python3
$ source bin/activate
(env1)$

2. Create a directory and change into that directory and then run virtualenv and which python to use.

$ mkdir env1

$ cd env1

$ virtualenv . -p /usr/bin/python3

$ source bin/activate

(env1)$

The code -p /usr/bin/python3 tells virtualenv to use python3.4 which is prefered version to use with Django1.8. Otherwise virtualenv uses the system python2.7.

I prefer the second method and directories called env number 0-N for each projects, each with a README.txt file describing the project and versions used.

To deactivate your environment:

(env1)$ deactivate

When you activate your environment then it’s time to install django and any other software you need.

This command installs the latest django version which as of this writing is django-1.8

(env1)$ pip install django

or if you want particular version of django then do

(env1)$ pip install django==1.7

Run this command anytime you need to know what packages have been installed so far in environment.

(env1)$ pip freeze

pip freeze lists the installed packages.

Django==1.8.2

wheel==0.24.0

I’m going to demonstrate how I develop with virtualenv using python3.4 and Django1.8.

$ mkdir kam_python

$ cd kam_python

$ mkdir django18

$ virtualenv env1 -p /usr/bin/python3

$ cd env0

$ ls

bin lib      // so far this is what is installed in env0

$ source bin/activate

(env1)$

(env1)$ python –version

Python 3.4.2
(env1)$ pip install django

Collecting django

 Using cached Django-1.8.2-py2.py3-none-any.whl

Installing collected packages: django

Successfully installed django-1.8.2

(env1)$
(env1)$ pip freeze

Django==1.8.2

wheel==0.24.0

(env1)$

Now I’m all set to follow latest Django1.8 Tutorials.

Note Double-check that you included the period (.) at the end of the command, it’s important because it tells the script to install Django in your current directory.

(venv1)$ django-admin startproject mysite .

(env1)$ ls

bin lib manage.py mysite pip-selfcheck.json README.md

This way you only have one instance of mysite instead of usual 2, all because of that period(.) at the end of startproject command.

You can also start a project mysite and change the first instance of mysite to (src) and leave the second as original mysite (I like this one too)

The most common method for you to install the new packages is by using the pip

command. There are the three versions of the pip install command, they are

as follows:

$ pip install PackageName

This is the default and installs the latest version of the package:

$ pip install PackageName==1.0.4

Using the == parameter, you can install a specific version of the package. In this case,

that is 1.0.4. Use the following command to install the package with a version number:

$ pip install ‘PackageName>=1.0.4’ # minimum version

Use the above command when you are not sure of the package version you are going

to install but have an idea that you need the minimum version of the library.

I hope this post was of some help to all beginner Python web developers

Notice any mistakes or confusion please let me know.

Besides the tutorial on Python Django’s home page, (which is very good):

www.djangoproject.com

there is another site that I really like, but not exclusively, for there are many out there and that is:

www.djangogirls.org

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s