7 days of WordPress plugins, themes & templates - for free!* Unlimited asset downloads! Start 7-Day Free Trial

Next lesson playing in 5 seconds

Cancel
  • Overview
  • Transcript

1.3 Set Up the Development Environment

Before we get started creating our application, we are going to set up a basic development environment. In this lesson you will learn how to set up a virtual environment in Python to sandbox your application and force the use of specific versions of software and frameworks. From there we’ll install Django and create the project we’ll be working on for the rest of the course.

Note: Installing Virtualenv

It isn't mentioned in the video, but you will also need to install Virtualenv to follow the instructions in this lesson. You can install the virtualenv tool with this command:

pip install virtualenv

Related Links

1.3 Set Up the Development Environment

The next piece of the puzzle that we're going to need to account for is, where the heck is Django? Well, that is kind of a prerequisite, but the reason I chose to separate this out is I'm gonna cover a little bit of how we're gonna create our development environment in this particular course. And the reason why I'm saving Django until now is because I want to introduce another concept that is very important when you are doing pretty much any sort of Python development, and especially Django development. So let's go ahead and start to work on creating our development environment. So what I'm gonna do is I'm gonna open up a terminal, and I'm gonna go into a directory, or a parent directory, where I ultimately want to house my application. So the first thing that I'm gonna do is I'm going to create a directory that I'm going to put all of my files in for this particular application. So I'm just gonna make a directory, and I'm gonna call this news-agg, as basically what we're trying to do here is create a news aggregator. So I'm gonna go ahead and go into that directory. And as you can see there's nothing in there right now. So the first thing that I wanna do is I want to create what's known as a virtual environment. Now, if you're not familiar with what this is, basically a virtual environment is a way for you, within Python, to kind of create a bit of a sandbox. So what I'm able to do here is I'm able to create this kind of controlled environment where I can absolutely control all the bits and pieces of my application, including the version of Python, including the version of Django, and I can have all of housed within kind of this directory structure that's not going to interfere with all of maybe the versions of Django or Python that I have installed within my platform. So it's not going to touch or use any of those things that I have, say, maybe at a global or system level on my machine, I can control the different versions within this directory structure. So it's kind of a nice habit to get into, especially if you're doing, like I said, any sort of python or Django development. So the way that we do that, the way that we create one of these virtual environments, is we simply go into our directory, and we're going to use the virtualenv command, and then we're going to give it a name. We need to give this some sort of name. You can call it pretty much anything you want because it's gonna create a directory structure for you, and I'm just gonna call this env, which is kind of a common name for creating one of these. So it's gonna go through, and as you see here it's going to create this base prefix and we're pointing at Python 3.4 here, it's gonna create a new Python executable, and it's also creating a Python executable for two and three, as you can see here. So now, if I were to list what's going on in my directory here, you see I've created this env folder, and this is where a lot of those different versions of my executables and different libraries that I'm ultimately going to pull into my application are going to live. And that's kind of that sandbox. So you can go ahead and take a look at what's in there. So I have a bin, include, and live, and this is where all those things are going to live, ultimately. And if I look in bin, you're gonna see I have a number of different things. I have my installation helpers as far as Python is concerned. I have my easy install and pip for both versions two and three of Python. And that's a nice thing. So as you can see, I also have Python 2 and Python 3 here. So that's going to allow me to do all of my work, all of my versioning, install all of my applications, all of my libraries, all within a controlled environment. But before you're actually able to use that or take advantage of those things, you need to activate this particular environment, and the way that we do that is quite simple actually. What we're going to do is we're going to say source, and then we're going to go into whatever directory it was where we have all of this installed. So, if I back out just a little bit. I'm gonna say source and then I need to reach into my environment into bin. And I want to use the activate script. And I'm gonna go ahead and run that. And so now what you're gonna see is prefaced in your command line. You should see the name of the environment that you chose within parentheses at the beginning of your command line. And that's gonna tell you that all the processes, all the things that you're doing right now are within the context of this virtual environment, and that's ultimately what we want. So now that we've done that, I can go ahead and start to install Django. And the way that I'm gonna do that is simply by using pip, which is kind of your Python installation manager. And I'm gonna say install, and I want to install Django. Now if I just leave it as this, it's gonna give me the latest and greatest, but I could definitely say I want to look for, maybe, say 1.8 or whatever version is that I want. But I think the default version or the latest version is gonna be just fine. So it's gonna go through and take care of all of the installation for me, and there you have it. Now we have installed Django. And now we've done this, let's go ahead and start by creating the default project and app that's ultimately going to run our application. And once again, if you're unfamiliar with this terminology, really, a project is just kind of the container that's going to house all of the sub-applications or bits of functionality that are ultimately going to drive this particular application. And if you've never done it before, then you have definitely head back and take at my Getting Started with Django course, but it's fairly straightforward. We're going to use something called the django-admin. And the django-admin is basically just a script that's going to ease the process of creating different things for us. So what we want to do is we want to say startproject and we want to give it a name. And this particular name, and I'm just gonna call this the newsproject, and then I'm gonna specify after that a dot. And it's very important to remember that because it's going to tell the Django admin where you want to create all of this. So let's go ahead and run this command, and if I list my directory now, I now have a news project directory as well as the manage.py file, which is basically going to be our entry point into doing a lot of automated things, when it comes to building out our application. Now that we've done that, let's go ahead and open up our text editor of choice, and mine is going to be atom in this case. And what I want to do is I want to open up the directory structure that we've just kind of created in my text editor, so I can see exactly what's going on. So I'm gonna come into File > Add Project Folder, and I'm gonna come in here, and I want to add my news agg. So I want to go into my root, and then I want to grab the top level directory where all of this is going to live. So I'm gonna select Open here, and now as you can see in my text editor, I've got my news agg, so my parent folder, then I have my environment folder here so you can see all the things that have been going on or all the files and directories that are associated with my virtual environment. And then I can see into my news project folder. I can see my manage.py file, my settings, URLs and all my other supporting Python files associated with my Django project. Now one of the more important things to pay attention to is going to be in your settings file, and we're not gonna get into making too many modifications to this right now. We will make a little bit of of a modification to it later on, when we start to integrate some of the UI capabilities, when we start to deal with CSS and some of those static files, cuz we're going to integrate Bootstrap into this eventually. But right now, we're not to worried about it. You can just kind of see all of the different configured settings that are in here, and one of the more important things that you're gonna see in here is when it comes to the databases. Now, obviously, you can make some tweaks to this and you can use whatever supported database engine that you would like, but we're gonna stick with SQL lite3 for this particular course. But you can make changes to that if you would like, and you could also change the Time_Zone and things like that if you would like as well, but for this particular course, I think we're gonna leave it just for now. Now the final thing that I wanna do in this lesson is I want to get our database set up so that we can go ahead and run our application just so you can see in the browser. And we can start to move on from there. So the way that we're gonna do that is we're gonna come back into our text editor, and being within our news-agg directory, so I have access to my manage.py file, I'm simply gonna type in python manage.py, and I want to migrate. And this is going to execute the migration and do all the setting up of the database, at least to get everything started for us. And if you'll notice, if you have a text editor like I do, you're gonna see that the db.sqlite3 file is gonna be created for us because we're using the sqlite3 database engine. So once I've done that, now I want to see my actual application up and running. So I can come up within my command prompt and I can simply say python manage.py and I want to runserver. And what run server's gonna do is it's gonna create a local instance of basically a very simple web server, and it's gonna be listening on 127.0.0.1 on port 8000. So if I were to open up a web browser and I were to go to that directory or to that address, you're gonna see that we have our Django application or our Django project up and running and being served with in the web browser, and as you can see here it worked, but we haven't actually done any work yet. But ultimately, that's gonna be the process that we're gonna follow throughout this entire course as we begin to build functionality into our site.

Back to the top