Setting Up Python
00:01 Welcome to the next lesson. What we’re going to do now is install Python, and then after that, we’re going to start integrating your Python install with Sublime Text so that you get automated feedback on your Python code directly inside Sublime Text 3.
00:15 We’re also going to install another plugin that will give us full code completion for Python code directly inside Sublime Text. Okay, let’s start by installing Python, because before we can move on, we need to make sure you have a functioning Python install on your system.
00:29 If you want to do a quick check if you already have Python installed on your system, you can do that by opening the command prompt.
00:37
And then inside the command prompt, you want to try and run the pip
command, which is the package installer for Python modules. It’s kind of a tool that allows you to install other dependencies inside your Python distribution. If you’re getting the same result that I’m getting here, where it says that pip
is not a valid command, then that means we need to install Python first before we can move on.
01:00 The best way to install Python on Windows is by opening a browser and then going to python.org. And here, you’ll want to find the download button. It should detect your operating system, and it should make a recommendation for the Python version you’ll want to install here. So for the purposes of this tutorial, I’m going to go with Python 3.
01:21 All of the plugins will also work with Python 2. So if you have a good reason to go with Python 2, then you might want to select that. If you’re unsure which version of Python you want to use, then I would recommend that you go with Python 3.
01:34 Let’s go ahead and click on the Python 3 installer, and this is going to download the installer from python.org.
01:47 And once that finished downloading, we’re just going to run the installer. And here, you want to make sure that you select this check box here, where it says Add Python x.x to PATH, because this is going to make sure that we can properly integrate your Python installation with Sublime Text later on for autocomplete and code linting.
02:07 All right, so let’s install Python now.
02:14 The installation was successful and we can close all of that, we won’t need it for now. And there’s one more thing I want to do now, and that is verifying that we installed Python correctly. So again, we’re going to open a command prompt.
02:28
And we can just pin the command prompt to the task bar as well, because we’re going to need that a couple more times in the future. Now what we’re going to try again is to run that pip
command we couldn’t run earlier, and now it should actually succeed.
02:42 You should get a bunch of output here. It might look slightly different than what I’m seeing here now, but it should not be an error message. And that confirms that we’ve got our installation of Python up and running and we can jump right in and set up Python code linting inside Sublime Text. Let’s set up Python code linting.
03:01 A code linter is a program that analyzes your Python source code and then gives you feedback on potential errors. And a code linter can detect stuff like syntax errors and then also structural problems in your program—for example, if you’re using an undefined variable. And it can also give you feedback on some best practice or code style recommendations. So it’s a really, really useful tool, and it’s a great productivity tool for writing Python. Because when you integrate code linting with your editor, like we’re going to do in a minute, this is going to give you immediate feedback on your code right as you type it. In some cases, this can really shorten the feedback loop that you usually have when you’re writing code, where it can just immediately call you out on a little mistake that you made so you don’t have to go and run the program to find out that something went wrong. Now, one of the best code linters for Python I’ve found is called Flake8.
03:52 It’s actually a combination of several other tools that check your code and also ensure that the code is formatted correctly. Flake8 is actually a command line tool that we need to install separately. And once we’ve got Flake8 installed, which is kind of the brains or the linter engine behind everything, then we’re going to go and install two plugins into Sublime Text that will integrate the Flake8 output directly with your code editor.
04:20
Let’s start by installing Flake8. Again, you’ll want to go to your terminal window and then just type this command, pip install flake8
, and hit Return.
04:31
And this will download and install Flake8. Okay, so once this completed, we can make sure Flake8 was actually installed correctly by running it with the --help
command line option.
04:43 And this should give you a bunch of output. The version numbers you see down here, they’re probably going to be different than the ones that I have here when I installed this, because Flake8 actually keeps getting better over time as the developers keep making more updates to the underlying code checkers.
05:01
And if you ever want to upgrade to the latest version of Flake8, you can do that through pip
as well. So, again, the command for that is pip install
, and then you want to pass the --upgrade
option and tell it which package to upgrade. And if you run that, this would make sure you’re on the latest version of Flake8.
05:21 So, in this case, it didn’t do anything because we just installed the latest version. All right, so we can close the terminal, and the next thing that we’re going to do is we’re going to install SublimeLinter, which is a plugin for Sublime Text that can then go out and run command line tools like Flake8 or other linters for you and integrate their feedback back into your code editing window.
05:46 So this is a really powerful concept because you can use SublimeLinter with a bunch of linter engines, like Flake8, and it will all be brought together through the one interface in SublimeLinter.
06:00 So for example, I do a lot of web development with Python, and that means I often also write JavaScript or some HTML. And with SublimeLinter, I can actually integrate a bunch of different linters for all these different formats—for example, a separate linter for JavaScript—and all of their feedback will show up in the same way, and it’s all kind of presented in a consistent way and I don’t have to deal with 10 different plugins.
06:25 I can just have SublimeLinter and then a bunch of linter engines and SublimeLinter will figure it out. Okay, great, I hope you’re sold on this concept. What we’re going to do now is install SublimeLinter.
06:36
Let’s open the Command Palette, find the Install Package command, search for SublimeLinter, this one here, Interactive code linting framework. And then hit Return, and this will install SublimeLinter. Now once that’s complete, it will pop up its little README
message here.
06:55 And there’s actually one more thing we need to install. So again, we’re going to go through the Install Package command, and we’re going to search for sublimelinter-flake8.
07:05 This is the piece that integrates SublimeLinter and Flake8. It’s kind of an adapter between the two, so we need that for every linter engine that we install and that we want to integrate with SublimeLinter. So let’s go ahead and install that.
07:20 And once the installation is complete, we need to quit and restart our Sublime Text. So generally, if you install a new plugin, it just makes sense to quit and restart Sublime Text, because otherwise that can lead to subtle and sometimes not-so-subtle bugs.
07:33 So usually I just quit and quickly restart my Sublime Text. It starts up quickly anyways, so—not a big deal. Okay! So now I want to show you how this newly installed Flake8 integration works. We’re going to close all of this here.
07:48 What we’re going to do here is start an example Python file, and we can do that by simply setting the syntax to Python. And you could also do that through the Command Palette.
07:58 So when you search for python, it should show up here as the Set Syntax: Python command. The Command Palette is really, really helpful for all of these little shortcuts that, you know, you don’t really remember the keyboard shortcut for but you still occasionally want to access that functionality through the keyboard. Okay, so I’m just going to type in some example Python code. Already as I’m typing in the code, I’m starting to get this feedback on my code with these red and yellow highlights and these little icons here on the side.
08:30
This is feedback that’s coming from Flake8, and if you want to see what it’s complaining about, then you can just place your cursor inside one of these boxes—either with the mouse or, of course, with the keyboard—and then look down here in the status bar area, where it will tell you what’s going wrong. So in this case, it tells me that os
is imported but unused, which is correct—I’m importing this module and not using it. It’s the same with the sys
module here. And then down here, it’s complaining about some formatting issues with our code where we’re not following the PEP 8 standard.
09:02 And PEP 8 is a very common and popular code formatting standard in the Python community. It’s complaining that we’re not having enough newlines between the rest of the code and where we’re defining this function, and then here we’re using tab characters instead of space characters.
09:18
So here, it says undefined name ‘primt’ because I have a typo here with the print()
function, and then I could fix that and the error would disappear.
09:27
And also down here, of course, it’s complaining that we’re calling this function, hello()
that doesn’t exist because it’s actually called hello_world()
.
09:35 Now, this feedback on my code is really useful, but one problem that I have with this is, number one, that it’s super visible so I get this kind of weird, fruit salad effect here with all these boxes.
09:47 And that’s a little bit too much for my taste, so we’re going to change that in a minute. And then the other problem is that as I’m typing out the code, it’s already starting to give me feedback, right?
09:58 So I don’t really have a chance to even finish a whole function before it starts hitting me with this feedback. And maybe I’m still in the middle of writing my code and I don’t really want this feedback, so we’re going to change these two things to make the SublimeLinter integration a lot more useful and not as distracting. To do that, you want to go to Preferences > Package Settings > SublimeLinter, and then click on the Settings - User for that.
10:25 Now, this will be an empty file, and there’s a little tip here because every time you’re faced with that situation, you’re curious what kind of settings are available, you can just go to the default settings for that package. So in this case, I’m going to go to the SublimeLinter > Default Settings,
10:43 and what I’m going to do here is I’m just going to copy all of those over. In this case, we need to make a little tweak and rename this key too. And then I kind of know what options I have available here for SublimeLinter.
10:59
And the first thing that we’re going to change is this "mark_style"
setting here. We’re going to change this from "outline"
to "squiggly underline"
,
11:12 and we’re going to save these settings. And then as soon as we go back to our code here, you can already see that the error style has changed and the warning style has changed. It’s now a lot more subtle but still visible enough and it just looks a lot more calm, and I like this style a lot better.
11:29 The other problem that we wanted to fix was that the linter is giving us feedback as we’re still typing out the code, and we’re not even finished typing out our code.
11:40
And you can modify this behavior by finding the "lint_mode"
setting, and right now, this is set to "background"
, which is a mode where it’s constantly checking your code in the background.
11:49
We’re going to change that to "load/save"
. And once we save the settings, then this will tell SublimeLinter to only run Flake8 when we save our file or when we load a new file.
12:02
So you can see here, as soon as I make a modification, my code feedback disappears because now I’m still working on the code. And then as soon as I save the file… In this case, I’m saving the file for the very first time, so we’re just going to save that as example.py
. And now as soon as I save this file, the feedback returns.
12:21 And as soon as I make a modification again, it disappears, but as soon as I hit Control + S again to save the file, the code linter runs and then I get feedback on my code. And to me, that’s the best way to get feedback on my code because as I’m typing out my code, I don’t want to get distracted by this feedback. But as soon as I save the file, that’s like saying, “Hey, this is a snapshot, I completed a new piece of functionality here, please check my code now,” and then I can go back and make sure that all of these things get cleaned up.
12:50 So now we’ve got automated feedback on our code as we write it with the Flake8 linter and the SublimeLinter package. And now we’re going to install a Sublime Text package that will give us code completion for our Python code right inside Sublime.
13:03 And it’s also going to add a couple more interesting features, for example, inline display for code documentation. So as I’m typing out a statement or a function call, we’re going to get a little popup that shows us the documentation for this function directly. This is really helpful and it’s almost turned Sublime Text into a Python IDE. So again, a very powerful plugin, and we’re going to install it through the Install Package functionality in the Command Palette.
13:33 This code completion is going to be powered by the Anaconda package, and there’s a bit of overloading in that name here. What I’m referring to is a Sublime Text package, or a Sublime Text plugin, called Anaconda.
13:49 And there’s also a Python distribution called Anaconda, so this could be really confusing to get these two names mixed up. In this case, I’m referring to the Anaconda plugin, and we’re going to go ahead and install that now. Okay, so this completed the install, and of course we need to quit and restart Sublime Text again because we just installed a new package.
14:13 I want to talk a little bit more about Anaconda. Anaconda is really awesome, and it’s a really full-featured solution for Python code completion inside Sublime Text, and has a ton more features. So, for example, it can also do things like run a code linter on your code as well.
14:31 Personally, I mainly use Anaconda for its autocompletion and the documentation or inline documentation functionality, which are really, really useful. But I actually like SublimeLinter better for code linting because that way I can integrate several linters for other languages besides Python under the same interface.
14:51 In this guide, I’m going to be a bit prescriptive and show you the exact setup that I use personally, but of course, if you find that you like Anaconda’s integrated linter better than SublimeLinter, then of course you could just switch that out and leave that setting enabled. Really, the whole idea of using an editor like Sublime Text is that you get to set everything up the way you want to and you can gradually build this into a setup that is exactly right for you.
15:18 So let’s go ahead and configure Anaconda. Again, we want to go to the Package Settings > Anaconda, and then the user settings here. And there are two changes we want to make here,
15:31
and that is changing the "anaconda_linting"
setting. I’m just going to set that to false
, which is going to disable Anaconda’s built-in linter.
15:41
And we’re also going to add this "pep8"
setting, which is going to disable another part of Anaconda’s built-in linter, namely the one that gives us code style feedback, and we already have that too with SublimeLinter.
15:54 I’m going to go ahead and save those settings here. And then we can close these open files here. And let’s actually reopen our example Python file from earlier. Okay, so you can see here we’re still getting the linter feedback.
16:09
Now I want to show you a couple of cool features that Anaconda added. Let’s start by typing out the name of a built-in function. Let’s say I want to use the abs()
function inside Python. As I start typing out a
and then ab
, you can already see that I’m getting this popup here that allows me to select a number of autocompletions that are already being generated by Anaconda.
16:31
So, let’s say we want to go ahead and pick the abs()
function, and what you’ll see now is that we’re getting this little inline popup or overlay that shows us the docstring for the abs()
function.
16:43
And so now we know that this returns the absolute value of the argument, right? In this case, we could just call this with an example argument here. And the same thing also works for imports. So for example, if we want to import the collections
module here or a specific function from the collections
module, then again here I’m getting the autocomplete for the collections
module and I could go ahead and say, “All right, so I want the namedtuple()
function from here.” And as I autocomplete this, again, I’m getting this little popup here that shows me the docstring for the namedtuple()
function and I can just kind of look that up without having to Google for it or, you know, having to go get a book or use some other tool.
17:29
And it’s directly integrated into my code window. And this is just awesome. Like, this is really, really helpful. Okay, so now we set that up and, for example, now we could go namedtuple
.
17:41 And then again, as I would type this out, I would get the code completion and the docstring here. Or as I’m inside the parentheses here, typing out the parameters, I would again get this docstring here. So this is a really, really handy functionality. Now, if you’re wondering how to trigger the autocomplete manually, because usually it just pops up when you start typing stuff, but you can also cancel that by hitting Escape, and then the autocomplete just kind of goes away. And you can bring it back by hitting Control + Space, and that will pop up the autocomplete again and show you this docstring overlay as well. Now to show you how powerful the Anaconda autocomplete is, I’m going to define a little list object here.
18:25
And then as soon as I go and put a dot (.
) behind that, Anaconda actually figured out that this is a list constant, and it shows me functions that I can call on this list.
18:36
So in this case, I want to call the .count()
function. And then again, this shows me a really nice representation of the docstring, and I know that this way I can count the number of occurrences for a specific value, so let’s just try and do that. All right, great.
18:50 So now you’ve seen how powerful the autocompletion in Anaconda is, and there’s actually two more really cool features in Anaconda that I want to show you as well.
19:00 So, have you ever wondered how a function is implemented in Python?
19:06 I certainly have, and especially when you’re working with a big codebase, it sometimes really helps to just jump to the definition of a function so you can read the actual code and understand how it was implemented.
19:17 But, of course, this often takes a bit of search and going back and forth. And now with Anaconda, you can very often just right-click on a name here and then click on Goto Definition.
19:30
And in this case, because this is a built-in function or a function that’s part of the standard library in Python, it actually opens up my library path here and bounces me directly to the definition of the namedtuple()
function, which is super useful because then I can actually dive into the code and see what it does. So, this works pretty well.
19:52 It’s not perfect, in all honesty, and to a large degree that’s due to Python’s dynamic nature where it’s sometimes a little bit hard to figure out what function you’re actually calling or where a name is actually defined, but it works in 90% of the cases. So it’s a super helpful feature, it’s going to work with your own codebase as well, and it can easily save you a lot of time. Now, another feature that I want to show you here has to do with some of the PEP 8 formatting complaints we got earlier, right?
20:19 So let’s just format this stuff here a bit differently. And then as I run the linter again, it’s going to call me out on a bunch of things. So for example, it’s expecting two blank lines before a function definition, and it complains that we’re using tabs instead of spaces here and that we’re not ending the file with a newline.
20:41 And now, all of these things are kind of mechanical. You know, they’re just having to do with the code style. And in Anaconda, there’s actually a really cool feature where you can just right-click anywhere in the code and then go to Anaconda and click on Autoformat PEP 8 Errors.
20:58 And this will, to its best ability, format your Python code or reformat your Python code, and it actually got rid of most of these formatting complaints, right?
21:08 So it changed this to spaces here, it inserted the missing blank lines, and also gave us this final newline here that was missing. So this feature, again, is not perfect.
21:19 In some cases, it will be ambiguous and it doesn’t really know how to format your code or how to fix your formatting so that it’s PEP 8 compliant. But again, you know, in 80% of the cases, this will do most of the work you need to do.
21:32 And this is super helpful if you’re working with a legacy codebase and you just want to clean up a file, or maybe you want to clean up a file before you commit it to a repository and just to make sure it’s formatted nice and cleanly. So again, this can save you a ton of work, and therefore Anaconda is a really, really awesome plugin. So, just to recap what we did in this lesson, we installed Python and then we started integrating it into our Sublime Text setup.
21:57 First, we set up code linting using the SublimeLinter plugin and the Flake8 code linter. And then after that, we installed the Anaconda plugin that gave us a bunch more really awesome features like autocompletion for our Python code and inline documentation display, and then also some autoformatting capabilities.
22:16 All right! So now this is already shaping up to a really, really awesome integration. All right, I hope you enjoyed this lesson. We’ve already improved our Sublime Text setup quite a bit, and when you’re ready to move on, then jump right in into the next lesson where I’ll teach you how to make the setup even better.
Kimberly Cornier on April 26, 2020
User settings are not what is showing in the video - I did a search on the options to be able to apply the changes for linter
Charlie Clemmer on May 16, 2020
The Anaconda goto definition function is not working with either my macOS or Windows installs of Sublime and Anaconda. In both cases, Anaconda opens a new tab, but the tab is named None and opens as a blank file.
konstantinsm on May 26, 2020
Hi, thank you for the course. SublimeLinter settings provided in bonus content section need to be updated. For instance, there is no need to place them under ‘user’ key anymore.
eawongtheprogrammer on Oct. 16, 2020
Anaconda autocompletes does not work anymore and so is sublimelinter and flake 8.
eawongtheprogrammer on Oct. 16, 2020
For those who are struggling to make anaconda auto complete working on sublime text version 3.2.2, build 3211 on Python version 3.9 the following has worked for me:
locate your Anaconda.sublime-settings (<user>\AppData\Roaming\Sublime Text 3\Packages\Anaconda)
set the interpreter to (take note of double slash) the absolute path of python.exe
“python_interpreter”: “C:\Users\<user>\AppData\Local\Programs\Python\Python39\python.exe”,
pyguy on Feb. 16, 2021
Hi Dan,
This video needs to be updated. Flake8 integration doesn’t seem to require a separate package install anymore?
Also user settings are different to how you run through it in the video.
useeme2ndtime on April 4, 2021
Sublimelinter v4.16.2 after changing “mark_style”: “Outline” on “mark_style”: “squiggly_underline” it still using “outline” for tabs as an indentation. For remain ones works fine.
You must own this product to join the conversation.
Chris S Bullock on Jan. 16, 2020
Question why install python3 as a 32 bit systems why not do 64 bit as the Video did 32 bit. I know back in the day it was said to install 32 bit version since packages back then were a bit behind the curve but just wondering on this point