Setting Up Python
00:00 Welcome to the next lesson, where we’re going to be setting up Python. This consists of three steps. We’re going to install Python first, we’re going to upgrade your system Python to Python 3—or, you know, that’s actually not really true.
00:12 We’re going to add Python 3 in parallel with the Python 2 that comes with macOS. And then we’re going to set up Python code linting so you can get live feedback on your code directly in your editor as you write the code.
00:25 We’re going to set that up using the SublimeLinter plugin. And then after that, as a last step, we’re going to set up Python autocompletion, or code completion, with the Anaconda plugin and we’re also going to do that directly inside Sublime Text so it’s nice and integrated. All right, let’s dive right in.
00:42 The first thing we need to do is install Python 3. Let’s do a quick check if you already have Python 3 installed. We’re just going to open a terminal window here and you can try out this command.
00:55 You just type in the command and then hit Return, and depending on what output you get, you might already be set up correctly or you’ll have to install Python 3.
01:04
In my case, this is a brand new system and when I run this pip3
command I get a result that says pip3: command not found
. That means, in this case, I don’t have Python 3 installed.
01:14 And what we’re going to do now is we’re going to install Python 3 using the Homebrew package manager. Let’s open a browser here and go to brew.sh, and that’s the homepage for the Homebrew package manager.
01:29 What that allows us to do is to very easily install a bunch of development dependencies on macOS. macOS doesn’t really have anything like the APT package manager on Debian Linux or Ubuntu Linux or on other Linux versions.
01:45 So Homebrew is kind of a missing piece there that allows us to just go, “Hey, I want to install Python,” “Hey, I want to install this library that I need for my development workflow,” and Homebrew will figure out where to get that library from and will make sure it installs correctly and you don’t have to do a bunch of manual fiddling around with that.
02:03 So it’s a really useful thing. The way this works is that you want to select this line here, like double-click on it and select Copy, and then you want to go to a terminal window
02:15 and paste that in. And usually that’s going to start executing the command immediately. Sometimes you have to hit Return for that command to be triggered, depending on how you copied that. All right, this looks good, so we’re going to need to hit Return again.
02:29 And now it’s going to ask us for our account passwords. I’m just going to type mine, hit Return again, and now it’s installing Homebrew. And if this is the first time you’re installing Homebrew, it’s going to ask you to install the Apple command line developer tools.
02:44 Basically, this is going to install a C compiler and a bunch of other dependencies that Homebrew needs to work, and they come from Apple directly. It’s going to trigger that install. I’m going to jump ahead here because it’s going to take a short while to download, and then we can continue the installation.
03:04 Now this asks us to confirm the installation, so we need to enter our account password again, hit Return. All right, and so now it’s done with installing the command line tools and it’s actually moving on to install Homebrew. All right, there we are!
03:18 So, depending on your internet connection, this will take a few minutes to complete and download all of its required files. And once the installation’s complete, you’ll end up back at the command prompt in your terminal window. All right, so now that we have Homebrew installed, it’s time for setting up Python. The big question here is whether you want to install Python 3 or Python 2.
03:38 Now, either version of Python will work fine with the plugins that we’re going to install, and if you’re unsure which version you want to use, then I would highly recommend you go with Python 3. Only if you absolutely have to work with Python 2 code, then I would pick Python 2.
03:52 And it’s actually possible to install both versions side by side using Homebrew. This is actually what I’m doing in my own setup. And for the purposes of this tutorial, we’ll install Python 3 now.
04:04
All right, so you want to go back to your terminal and then run the following command. So, Homebrew adds this brew
command and now you can use the brew install
command—or sub-command, I guess—to install Python 3. You want to go brew install python3
and hit Return.
04:21
And that’s going to do a bunch of stuff to download and install Python 3 in the background. And this is the big convenience of brew
because you can just go, “Hey, I want python3
,” and it’s going to install all of these dependencies and it’s kind of the quickest way to do that, right?
04:37 Because you can just type in that command, run it, and actually walk away, come back a couple of minutes later, and everything will be set up for you. It’s just a really great way to install these dependencies. Okay, so that’s it.
04:49
Now this added a new command, so when I clear that terminal again and we try that pip3
command that had previously failed, what’s going to happen now is that we actually get some output here.
05:00
We’re running this command and it actually gives us some output so we know that pip3
was installed correctly, or that python3
was installed correctly and then pip3
is a part of that. So, this is great.
05:12 Now we can move on and actually continue with setting up SublimeLinter, which will integrate code feedback tools, or code linting tools, directly into our Sublime Text setup.
05:27 Okay, so let’s set up Python code linting. The setup that I use personally is built around the SublimeLinter plugin. And the cool thing about SublimeLinter is that it actually isn’t a code linter itself, but it’s kind of a tool that just integrates a bunch of linters into your Sublime Text setup, which means you can use various linting tools or actual linting engines with the same interface in Sublime Text. So, I do a lot of web development.
05:57 That means I have a linter for Python, I have a linter for JavaScript and for my HTML code, and all of these I can use through the same interface with SublimeLinter.
06:07 I’m going to show you how to set that up because it’s a really flexible and awesome setup, I think. The first tool that we actually need to install is called Flake8, and that’s kind of the linter engine here.
06:17
We need to go back to the terminal for a second and then we’re going to install the Flake8 Python linter, which is a command line tool. I’m going to go ahead and type the install
command here using the pip3
package manager.
06:33
And then this will install Flake8 on our system. And just to make sure it actually worked, we’re going to try and run flake8
and pass the --help
argument.
06:47 And so when you run this, you’re going to get a bunch of output that shows you that Flake8 was installed correctly. At this point, you should be able to do that.
06:53 And down here, you can see some version numbers of the internal tools, like the internal code checking tools that Flake8 uses and their version numbers.
07:02
Those might not be the same that you’re seeing here, like they’re probably going to be higher by the time you go and install Flake8, and that’s great because Flake8 is getting upgraded and updated all the time. And if you ever want to update the version of Flake8 that you have installed to the latest version, you can just do that with the pip3
command.
07:20
You just want to go pip3 install --upgrade flake8
07:26 and then run that. And now, you know, it didn’t do anything because it’s already in the latest version, but that’s going to bump your Flake8 version to the latest version.
07:34 So, Flake8 is actually a combination of other tools. It’s a combination of a static analysis tool that is going to give you feedback on bugs and potential issues in your code, and then a code style checker that’s going to make sure our code is formatted according to the popular PEP 8 formatting standard. So, it’s a really nice, all-inclusive solution. All right, now we can close the terminal again and we’re going to go back to Sublime Text.
07:58 Now we need to install SublimeLinter. SublimeLinter is a code linting framework and we’re going to install that through the Command Palette. We’re going to go to Install Package in our Package Control.
08:11 This is going to load for a second and then we’re just going to type in the name of the command we want to install. In this case, we’re going to install the SublimeLinter plugin here.
08:21 SublimeLinter is, like, a framework for code linting inside Sublime Text. So now what we’ve got is we’ve got SublimeLinter and Flake8 installed and now we need to connect them both.
08:32 And the way we’re going to do that is by installing another plugin—again, with opening the Command Palette and going to the Install Package command.
08:42 And that one is called SublimeLinter-flake8,
08:47 so this is the one. And what that’s going to do, it’s actually going to integrate, it’s going to make sure that Flake8 and SublimeLinter can communicate with each other. Or I guess, SublimeLinter’s running Flake8 and then feeding the output back into your editor.
09:02 So, once we’ve installed these other plugins, we need to quit Sublime Text. And this is usually what you want to do every time you install a new plugin. It’s not always necessary but, you know, just for good measure I always quit my Sublime Text and restart it because it can lead to really weird and subtle and sometimes not-so-subtle bugs, so I generally do that every time I install a new plugin. All right, so now I’ve restarted Sublime Text and that finalizes the SublimeLinter install. We can get rid of that message here. I’m going to close the window. Okay, and so what I’m going to show you now is what this actually added.
09:37
What I’m going to do here, I’m going to start typing some Python code and I’m going to add some mistakes to that code. So in this case, I have a typo here that says primt()
instead of print()
.
09:50 And now I’m going to switch the syntax highlighting from plain text to Python.
09:56 You can do that down here in the lower right and then click on Python. Or alternatively, you can also just search for python in the Command Palette and then hit Set Syntax. Okay, let’s bump up the font size a bit. All right! And then once you set the syntax, you should see this red warning and this little dot here.
10:16
This is already linter feedback that comes from Flake8. When you click inside that area here, you’re going to see the error message displayed down here. It says undefined name 'primt'
and then also you’re getting this little highlight here. So, this should now work. In some cases, it doesn’t work the first time around so you might have to quit Sublime Text and restart it, or also save the file locally.
10:41
You can just go and save that somewhere as a test file and then Flake8 will start picking up these errors. Now, Flake8 isn’t just going to call us out only on these misspellings, but it can also do more interesting things. For example, if you’re adding a bunch of import
statements—you know, where we’re importing the os
module or we’re importing the sys
module and we’re not really doing anything with them, then Flake8 is also going to call us out on this and it’s going to say, “Okay, os
was imported, but it’s actually unused.” And then similarly, if you had a structural problem with the formatting in your code, so let’s say we’re importing the collections
module down here, then we will get a warning that says, “Hey, you’re importing this thing and it’s not at the top of the file.” So, Flake8 is really good at pointing out these actual issues in your code—for example, this typo here with the print()
function.
11:32 It’s also really helpful when it comes to making sure your code is formatted consistently. So, in this case, we already have a bunch of issues here in our code. You know, there’s a blank line at the end of the file, or like an extra blank line, and it’s calling us out on all of this stuff. And cleaning that up can really help you become a better Python developer.
11:52 And I had really good results from starting to use tools like Flake8 and also with a team that I worked with. It just cut down on a lot of time spend—or time wasted, actually—on fruitless discussions around code style and making sure the code is formatted exactly the way everyone wants it to be. With a tool like Flake8, you can just agree on what the tool wants and then it’s going to look reasonable and nice, actually.
12:17
So, for some of these warnings, sometimes you will get, like, a false positive and you can disable these warnings by putting a comment and then typing out # noqa
, and that will disable that particular warning or error on that line.
12:32 So basically, it’s going to disable any linter messages on that particular line of code. So you can use that as a tool to sometimes suppress these error messages.
12:42 All right. So, there are two changes I want to make to SublimeLinter.
12:48 The first one is that it’s actually kind of distracting to get all of these messages with the super visible, like, surrounded in a colored box style. I don’t really like this, like, crazy fruit salad look it gives my code, so let’s change that.
13:05 And the way we’re going to do that is you’re going to go to Sublime Text > Preferences and then we want to go to Package Settings > SublimeLinter and then your user settings for SublimeLinter.
13:17 Now, these are going to be empty now, so what we’re going to do here is also open the default settings for SublimeLinter. And this is like a general trick you can apply in a lot of situations.
13:28 In this case, I’m just going to copy and paste that, and then we’ll paste that in into my user settings. So, the problem with the default settings is when you change them, they can get overridden again by Sublime Text, so you always want to make sure you’re moving your actual settings into the Sublime Text user settings.
13:48
The change we need to make here is just rename this so it’s our "user"
settings now. And we can actually close the default settings again. Okay, so we’re in our SublimeLinter user settings, and now we need to find that "mark_style"
property. Right now this is set to "outline"
, which gives us this super visible box warning layout, and we’re going to change that to a "squiggly underline"
, which I’m going to show to you in a minute. And you just want to hit Command + S again so that saves. And then when we go back to the test
file, you can already see this is a lot less distracting now, right?
14:23 Because it’s just this squiggly underline highlighting and it has less of that in-your-face fruit salad kind of look to it. Now, the other thing that we’re going to change is that, at the moment, every time you modify this file, SublimeLinter is going to immediately run and give you feedback on your code. Now, you know, this can be a good thing, but it can also be a bad thing. Because as you’re typing out your code,
14:48 it’s going to start detecting all these problems as soon as you stop typing. And this is just, you know, an in-progress thing as I’m defining this function.
14:55
I don’t really care about this feedback, so I’ve found having too much linter feedback in here gets kind of distracting too. You can fix that by going back to your SublimeLinter user settings and actually setting the "lint_mode"
property to "load/save"
.
15:14 What that will do, that will change the settings for Flake8 so that it only runs every time you save a file. And to me, that’s a much better way to use Flake8, because now I can make all the changes I want and then as I’m getting ready to review the code or just kind of take a look at what I wrote, I’m going to hit Command + S and save that.
15:35 And now this is when I get my Flake8 feedback popup inside my editor, and I can start fixing some of these things. And as soon as you start editing again, that’s going to go away.
15:45 So it’s, like, nice and clean and you can focus on the actual code when you need to and still get that feedback when you want it.
15:53 And so this is how I use SublimeLinter and Flake8 in combination. We can close these other files here. And what I’m going to do next is I’m going to show you how to set up Python code completion. So, let’s set up Python code completion.
16:08 A lot of Python developers who use Sublime Text asked for a way to get good, intelligent autocomplete for Python code. It was one of the most requested features. And for a long time, Sublime Text was really lacking in this respect.
16:20 There weren’t really any good plugins that did this. But this really changed when the Anaconda plugin came out. And now, I want to mention one thing first.
16:29 There’s actually two things in the Python space that are named Anaconda. The first one is a Python distribution that’s called Anaconda, and it’s mostly used on Windows and in the scientific computing community.
16:42 So, you might have heard of that, but what I’m talking about here is the other Anaconda, and that’s a plugin for Sublime Text that adds autocompletion and code documentation and a bunch of other cool features directly to your Sublime Text editor.
16:57 Because it’s easy to confuse these two, and I just wanted to mention that so that you know what I’m referring to. I’m talking about Anaconda, the Sublime Text plugin, that’s actually a full-featured solution for Python code completion—almost like turning Sublime Text into a little IDE.
17:13 And it has a couple of really cool features that I’m going to walk you through. I mainly use it for the autocompletion and the show inline documentation functionality. But before we dive into that, let’s actually install Anaconda and get it set up. So again, you want to go back into your Sublime Text. Then open the Command Palette, find the Install Package command in Package Control.
17:35 It’s going to download all of the available packages, and then we’re going to search for Anaconda. And it’s this one here with this weird formatting.
17:43 By the time you’re installing this, it might look a little bit different. It’s hosted at damnwidget.github.io. I’m going to go ahead and install this now. All right, so we get the success message here that Anaconda was installed successfully.
17:57 We can close that, and then you want to make sure you’re actually quitting Sublime Text and restarting it to finalize the install, which is just a general best practice because it can lead to all kinds of weird bugs here. All right!
18:10 So, first things first, we’re going to dive right in and configure Anaconda. Again, you want to go to Preferences > Package Settings > Anaconda and then go to your user settings.
18:21 This will pop up an empty settings file for Anaconda. And what we need to do here first is we need to point Anaconda to the right version of Python, because by default Anaconda will use the Python version that came with macOS, but now that we installed an up-to-date version of Python through Homebrew, this is not ideal.
18:40 So we want to make sure it uses the right version of Python because otherwise it’s going to populate its code completion with a different version of Python and then you will get these oddly mismatched or invalid code completion solutions.
18:52
The way we’re going to do this is by setting the "python_interpreter"
option. We’re going to set that to your Python 3 path. And the way you can find that out is by going to the terminal and doing a which python3
, and that’s going to point you to the location that we used here.
19:16 If you’re following this tutorial and you used Homebrew to install Python 3, it will be this path. So you can just use that or you can also double-check if you want to make sure.
19:26 Now we’re going to save this, but there’s actually another setting I want to make here. Remember how we installed SublimeLinter just a couple of minutes ago?
19:34 That was a code linter. Now, Anaconda actually also has its own built-in code linter. But personally, I like SublimeLinter a lot better in terms of performance and in terms of the flexibility it gives me because it’s one single tool that I can use with a bunch of different languages like Python and JavaScript.
19:51 And if you’re having both of them enabled at the same time, if you’re having the Anaconda linter and the SublimeLinter enabled at the same time, then you’re going to see many of those linter warnings twice.
20:02
The solution is to actually disable code linting in Anaconda, and so we’re going to do that now because this could get really distracting if we don’t disable it. All right, so you want the "anaconda_linting"
setting here and I’m going to set that to false
.
20:16
And then you also want to set the "pep8"
setting to false
as well. And then hit Command + S to save those options. So, by the way, these settings are all JSON files, so you want to make sure you’re terminating each line except for the last one with a comma (,
) at the end and use the JSON syntax.
20:37 These files are actually pretty easy to edit, you just want to make sure you don’t introduce syntax errors because otherwise you’re going to get a warning or an error message from Sublime Text. Okay, so these settings should kick in effect immediately but again, just to make sure they’re getting picked up, we’re going to close all of this stuff and restart Sublime Text.
20:55 And I know this is a little bit frustrating having to do that all the time but, you know, once we’ve set everything up, then of course you won’t have to restart your Sublime Text all the time.
21:05 All right, so now I want to show you some of the cool features that we have added now through the Anaconda plugin. The first one is autocompletion and code documentation for Python code.
21:15 Okay, so we’ve got a blank file here and the first thing we need to do is we need to set this to a Python syntax so we get all of the Python tool support.
21:24
The first thing I want to show you is how I can trigger the autocomplete for Python code. It’s actually really easy to do that because you just start typing out Python code. So for example, let’s say I’m looking for the abs()
function.
21:35
I would just type a
or ab
and then already I’m getting all of these autocompletion suggestions and I can just pick the one that I want by hitting Return.
21:45 And now once I’ve completed a function name, I’m getting this nice inline popup that shows me the Python documentation for this particular function. I can call this function like that.
21:58
And this also works in a number of other places. So for example, I can go and do an import
statement, and then this will list me all of the Python modules that Anaconda knows about.
22:07
I could go import os
, and then again I would get the docstring, the documentation for the os
module. What I want to show you now also is that you can cancel this autocompletion by hitting Escape, and then that’s going to go away. And you can also cancel documentation display.
22:25 Now if you want to bring that back, you’ve got to hit the Control + Space key and that will bring back the autocompletion. That’s kind of the two ways you can trigger autocompletion.
22:35 Just start typing out stuff, or you want to hit Control + Space and then that will bring back the autocomplete. The autocomplete in Anaconda is actually really powerful.
22:44
It’s smart enough to do things like here, when we’re defining this list constant, and I’m hitting a dot (.
). That’s also going to trigger the autocomplete, and then I get a bunch of functions here that are available on this list object. I could just go ahead and narrow that down if I want the .count()
function. And again, that’s going to tell me this returns the number of occurrences of this value that I would need to pass.
23:11 So this is a really, really helpful system. And of course, the autocomplete is also going to work with your own code and with your project code, so it’s a really powerful feature.
23:22 Another really cool feature that I want to show you is the Anaconda Goto Definition feature. This is a really IDE-like feature where you can jump to the definition of a Python symbol just by clicking on its name. Let’s try an example here.
23:37
Let’s say we’re importing something from the collections
module, and we’re going to get the namedtuple
class. Again, we get the docstring and everything. And now what we could do is, you know, we want to do something with this namedtuple
at some point.
23:53
And what we could actually do now is double-click on namedtuple
and then go to Anaconda > Goto Definition. And that would actually jump us directly to the definition of the namedtuple
factory here—this object here—that we could then read and understand.
24:11 This is jumping directly into the Python source code here, so this will only work for modules that have actual Python source code that weren’t implemented as C modules in Python.
24:25 But of course you can imagine this, on the one hand, is going to be really useful for jumping into the Python standard library, and on the other hand, this is going to be really awesome if you have a bunch of project code and you want to find out where a symbol was defined inside that project.
24:40 So this is a highly useful feature if you’re exploring a codebase, and it works surprisingly well. It can’t always exactly find out what object you’re referring to and where it’s defined, but it works in 90% of the cases and it’s really a huge time-saver so you don’t have to try and find that manually and try and find out what symbol you’re using there.
25:02 Another really useful feature is the autoformatting feature in Anaconda. Just to give you an example here, I’m just going to write out a bunch of example code and I’m going to introduce some formatting blunders here as well. Again, we’re going to set that to Python syntax, and this is clearly not formatted correctly according to PEP 8.
25:24 We can actually confirm that by just saving this file and then running the linter on it. We’re getting all of these warnings in the small snippet of code where it’s complaining about a bunch of formatting issues.
25:33 What we can do now is actually just right-click anywhere and then go to Anaconda > Autoformat PEP8 Errors. You want to click on that and what that’s going to do, it’s going to format our code according to the PEP 8 standard.
25:47 So when I save it now, this is actually going to lint clean in Flake8 as well. And you can see here, it cleaned up all of the formatting issues that we had here, and of course, I picked an example where that worked really well.
25:58 It’s not always perfect because sometimes, you know, it’s ambiguous and doesn’t know how to format your code correctly. But if you’re working with a bunch of legacy code or if you want to make sure your own code is formatted consistently and you don’t want to do that manually, then it can be a really great shortcut for making sure your codebase is formatted according to PEP 8. So again, this is a really nice feature.
26:20 It’s going to make your life a little bit easier as a programmer and it’s a great last step to run, you know, before you’re committing a piece of code. It makes sense to just run that clean up and make sure it works!
26:29 And of course, we can still undo that by hitting Command + Z. Every time you do this, you have the full undo functionality available, so you can just try it out and see what happens and then jump back and jump forward again. A really cool feature in Anaconda. All right!
26:42 To recap quickly, now we installed Python 3 using Homebrew and then we set up SublimeLinter, so now you have inline code linting. And then after that, we set up the Anaconda plugin for Sublime Text, which almost turns Sublime Text into an IDE in terms of the autocompletion and code documentation and Goto Definition and autoformatting capabilities that we added. All right!
richardoychen on March 24, 2020
For the “Goto Definition” function at 23:55, it seems like the current version makes you to first jump to the definition at line #1 (from collections import namedtuple), and then you have to right-click out the Anacobda menu and click again the “Goto Definition” so you can go to the actual function definition. Wonder if there’s a workaround for that? or maybe this change is made on purpose…?
Raul C Pena on March 27, 2020
I do not see a default setting under Sublimelinter. When I click on the setting it opens up the Sublimelinter.sublime-settings – Sublimelinter, with another side window that opens up as blank which says user
Raul C Pena on March 27, 2020
I see your supporting documents have what I need. Thanks
Raul C Pena on March 27, 2020
I get this error
Invalid settings in 'Packages/User/SublimeLinter.sublime-settings':
Additional properties are not allowed ('user' was unexpected)
Charlie Clemmer on May 9, 2020
Like Raul, I’m seeing some differences with the sublimelinter settings as mentioned around 15:05 of the video. I’m still working through it to see if it resolves itself, but possibly a new change with more recent versions of Sublime 3?
Charlie Clemmer on May 9, 2020
It appears SublimeLinter does not like the extra “user”:{} section in the user settings. It doesn’t have the “default”:{} heading as shown in the video either, so when I leave out that section it seems to save fine.
Charlie Clemmer on May 9, 2020
Full notes from this video. I’ll try to verify with the Windows version as well, but this pass was done on macOS as that’s my primary platform 90% of the time.
The SublimeLinter preferences don’t like the “default”:{} and/or “user”:{} clauses and will throw errors when trying to save with those in place. There are several config items in the included macOS file that also don’t work, so I went back to the default settings and only modified the two settings discussed in the video to come up with these notes.
“mark_style”: needs to be “squiggly_underline” with an underscore instead of a space. This value is documented in the default settings, so likely a version change from when the video was produced.
“lint_mode”: needs to be “load_save” with an underscore instead of a slash, as mentioned in the comments above. Same note, the underscore is listed in the default setting comments, so likely another change since production.
Using the included macOS settings file for SublimeLinter throws many errors, so I stopped trying.
Using the two settings tweaks that were discussed in the video, I noticed that SublimeLinter no longer alerts on unused modules that were imported, as it did before making the user settings changes.
With Anaconda - double clicking the namedtuple symbol and trying to use the Anaconda “goto definition function takes me to a blank page labeled None. I did not use the HomeBrew python install, as I had downloaded Python 3.8.2 directly from python.org after encountering errors with IDLE from the HomeBrew install, so that could be the source of my problem.
Lastly, running the Anaconda PEP8 format errors function, SublimeLinter did not re-evaluate the syntax until after I saved the source. You mention the same in the video, however, what’s shown is the linter error highlights disappear as soon as Anaconda runs.
MSimcox on June 12, 2020
in the PDF setup for the Mac edition, when installing flake8 it says to “try running the following command in your terminal window:”
~/.local/bin/flake8 --help
but in the video it’s just
flake8 --help
the PDF caused an issue for me since there was no /.local/ folder, I don’t know if this is because I’m running Catalina or not.
You must own this product to join the conversation.
Max Carey on Feb. 16, 2020
Looks like at 15:00 minutes the correct setting should be load_save not load/save for my version of subliem Text