Introduction to the Django Setup Tutorial
Hi guys, this is Mike Hiber back again with another Python tutorial. This tutorial is going to be a series of tutorials and we're going to begin with the installation and setup of Django on your system, Django being the web development system that most people choose these days when they're thinking of doing web applications written in Python.
Now through the course of this, we're going to also look at a few of the tools that most of Python developers use in setting up applications, things like easy install, things like virtualenv, those things that we use so that we can easily manage Python-based applications and make sure that we can cope with dependencies of modules and versions and that sort of thing. So I'm going to try and make this as simple as possible so that you can get straight from A to B and get yourself installed and up and working pretty quickly doing Django applications.
So the first thing we're going to look at is how to get the easy_install script. Now the easy_install script is basically so that we can install Python modules from PyPI, which is the the Python Package Index, which is a repository of modules that have been specially prepared ready for us to install that you can get pretty much to install on most web servers around the world.
Now I'm going to be dealing mostly Linux, so I hope that your Windows users or your Mac users can follow along with this. I'm pretty sure that you can, and with a little bit of Googling you'll probably be able to get the exact commands for your particular operating system. I know for a fact that Mac will probably be very similar to what I'm about to do here.
So let's go. So how do we get the easy_install script? Well, the easy_install script is part of a package called python-setuptools. So to install it on my system we basically go:
sudo apt-get install python-setuptools
Oops. So python hyphen setup tools. I've already installed this so it should just tell me that I've already got the current version, but let's see what happens. I'll type in my password. And there you go, python-setuptools is already the newest version. So I've already got it on here. I did that so that you wouldn't have to sit there and watch my dark screen whilst it downloaded.
Understanding virtualenv for Sandboxed Environments
So there you go, that's installed now. Now that allows us to use the easy_install script, and we'll use the install script to then install virtualenv. virtualenv is a setup that we can use so that we can more or less sandbox our Python applications, so that we can put them in their own individual environment that has its own packages, has its own references, and own Python path so that everything that we import comes from that local space rather than from our system.
The advantage of that is that my system doesn't have to have a fully fledged install of Django in my root system. I can do that in a separate folder away from all of my main system files, which is really great when you're running on different versions of Django or different versions of modules even, because then you don't have to sit there hoping that the dependencies aren't going to clash with each other. Because you can prepare a special folder with all of those correct modules ready to work with whatever you're trying to develop in that one space. And you can have it segregated off from everything else so it really keeps everything nice and clean and keeps you from having the headache of all these modules that clash with one another. So it's really useful.
Installing virtualenv with easy_install
So here's how to install it. What we do is we go:
sudo easy_install virtualenv
And then after a while, that should download and install. I've already got it installed as you can see there, so it's it's already there. So those are the two main applications that we have to install using the sudo command. Once we've done that, we can then start and use virtualenv so that we can cordon off all of our code and our modules into a separate folder that won't affect our main system. So we won't have to do any more sudo after this point.
Creating a New Virtual Environment
So the next step is now to use virtualenv to create us a folder that will be able to cordon off everything and we can then install our Django installation. So here it goes. We do the following command:
virtualenv --no-site-packages
Now just to stop there and talk about that. The no-site-packages section, or switch rather, means that when virtualenv creates this separate folder with all its various modules, we're not going to include any modules that are already installed on this existing system. So whatever modules I've already installed in my root folders for my Global installation of Python will not be copied into this folder. We want everything to be from scratch without any other modules that are going to clash with that. So if I've got modules in there for, I don't know, a games development package that has absolutely nothing to do with my Django installation, I don't want that to be pulled in. I don't want that to be cluttering up the installation, and I don't want it to add extra space or extra data into where it's not needed. So saying don't use the site packages. In this case, we want a very bare installation folder.
So we do that by saying hyphen hyphen no hyphen site hyphen packages, and then finally we're just going to add in a name for the folder. So you can call this anything you like, django-mike maybe, whatever you fancy. Although a naming scheme might be helpful, that's up to you though. And then we press enter and that's pretty much it. New Python executable in django-hyphen-mike/bin/python. Installing setup tools and pip. So those two are to do with just using an easy_install or the pip command, which is a similar command to easy_install, and it just lets us put more modules in once we're using the virtual environment.
Activating the Virtual Environment
So now that we've created that virtual environment, we want to be able to activate it so that whenever we run any installation commands from now on, it will automatically put those within our virtual environment and not in our root folder. So to activate the virtual environment, to make that the current environment that we're working in, we run the following command. We first of all go:
source django-hyphen-mike/bin/activate
Now what this will do is it'll tell the shell that I'm currently using to change its context into the folder where I've created this virtual environment. So that things like the root of the installation package location will be rerouted from my root folders in the actual s... my actual laptop through to this folder. So to demonstrate that, we'll go enter and now you can see that it's now recognizing that it's inside of this virtual environment and that any commands to do with easy_install or pip will now install packages within this current environment.
So I'm going to just go inside of that folder. You can see we've got the bin, include, lib, local folders and it's basically treating this as almost as if it's the root folder of your system. But as we know it's not, it's a separated folder.
Installing Django Inside the Virtual Environment
So now we've actually activated that, we want to be able to install some packages, the main one being that we want to install Django. So to install Django, we're going to now use our easy_install script. Excuse me. And notice now we're not using the sudo command in this case. We don't need to because we're inside the virtual environment. It's almost like we are already using the root user, so we don't have to run sudo anymore. Everything that goes on within this current environment is classed as root, although we're nowhere safely being sandboxed away from the actual root system. Sorry to cloud that up with some weird kind of Linux-based user terminology, but it just basically means that you're safe when you install these packages, that you're not going to completely ruin your system. It's going to be kept separate in this nice separate folder.
So easy_install is what we're going to do and we're going to say:
easy_install django
And then just press enter. I'm hoping this is not going to take very long. Now what it's doing is it's actually gone off to the Python Package Index website, looks for the package called Django. It's then been redirected to the Django project website where it's been given details of what the file is. Then it's noticed that the current version available is Django 1.4.3 and now it's downloading that zip file. Once it's downloaded that, it'll install that into the relevant folders of this current virtual environment and then that should then functionally be as if we had actually installed it into our route and we can use it as a module. And now you can see it's now installing there, so that's great. And we're all done. Didn't take very long. And believe me, that wasn't very long because I'm not on a very, very quick internet connection yet, it's probably 1.2 megabits per second at the top rate. So it isn't very fast at all, but it didn't take long. So that's great.
Creating Your First Django Project
Now if we look in this folder again, we've got bin. In the bin folder, if we have a look in the bin folder, we have our activate script which is what we use to activate the virtual environment, we have our easy_install script and pip, and our Python executable. But we also have this new one called django-admin. And that's the main script that we use to install a working Django project.
Now we're not going to go anywhere further than just basically creating a project with this first tutorial because I don't want to add too much detail into here and make it so that it just blows your mind. So the last couple of things that I'm going to show you are just basically how to create your project first of all, and then we're going to test it to see if it works. And then finally, I'll show you how to deactivate your virtual environment so that you can go back to the way the shell was working before.
So how do we create a Django project? Well django-admin.py has a nice little set of help instructions, so if we just type its name, it'll give you the list. Now you get all of these pieces of information but the bit we're looking for is that one there: startproject. So we're going to go:
django-admin.py startproject django_test
I noticed I used an underscore there in the name because you can't use hyphens in the project names. That's something that you'll find out if you attempt to try and stick something with a hyphen in there, but you can always replace it with an underscore. So this is to create our first project. If you just press enter there, very quickly executes that script and then you can see there's now a folder called django_test.
Running the Django Development Server
Now if we go inside of there, we can now test our server. If I just remember how to do a directory listing... there we are. So we have a project folder and a manage.py file. Now the manage.py file is very useful in the process of developing your applications. And in fact, a lot of the things that you'll do later on in terms of, you know, configuring your system and also setting off your actual web server is done through the manage script. So we're just going to basically demonstrate that one of the features in the manage script here by just saying:
runserver
And this is basically how we test if our server is actually working. As we can see there, it's running the server. There are zero errors found. It even tells us that we can then run our web page in a browser from that link. So let's open that link and you'll see there the basic web page that you get when you do a Django installation. It also gives you some instructions on things like setting up your database, and also telling you that the reason why you're seeing this, it's because you haven't configured the rest of the system yet. But we're not going to do that in this example because this is the extent of what I want to show you in this tutorial because I don't want to make it any more complicated than it is already.
Deactivating the Environment and Conclusion
So now we've tested the server, we've got everything up and running. We can press Control+C and that will kill the test web server. Now finally we want to switch off or deactivate our virtual environment because we don't want to continue using it at this point. So how do you think we're going to do that? Very simply we're just going to go:
deactivate
It's as simple as that. And now you can see we're no longer inside this environment here. We're back to the normal prompt and all of our Python defaults and Python paths have been reset back to normal.
That's the end of this tutorial. I hope you've found it informative and instructional and I hope that you benefited from it. If you like this this video or it helped you in any way, please click the like button. If you'd like to know more about future tutorials or want to see the rest of this series, please click the subscribe button and hopefully I'll be able to post more lessons for you in the future. If you've got any suggestions that you want to, you know, see any tutorials on that are in relations to Python, then please feel free to send me a message in the comment box below. Thanks for watching.