In this video, we’ll start setting up our PythonAnywhere environment to make it ready for our app. We’ll get a virtual python environment set up, and get our app code into PythonAnywhere.
[MUSIC] 0:00 Hi I'm Kenneth and I'm a Django teacher for Treehouse. 0:04 Building applications in Django isn't the most difficult thing in the world and 0:07 you've probably built a couple by now. 0:11 Either with Tree House or on your own. 0:13 Once you build your project though, you'll want the world to see it. 0:15 Years ago, deploying Python and Django sites was, well, it wasn't fun. 0:19 I remember keeping a very long text file of all the steps I needed to do to get 0:23 updates deployed on a site I built on a very early version of Django. 0:27 Thankfully, services like PythonAnywhere exist now that make this a lot simpler. 0:31 Unlike some platform as a services, 0:37 PythonAnywhere gives you live access to a server through a web browser. 0:39 It's actually pretty similar to how workspaces works here at Treehouse. 0:43 PythonAnywhere is a bit more involved than some platforms like Heroku, but 0:46 it's a better service if you want to know more about the underlying 0:50 technology of your site. 0:52 Stick around and 0:54 I'll show you how to put your Django site live on the Internet at PythonAnywhere. 0:55 So, I'm gonna start with a quick overview of the PythonAnywhere dashboard. 1:00 So, in your web browser, go to pythonanywhere.com and go ahead and 1:03 log in, 1:07 if you're not logged in already or sign up if you haven't created an account yet. 1:08 You should land immediately on this Consoles tab. 1:11 Now, I wanna remind you that I have no control overall PythonAnywhere looks like, 1:15 so this may very well look different by the time that you come here. 1:20 But as of the time of this recording, 1:24 this is what PythonAnywhere looks and works like. 1:25 So, Consoles is where you can create new consoles or 1:28 shells in the PythonAnywhere environment. 1:31 So, you can go into Bash, you can go into MySQL, you can go into different 1:34 versions of Python, you can even create custom consoles. 1:37 We'll be using this tab and 1:41 the PythonAnywhere Consoles very heavily during this workshop. 1:42 If I click on the Files tab, I'm given this web based file manager. 1:46 Now, I'm not gonna be using this much, 1:51 since most of the file management is going to happen either 1:53 in one of the PythonAnywhere consoles or in the console on my own computer. 1:55 But this is a place where you can go and you can edit your files, 2:01 move them between folders, create and delete folders, all of that kind of stuff, 2:04 to control your files and everything that lives in your PythonAnywhere world. 2:08 If I move over here to the Web tab, 2:15 this is a mostly empty screen, there's a button for creating a new web app. 2:17 I'll be coming back to this when I'm ready to deploy my Django project. 2:22 If I click the Schedule tab, I can see PythonAnywhere's web based task scheduler. 2:26 Now the project that I'm gonna deploy doesn't need scheduled tasks but 2:31 depending on what you end up building and putting on PythonAnywhere, 2:36 you may find yourself needing to run a task on a repeating basis, right? 2:39 So, you need to send out a new email to people every hour, 2:43 or you need to run some sort of cleanup script every ten minutes or whatever. 2:48 This is where you would do that. 2:52 The last tab is the Databases tab, and here you can see settings for 2:54 a default MySQL database, and that's what we're gonna be connecting our app to, 2:58 in the last video of this workshop. 3:03 For now though, I'm gonna jump all the way back over here to the Consoles tab and 3:05 I'm gonna start getting PythonAnywhere set up for my Django project. 3:09 So, I don't wanna launch a Python, iPython or PyPy shell at the moment. 3:13 I need to launch a Bash shell. 3:18 So, I'm gonna do that by right clicking where it says, Bash, and 3:21 I'm gonna select Open Link in New Tab. 3:24 And this gives me the shell. 3:27 And this should look pretty similar for 3:30 those of you who have used workspaces before. 3:31 This is very similar to the little console thing down at the bottom 3:34 of your workspace. 3:37 So, this is a Bash shell, just like you would get in Mac OS or Linux. 3:39 And this is where I'm gonna set up the environment to hold my app. 3:43 Now I want to have an isolated virtual environment for the app to run in, so 3:46 that way I can control exactly what packages are being used for this app. 3:51 So I'm gonna start by cloning my GitHub repo into my PythonAnywhere environment. 3:55 Now I'm using a repo that's made for this workshop, you can, of course, 4:13 use whatever repo your project lives in. 4:17 So, I'll have to identify with GitHub. 4:20 So I need to identify with GitHub, and then the cloning will actually happen. 4:30 All right, so, now I have all the files on the PythonAnywhere server. 4:33 So, now I'm gonna go into my project folder, which is called deploying_django, 4:39 and I'm gonna create a virtual environment for my project. 4:43 I'm gonna do this by using the mkvirtualenv, or 4:47 make virtualenv command. 4:51 And I wanna tell it which Python I want, which is Python 3.5. 4:54 And then the name for the virtual environment which is a venv. 4:58 This is a little different from how you've done virtual environments before on your 5:02 machine, where you've done like the python -mvenv command or 5:05 python 3 -mvenv. 5:10 This is using the virtualenv wrapper library, 5:13 which consolidates your virtual environments into one central location. 5:17 Which in this case, you can see here, it's in home/kennethtreehouse, 5:22 which is my PythonAnywhere username.virtualenvs, 5:25 this is where all of them live, instead of living in the project directory. 5:29 This installation takes a little bit of time, so I'm gonna just let this run. 5:34 And then when it's done, now I'm gonna check to make sure that I'm using 5:38 the Python that I just told it to use. 5:41 I can see that the virtual environment has been activated but 5:44 I just wanna double check. 5:47 So, if I do which python, I see that it's this one that was up here. 5:49 And if I was to do a python --version, then I see that it's 3.5, 5:53 which is the Python I told it to use. 5:58 So, now everything is set up and it's ready for 6:00 me to start getting the project actually live and deployed on PythonAnywhere. 6:03
You need to sign up for Treehouse in order to download course files.Sign up