Setting Up Python
00:01 Next up, we’re going to start setting up Python, which means we’re going to install everything we need to get our Python environment up and running. And then we’re going to start integrating Python into Sublime Text with automated code linting.
00:15 This is going to help you identify formatting and other issues in your Python code automatically as you write it. And then after that, we’re going to also set up autocompletion for Python using the Anaconda plugin for Sublime Text.
00:30 And the first step we need to take here is to make sure our Python install is set up correctly. What you want to do is you want to open a terminal window. Now because I’m on Ubuntu 16, it includes Python 2 and Python 3.
00:49 We can check for that by typing in the command for the Python 3 interpreter, and this is going to boot right up and it’s going to tell us what version of Python 3 we’re on.
00:59
You can exit that by hitting Control + D, by the way. Now we have Python 3 installed, but we need one more thing and that is the pip
package manager.
01:08
pip
is a package manager to allows us to install additional dependencies and extra Python modules automatically so we don’t have to do it in a manual, copy-and-paste-y way. That would be really hard to maintain, so pip
is kind of like Package Control for Sublime Text.
01:26
And now, when I try to run pip
—or in this case pip3
because that will be the version of pip
that would install new modules into our Python 3 install—then I can see that it’s not actually installed by default.
01:41
In your system this might be different, you might already have this installed. But if you don’t, then we just need to install it really quickly. So what I’m going to do here is I’m going to type this command directly into the terminal that will install the python3-pip
package manager.
02:00
I’m going to hit Return and it’s going to ask me for my account password. All right, and I’m going to have to hit Return again, to continue to install. And now this is going to install the latest version of pip
for Python 3.
02:13
This is just going to take a second here. Okay, so once it’s completed, I just want to make sure that pip
was installed correctly, so I’m just going to run this pip3
command and then this should give you a help screen with some of the options that pip3
has available. And yeah, once you get this output and you don’t get an error message, then that means your Python setup is working correctly. So to recap, you want to be able to run python3
and it should take you to a Python 3 shell, and you can exit that by hitting Control + D. And also if you run pip3
, this should also run the Python 3 version of pip
.
02:52 And once you’ve got that, then we’re all set and we can jump right in into setting up Python code linting. Let’s set up Python code linting. A code linter is a program that analyzes your source code for potential errors.
03:04 It can detect errors like syntax errors or structural problems like using undefined variables and other best practice or code style violations in your code. And to me, using a code linter is just a super-great productivity tool for writing Python. Because especially when you integrate a linter directly into your Python environment, it gives you that immediate feedback on the code right when you type it.
03:31 And so for some classes of errors or bugs, using linting can just cut down this feedback loop of, you know, you write your code, you run that code, oh, you catch an error, you go back to fix it. With a linter, you can often—not always—but often you can cut that down to write code, you’re going to immediately see that little error message, and then you can fix that error and you don’t have to go through that extra oh, jump to the terminal, run the code, and go back steps. And this might not seem like much, but in the course of a day, this stuff really adds up.
04:02 So they’re a fantastic productivity tool. One of my favorite tools for Python code linting is called Flake8. I’m going to show you now how we can integrate Flake8 feedback into Sublime Text directly as you’re writing the code.
04:20 Flake8 is one of the best linters I’ve found. It’s fast, it as a very low rate of false positives, so it doesn’t really get into your way, and it’s a combination of two tools.
04:30 It’s the pyflakes code checker and then the pycodestyle checker, and the combination of both makes it pretty easy to get valuable feedback on your Python code as you’re writing it.
04:46 Flake8 is a command line tool that we need to install separately, and then we’re going to install SublimeLinter and another plugin that integrates SublimeLinter and Flake8 with Sublime Text.
05:01 Okay, that was a bit of a mouthful, but let’s jump right in. The first thing we need to do is install the Flake8 linter—
05:10
or it’s almost like a linter engine—that we’re then going to integrate with Sublime Text. So in your terminal window you want to type in pip3 install flake8
05:24 and this is going to install the Flake8 Python package.
05:29 And now with this, you want to make sure that this actually went through successfully, right? And where this has been installed is under this directory here,
05:46
so if you type out this command, ~/.local/bin/flake8 --help
and run that, you should get some output from Flake8 and it should tell you what other dependencies it’s running on.
06:00
It’s going to probably say some other version numbers here in your copy when you’re doing this, because they’re constantly upgrading these packages and making them better, but you should be able to run this flake8
command and it should actually give you some output. So once we’re at this point, then we can move on and we can install the integration with Sublime Text.
06:27
Now, there’s one more tip, so if you ever want to upgrade the version of Flake8, you can do this by doing pip3 install
and then a --upgrade flake8
, and that would bounce you to the latest version of Flake8 if you ever want to upgrade, which makes sense! You know, it makes sense to upgrade occasionally because they introduce new checks and new feedback, so it definitely helps to do that. Okay, so now that we have Flake8 installed—Flake8 the command line tool—we’re going to install SublimeLinter, which is a code linting framework for Sublime Text.
07:01 It can integrate a number of linter engines, like Flake8, directly into Sublime Text. But the cool thing is not only can you use this to lint Python code, but if you’re doing web development, maybe you’re having some JavaScript code or, you know, other languages that you’re using with Sublime Text, then SublimeLinter is kind of this meta package or framework that allows you to run a bunch of linting tools on your code in the same way and it’s all going to be nicely integrated. So this is a really, really great tool. And the way we’re going to install this is by opening the Command Palette.
07:40 And then we’re going to launch the Install Package functionality. All right, and one that pops up, we’re going to search for SublimeLinter. And this is the one we want, we just want the plain SublimeLinter and these other ones—they’re all plugins on top of SublimeLinter.
07:58 So I’m just going to click on that guy and then down here you can see it’s installing SublimeLinter. And after the install completes,
08:07
it’s actually going to pop up this little, README
message here. Okay, we can leave that open and then open the Command Palette again. Again, go through the Install Package step, and we’re going to install one more package,
08:23 and that is called SublimeLinter-flake8, this guy here. And again, we’re going to install this and wait for the installation to complete. Okay. And then we can close of all of these messages.
08:38 Now, there’s one final step we need to take to set up code linting for Python files using SublimeLinter and that is configuring SublimeLinter-flake8 so it knows where to find the Flake8 linter.
08:48 You want to go to Preferences > Package Settings, and then find SublimeLinter. And then we’re going to open the user settings,
08:59 and these will be empty for now, right? So what we’re going to do—and this is actually something you can do for other packages as well—
09:09 I’m going to open the the default settings for SublimeLinter as well, and then that’s going to show me what options are actually available for this particular package. And I’m going to bump up the font size here a bit.
09:22 So in this case, what we’re going to do, we’re actually going to copy and paste all of that. We’re going to copy that and and then insert it over here. Okay, so once I’ve copied them here, pasted them here, we’re going to make two changes here.
09:36
We’re going to change this "default"
key to "user"
, and then we’re going to find this other setting under "user"
, "paths"
, and then find the "linux"
setting here.
09:49
And now we’re going to type in the path where we had installed Flake8 earlier. So this is going to be "~/.local/bin/"
and now this is going to point SublimeLinter to our Flake8 binary.
10:10 What you want to do here is just save the file by hitting Control + S or you can, of course, also click through the File menu and hit Save.
10:18 Then close all of these. And now what we need to do is actually restart Sublime Text 3. So again, you’re going to go to File > Quit, just quit Sublime Text and then we’re going to start it again. And now at this point, we can try out the linting integration.
10:35 The way we can test this out is by writing some Python code. I’m just going to make something here on the spot. And then there’s one thing I need to change here, and that is setting the syntax to Python.
10:47 I’m going to open the Command Palette, just type in python and it should already pop up the Set Syntax: Python. All right! And now we’re getting the syntax highlighting.
10:55 What should also happen a short time later is that we’re getting some of these yellow messages here popping up. In this case, this is feedback from Flake8.
11:06 And I can trigger some more of that by actually, for example, importing a bunch of modules that we don’t need.
11:17
And then I’m also going to introduce an error here. I’m just going to call the function hello()
that doesn’t really exist. And now I’m getting all of these areas highlighted here already, and these are linter at warnings from Flake8.
11:32 There’s two ways to see that there’s an error, right? You’re getting these little dots here on the left side, and then also it’s going to highlight that piece of code that caused the error inline here with the code.
11:46 And actually, every time you click on one of those or place the cursor here—it also works with the keyboard—you can see the error message there in the status bar area.
11:56 All right, so when I click on this problem here, it tells me, okay, 1 of 7 errors. ‘os’ imported but unused. And then here it would tell me, okay, it’s expecting two blank lines for formatting and that also I’m using an undefined name ‘hello’.
12:10
And so this is already pretty useful. I can disable these checks individually by putting a comment that just says # noqa
, and that’s going to disable this particular error for Flake8.
12:23 It’s basically going to ignore errors on that line.
12:27 So, you know, you only ever want to use that when you’re exactly sure that you want to ignore an error. Just to clean that up a bit. But actually what I wanted to show you here is that now we’re getting these errors indicated. However, personally, for me, this is a little bit too verbose, right? So for example, one thing that I don’t like is that as I’m typing this out, as I’m typing out the code, it’s starting to already highlight all of this stuff.
12:55 So you kind of get this fruit salad crazy colors effect that I’m not too fond of, so what I usually do is I go into my Package Settings and you want to find those SublimeLinter settings again.
13:10
You want to go to the user settings. And then here in the SublimeLinter user settings, you want to find that "mark_style"
setting. And right now, this is set to "outline"
, and we’re going to change this to a different "mark_style"
.
13:23
The "mark_style"
determines how these errors are indicated, and we’re going to going to set that to "squiggly underline"
and save that. And now when you go back, you’re getting this different highlight style, which I just find a lot calmer and it actually makes it a little bit easier to read the rest of the code.
13:44
But there’s actually one more setting that I like to change here in the SublimeLinter settings, and that is the "lint_mode"
. Right now this is set to "background"
, and "background"
basically means every time you make a change, the linter’s going to run and it’s going to give you feedback on your code. And this can be what you want—I mean, this can be desirable, right? Like maybe that’s how you like to work with your code and you actually appreciate getting that feedback instantaneously. However, what frustrates me is that every time I create a new line, this is already starting to flag things, right?
14:19
So I usually go and find the "lint_mode"
setting here and then set that to "load/save"
. You want to save that file again. And now every time you modify the file, nothing really happens in terms of linting.
14:38 The linter only runs when I actually save that file. Every time I save it, it’s going to run the linter. So now after I save the file, it’s going to highlight all of these errors and then, you know, when I make a change—like, let’s say we’re going to fix this problem here—then actually all of the linter feedback disappears.
14:59 But now when I hit Control + S again to save this, then all of the linter feedback comes back. And this is kind of the way I like to work with my linter integration because it gives me some time to actually write some code without getting distracted by all of this feedback, and then save the file, and then get the linter feedback so that I’m able to go jump in and fix some of the problems if there are any. All right!
15:22 At this point, we have a pretty sweet linter integration with Sublime Text already. The next thing we’re going to do is we’re going to set up Python code completion as well. So next up, we’re going to set up Python code completion.
15:37 Many Python developers using Sublime Text are wondering for the best way to do that and it’s among the most requested features. And for a really long time, Sublime Text was actually really lacking in this respect and it was really hard to get good autocomplete for Python code.
15:55 But this all changed when the Anaconda plugin came out. There’s actually two things, two pieces of software in the Python space that are called Anaconda.
16:07 There is the Anaconda package manager. I think it’s mostly used on Windows and for scientific computations—kind of an all-included package.
16:19 That is not what I’m referring to. And, you know, I understand this can be a little bit confusing. What I’m referring to is a plugin for Sublime Text that’s also called Anaconda, and it has a bunch of features like autocompletion for Python code.
16:34 It can display inline code documentation, and it can do a bunch of validations on your code, and it can also autoformat your code. You can jump to Python code definitions, and it has a bunch of really, really cool features.
16:52 But it is not to be confused with the Anaconda package manager. Personally, I mainly use Anaconda for its autocompletion and inline documentation features, but I still like SublimeLinter Linter a lot better for doing the actual code linting because I can use the same plugin, SublimeLinter, to not only lint my Python code, but I can also use it to lint my JavaScript code or my HTML and all kinds of other things. So that’s why I like to keep these two concerns separate.
17:27 And I also felt that SublimeLinter was a little bit better on performance. I’m going to show you the setup that I’m using personally. I’m very happy with that. But of course, you could also change and modify these settings, so take them as a starting point and then gradually built the setup that is just right for you. Okay. So again, the first thing we need to do is we need to install the Anaconda package through the Command Palette in the Install Package feature.
17:55 And I’m just going to search for anaconda here. And it’s this slightly odd-looking package that’s hosted on damnwidget.github.io. You just want to click that and it’s going to take a second here to install.
18:12 All right, and it’s going to pop up this little message here. And after that, we can actually close all of these and then also quit Sublime Text because we need to restart it. And this is a general thing you want to do every time you install a plugin, even if it doesn’t explicitly ask you for it.
18:35 It just generally helps to restart Sublime Text at this point because, you know, it can cause all kinds of subtle and not-so-subtle bugs so it’s best to just avoid all of that by remembering to restart Sublime Text every time you install or uninstall a plugin. All right!
18:50 Now we need to finalize the Anaconda install and make some tweaks to its settings. So again, what you want to do here is you want to go to Package Settings > Anaconda,
19:03 and then open up your user settings for Anaconda. The first change we need to make here is to tell Anaconda which Python interpreter to use. We can find out where our Python interpreter lives by opening a terminal window. And if you’re on Ubuntu, you can just use the same path that I’m using, but if you want to look this up on another system, this is helpful.
19:26
So basically, you’re running this which
command, and this is going to tell you the absolute path where your python3
executable is. So I just copied that over, and then back in your Anaconda user settings, I’m going to add this new setting here.
19:46 I just paste that option in and then save that file. This is going to apply the updated settings. But leave that window open because we’re going to make one more change.
19:57 So, like I said, I like to use SublimeLinter for all of my linting needs and I like to keep that separate from the autocomplete stuff that Anaconda adds. Now, Anaconda has its own linting built-in, but I usually turn that off and rely on SublimeLinter for that, so I’m going to show you how to do that. Because if you don’t turn it off, you’re going to get duplicate messages and they’re going to differ slightly and so it helps to turn one of them off, because otherwise you’re just going to get bombarded with these messages.
20:25 So, I like to make these two changes, and that is disabling linting in Anaconda
20:32
and then also disabling the code style checks in Anaconda as well. So, yeah. You want to make sure all of these lines are terminated with a comma (,
), except for the last one because this is just a JSON format in the Sublime Text settings.
20:49 And now we’re going to save this file, and we’re basically all set.
20:55
And so at this point, we can try out some of Anaconda’s features here. I’m just going to open a new file and then actually set this syntax to Python. So to show you some of the cool things you can do with Anaconda, I’m going to start typing out an import
statement.
21:11
Already, it’s popping up with some of the modules that Anaconda knows about. Let’s say we want to import the time
module. And as you type out the name of the module, you’re getting this inline docstring displayed here.
21:23
This is getting pulled directly from the Python documentation so we know we’ve got the right module. This would also work for, let’s say, the os
module. So we’re going to import that. And then let’s say we want to call a function on this time
module. So again, as I’m typing out time
, I’m already getting this popup here that says, “Hey, do you maybe want the time
module?” And I can accept that completion by hitting Return.
21:49
And again, I get the docstring for the time
module. Now if I go time.
this is going to show me all of the public names in this module that I could call. So for example,
22:06
if we want to call the time.time()
function, again, as I type this out it’s going to narrow down the autocompletion here. It shows me the docstring for this module, it tells me that this will return the current time in seconds, right? So, I can accept that. All right, so this is exactly what we want. We can call this time()
function.
22:27 And now maybe if you’re wondering when the code completion gets triggered, you can close the code completion by hitting the Escape key, and that will turn it off. Now you might be wondering, “Okay, how do I get it back?” And on Linux, the key for autocompletion in Sublime Text, the combination is Alt + / (slash).
22:48 On other platforms it’s usually Control + Space and you can remap that key, but in Linux by default it’s Alt + /, so this is how you can trigger this autocompletion. And actually, I want to show you another cool feature. So this autocompletion—it’s not perfect to be honest, but it’s really, really good and it’s actually quite impressive.
23:07
Let’s say I’m defining this little list here as a constant, and as I go and hit the dot (.
), Anaconda knows that this is a list constant, and I can call all kinds of functions on that list.
23:23 I mean, it wouldn’t really make sense to reverse it in-place but, you know, let’s say you want to pop of an element or something like that. Anaconda is smart enough to actually detect these things, and that is pretty impressive.
23:35 This autocompletion is going to work for your own code as well, and also for third-party modules and built-in modules for Python. There’s one more super useful feature in Anaconda, and that is the ability to jump to the definition or implementation of a Python symbol just by right-clicking on it and selecting the Goto Definition feature.
24:00
I want to demonstrate that to you. So, there’s one caveat. This only works for symbols that are actually defined inside Python code, right? So for example, the time
library—that’s implemented in C code, so that wouldn’t work.
24:16
But I’m just going to import something from the collections
module, and let’s say we want the namedtuple
object here. And then… okay, I’m just going to type out this namedtuple
name here from the collections
module.
24:31 And I’m going to go Anaconda > Goto Definition, and this is going to jump me directly to the definition in the Python source code. In this case, it could be really interesting to actually find out and understand how some of the internals are defined here, but you can imagine that this is super useful if you’re working with a bigger codebase and you would just be able to right-click on a symbol here and then actually jump to its definition to understand how it was implemented or where it is exactly defined.
25:01 So, this works. Very often it works correctly—you know, the way I showed you. Sometimes it can’t really look up a symbol and it doesn’t really know where a symbol comes from because of the dynamic nature of Python. But in general, this is a super helpful feature, even though it might not be perfect.
25:19 Play around with it for a bit and familiarize yourself with it so you can get a good feel for how it works. It’s a really great tool for understanding a new codebase for example.
25:29 There’s one more really, really handy feature in Anaconda that I want to show you, and that is the PEP 8 autoformatting. What it does is it takes some of your existing code and it can just autoformat it in the PEP 8 style, according to the commonly used PEP 8 Python style guide. So in this case, you know, I’ve introduced a couple of weird formatting quirks here with extra spaces and stuff that are already getting picked up by the linter, and now what I can do with this is I can just right-click anywhere here, go to Anaconda > Autoformat PEP8 Errors, and this fixes most of them. It has the right sort of whitespace settings here and it also resolved these, like, weird extra spaces inside my parentheses here. And again, this is not perfect, but it can get you really, really far.
26:18 If you’re working with an existing codebase and want to adapt it to PEP 8 and just kind of standardize the formatting, this can be a really, really helpful tool, and it absolutely helps to try that out.
26:30 Usually I run that as a final cleanup step before I commit my code, and then it’s really useful because then you don’t have to go through and manually, like, fix things like an extra space and stuff like that.
26:42 You can just load that off to Anaconda and that’s going to save you a lot of time. All right! So this actually concludes the setup for Anaconda.
You must own this product to join the conversation.