Python is quick and easy to learn. And yet, there is a massive gap between knowing the
common aspects of the language, loops, variables, functions, and so on, and knowing how to write
a well-factored application using modern tools and libraries. That's where learning Python
is a never-ending journey. Sebastian Witwoski is here to give us his take on a modern Python
developer's toolkit. There are a bunch of great tips in store for us. This is Talk Python to Me,
episode 279, recorded July 21st, 2020. Welcome to Talk Python to Me, a weekly podcast on Python,
the language, the libraries, the ecosystem, and the personalities. This is your host,
Michael Kennedy. Follow me on Twitter, where I'm @mkennedy. Keep up with the show and listen to past
episodes at talkpython.fm, and follow the show on Twitter via at Talk Python. This episode is brought
to you by Linode and us. Hey folks, before we jump into the interview, there's so much going on over
at Talk Python Training that I want to share not one, but four exciting announcements with you.
Number one, we released a new course that has been a passion project of mine,
understanding how memory management works in Python. We don't have enough clear info out in the world to
get a solid picture of how our code runs. So I dove in and created a five-hour course covering all the
internals of Python memory. Things such as reference counting, garbage collection, and the fascinating
allocation algorithms. We also touch on a bunch of simple techniques and patterns to employ, which allow
our code to run much faster and use only half as much memory. Check it out at talkpython.fm/mem.
Announcement number two, Chris Moffitt of Escaping Excel Hell, episode 200 fame, has written a course to
help folks escape Excel and start leveraging Python's data science stack with tools like Jupyter and Pandas
to solve the same type of problems and do the same type of computation. So if you're keen to use more
Python and less Excel, check out that course at talkpython.fm/Excel. Announcement number three,
Chris and I are putting on a free webcast pulling some of the tips and tricks out of that Excel course
to help folks move from, well, Excel to Python. If you want to join us for that live event, it's going
to be live on September 29th, 2020, and it'll of course be available as a recording afterwards. Just
visit talkpython.fm/Excel dash webcast to be part of the webcast. And number four, we've crafted a
unique learning experience for teams built on our courses over at Talk Python. We call this team
cohorts. We've already tested this for individuals and we'll be announcing something for them shortly.
But right now we're launching this experience for teams within companies. The idea is to put together
a digitally native experience that brings your team together and keeps everyone on track and gets them
ready for that big upcoming project. These experiences include a private community, weekly video calls,
a clear schedule going through one or more courses of your choosing, and a lot more. Check it out at
talkpython.fm/cohorts. Wow, that's a lot for just one week, isn't it? All of these links will be in
your podcast player's show notes. Please check them all out. This is truly the best way to support the show.
Now let's talk about some modern Python.
Sebastian, welcome to Talk Python to Me.
Hi, thanks. I'm excited to be here.
Oh, it's great to have you here. I'm looking forward to exploring this idea of a modern Python
developers toolkit. You put together a really nice tutorial, which you, quote, presented at PyCon,
like all the other folks who presented at PyCon this year. And we'll talk about that. And I think
it's going to be a lot of fun to see your take on it. You do have a lot of good advice in here.
Before we get to it, though, let's start with your story. How'd you get into programming and Python?
So you know all those cool stories about people starting to code when they were like 10,
and then they learned some ancient programming language to program a video game?
Well, that's not me. My father had a computer, but as a kid, all I did was playing video games.
And then I went to the university where I was studying automatic control and robotics.
And I had three semesters of programming. And I think first semester was C, then was two semesters
of C++. And boy, I hated C. I mean, it was so bad for me that I actually had to cheat on the exams to
even pass it. And then I switched to C++. And it was slightly better. But so I eventually forced
myself to learn C++ enough to pass all the exams.
Yeah, C++ is better. But yeah, still, it's not Python.
Exactly.
Other modern languages. Yeah.
And then at some point, I think I had to do a website. And that's when I really got interested
into programming, let's say, because, well, it was HTML and CSS. But just the idea that you could
immediately see the results in the browser was what really picked my interest. So I learned
just making, I learned some basic CSS, some basic HTML and jQuery. And that's how I got into programming.
Then I learned PHP and I started building websites.
Cool. Well, I think building websites is one of those things that while it's pretty complicated,
because there's HTML and CSS in the programming language, there's a lot of moving parts.
It's definitely one of those things that's more rewarding than a lot of lower level programming,
because you can see it right away. It's easy to share, right? You can put it up on
some site and say, hey, everyone, check out this thing that I built. Try it. It's pretty fun.
Yeah. Yeah, definitely.
Speaking of learning languages that you might not want to learn, when I was in college,
I started out in chemical engineering and then moved over to math. But while I was in chemical
engineering for just a little bit, they said, you have to take a programming course. I'm like,
excellent. I've always wanted to learn C++ or something like that. And they're like,
no, you have to take Fortran. Really? I have to take Fortran. Like, yes, you have to take Fortran.
If you want, you can take C++ as an elective, but Fortran is the most important programming
language you're going to learn in your entire life. Don't you know? And I'm like, no, it's definitely
still not true. I'm waiting for that to come true, but I don't think that was true.
Anyway.
Yeah. You got to start where they have you start, I guess.
Yeah. It's nice to see that now it's changing and Python is more and more.
Absolutely.
Becoming more and more popular.
Yeah. And I think it makes it way more approachable to people who are not there to be programmers.
Exactly.
Right. Like economists or whatever. So that's how you got into programming and found your way over to
Perl. And where did Python come into this whole story?
So once I know how to make some websites, I applied for an internship at CERN. CERN is this
European organization for nuclear research. And so they have this large hard-run collider where they
do a lot of cool stuff, but they hire not only physicists. So I applied for an internship as a web
developer. And actually the first day when I started, one of my senior colleagues went to pick me up from
the reception and he had this t-shirt saying, I enjoy learning Python. It was a nice afternoon.
So that was my first interaction with Python. It was hilarious.
That's awesome.
I had to learn Python on that job, but I really enjoyed it. So I stayed at CERN for the next six years,
building some really cool stuff there.
Oh man, that's got to be awesome. Did you work remotely or were you in Geneva or where were you?
I was working there. CERN doesn't, at least back then didn't support working remotely.
Yeah. That's got to be such a cool environment to be in.
It was. I mean, people from every country, it's crazy. You just go for a lunch with people from
around the world.
That guy has a Nobel prize, of course. So he can join us. Come on.
Yeah, quite cool. And how about today? What are you doing now?
So I finished working at CERN and then I moved to freelancing. So right now I'm a Python freelancer
based in Poland. I still do a lot of web development because I really like it. I think that's my favorite
part of programming, but I also help companies fix their workflows, set up their architecture for new
projects. And I even do some custom onsite trainings.
So that sounds like a lot of fun and it's a nice mix. So you get to try a bunch of different things
and keep your hands in them. Nice. All right. Well, let's talk about your tutorial that you
did at PyCon online, which was really, you know, people who I'm sure most people know now, but
basically the way that PyCon US handled the whole shutdown lockdown thing was they said, okay,
we're going to take tutorials and the talks and still give people a chance to present. But it was
record this as a screencast basically, and then we'll publish it to the YouTube channel and we'll
list it on the PyCon page as a list. That's what you did. And I think honestly, for yours,
that might be even better because you came up with such a nice set of digital resources that people can use
that I feel is going to be really valuable to everyone, not just the, you know, 50 people or
however many would have taken it in person there. Yeah, I agree. I looking in the hindsight, I think
that it was much better than a tutorial that I would be able to give in person because I was over two
hours. So I really had to time everything. Well, I did a lot of editing to remove some stuff and still
it's almost like two and a half hour of basically only meet. Yeah, but it's engaging. You did a good job
with it. So when you come up with this idea, like we all think about what kind of tools we use
and it's easy to get stuck in doing certain things. So, you know, I never really, I mean,
I never really wrote tests. So, you know, I'm not using pytest, for example, or I've always just
enjoyed using Vim. So I'm going to stick with Vim, even though there may be a more productive
editors, for example, out there and so on. So how'd you come up with this idea for your,
your workshop here? So I actually come up with this idea talking with people.
So I noticed that people who are learning Python, they might learn how the language works. They
might know the syntax, but then when they sit down to write a Python project, they just don't know how
to start. I mean, you can see a lot of people asking what's the deal with virtual environments,
like what they should use for testing. So I had this idea in the back of my head, like,
hey, let's, let's make a tutorial how to build a simple Python project from scratch,
explain what tools to use, and just show enough tools. So people can like, take this project and
build something bigger on top of it. So to not overwhelm people with like, too much of different
stuff. Don't toss poetry or like different styles of managing dependencies, just give them one solid
approach that they can use. Yeah. And that can be one of the challenges, right? The more guidance
that you give, the less generally applicable the result is, right? If you, that you have to use
talks in this way, and you have to make sure you have these tests, and then you have to make sure
your APIs always look like this, and so on and so on, like every layer, here's your continuous
integration, how that has to be, right? Like, it just means fewer people necessarily fit that same
workflow. And so I think you found a pretty good balance here. Thank you. I had to remove a lot of
stuff because initially, I wanted to add even more features. But then I decided to like really narrow it
down to only the essential things that you're going to need. Yeah. So you set the stage. And I think this
is hard for beginners to get a lot of times to understand, sort of internalize this feeling. But
you said that there's a huge gap between knowing Python, the language, like I know how to do a loop,
I know how to create strings, I know how to like write a function, that kind of fluency with Python,
there's a huge gap between that and knowing how to write applications or create proper projects with
Python, right? Yeah, I mean, maybe huge gap is a huge word, but there is a gap. Yeah, there's definitely
a gap. Yeah, let's say that. Yeah. So that's the gap that I wanted to fill with my, my workshop. Yeah. And I
think it's an important one. I don't want to put words in your mouth. I'll put the huge out there for
the students who are taking, say, like my beginning programming courses. I find that they all can see
the syntax, they can recreate the syntax. If you say, okay, here's a thing, I want you to loop over it.
Yeah, no problem really quickly. A lot of times they've been able to learn how to like do that and
make Python do that thing. But if you put them in front of a blank file, just open up, you know,
app.py and come up with a solution to some kind of problem, like I want to use the right data
structures, and I want to have the right libraries to talk to this, you know, to APIs and stuff. I feel
like there's, there's a pretty big gap where they feel like they just don't know where to start.
And so putting a lot of the structure in there, I think will help people get sent down in the right
direction. One thing that I thought was pretty neat about your presentation was you had a whole website,
right? A whole dedicated domain for this tutorial. Do you want to tell us about that?
Yeah. So the website is bycon.svitowski.com.
I'll link to that in the show notes so people can just click it.
I used Hugo, the static site generator with learn template. And actually I stole it from
Nina Zakarenko's tutorial that I saw some years ago. She also used this template and I really like it.
So then I found it, I think completely by accident. And I saw it. Oh, that's the website template that
she was using. So I gave it a try and it looks really good. It has a nice sidebar with all the
sections. Yeah. So you have a whole bunch, like, as you go through the tutorial,
you've got the website and you've got the video on the website, which is pretty straightforward.
But then, you know, you, for each major section, you've got like a write-up about that with links.
So people don't have to try to type down or search for the various things you're talking about.
They've, you've got the exercises that people can do walking through. So I think like, this is what,
this is what I was thinking of when I said, I feel like this is almost better because it was done
online because you have this, this whole resource for people here.
Yeah. I was trying to make this website independent of the video. So you can just,
even if you don't know the video, you should be able to go to the website and kind of follow
everything. So it's complimentary to the video, but it stands on its own.
Yeah. Cool. So there's a couple of exercises that you have the students do. Maybe we could talk about
the exercises and then we could start going through the steps and the guidance and whatnot.
Sure. So I basically have two sections of exercises. First one is done after I talk about managing Python
versions and Python dependencies. So first I explain how you can install different versions of Python,
how you can like install different versions of packages. And there is an exercise to make sure
that people understand what's the difference between virtual environment and Python version and how they
interact with each other. So I have like a set of steps, try to install, let's say this package globally,
try to install, try to create a new virtual environment, create a package there,
deactivate virtual environment, try to run this package and see what happened. Do you understand?
And then I have the second exercise. This is a bigger one. This is basically a mini project.
So at the end of the workshop, after I discuss everything, testing documentation and all this
stuff, I have a mini project to create a to-do application. So basically you take everything
that I showed you before and your objective is to create a to-do application. I wanted to make
something more interesting, but everyone knows what a to-do app is. So I decided to go for this.
That's the canonical example of a like, let's rebuild something that everyone is familiar with.
That's right. It used to be blogs, but now the JavaScript people made it the to-do app that
everyone builds. Yeah. I put a lot of thought into finding a more interesting project, but nothing.
Yeah. The problem is the more interesting it gets, the more you get lost in the details,
right? It's yeah. Anyway, it's fine. It's a cool example. All right. So there's a couple of
hands-on exercises that people get to do. That's good. Now, maybe the first big piece of advice,
but also advice that is challenging for people who've been doing a long time. And I guess to some
degree, this is probably more focused at people who are new. So that hesitancy matters less,
but is to recommend a code editor, right? People are very passionate or sticky to their code editors.
Yeah. I was really hesitating if I should include this point or not. But at the end,
I checked the Stack Overflow survey from last year and VS Code was used by basically every other programmer.
This portion of Talk Python to Me is brought to you by Linode. Whether you're working on a personal
project or managing your enterprise's infrastructure, Linode has the pricing, support, and scale that you
need to take your project to the next level. With 11 data centers worldwide, including their newest data
center in Sydney, Australia, enterprise-grade hardware, S3-compatible storage, and the next-generation
network, Linode delivers the performance that you expect at a price that you don't. Get started on
Linode today with a $20 credit, and you get access to native SSD storage, a 40 gigabit network,
industry-leading processors, their revamped cloud manager at cloud.linode.com, root access to your
server, along with their newest API and a Python CLI. Just visit talkpython.fm/Linode when creating
a new Linode account, and you'll automatically get $20 credit for your next project. Oh, and one last
thing. They're hiring. Go to linode.com slash careers to find out more. Let them know that we sent you.
You actually, at the start, you said, okay, we got to pick a code editor because you got to write
some code. Let's do VS Code. And yeah, you went through the stats from the Stack Overflow
Developer Survey, and you're like, it's like 12% in 2017. It was 30% in 2018. In 2019, it was like 50%
or something. You're like, all right, this is a pretty safe bet. A lot of people are doing this
and liking it. Yeah, exactly. Yeah. So I guess first bit of advice, would you get VS Code? You
don't have any Python tooling? Not really? Yeah. So when you install VS Code, it's pretty empty.
But the nice thing about VS Code is that when you install it, you have this welcome window,
and it says like, oh, if you're programming in Python, check out this package.
So basically, first package that you install is Python package. And it's actually the most popular
package on the VS Code marketplace. It's like way above...
By a long ways. Yeah. It's like two or three times above even the C# and C++ one. And those are,
you know, very core to Microsoft, right?
Yeah, exactly. Yeah. Yeah. So when you start, you basically first install Python package. And then
once you have Python package, it's going to give you some pop-ups like, hey, do you want to install
a code formatter? Or like, hey, do you have some tests? Do you want to install a test tool and stuff
like that? So it really takes you by hand through all the steps that you need to have the basic Python
IDE. Yeah, for sure. Have you looked at PyLance, the new language extension for VS Code for Python?
So actually, no, I've heard about it from Python Bytes. Yes.
In the podcast. And then it fell off my radar. Yeah, Anthony Shaw brought that one. Yeah,
that was cool. Yeah. I thought it's going to be like built in into Python extension,
but then I saw it's a separate extension. So I kind of forgot about it. But have you been using it?
Not really. I just looked at it when Anthony and Brian and I were speaking about it. And it looks like
it looks deeper into the Python language and more across your project, more across
the libraries, looks at the documentation that might come with like the help strings and whatnot.
So you actually have to have the VS Code Python plugin. And then the PyLance plugin is like on top of that
one. So it uses the new language server thing that they've got that I don't really know much about. But
it looks I installed it. It looks like it'll be be nicer. We'll see. I haven't done a ton with it.
Yeah, I'll definitely check it out at some point. Yeah, not to start too much of a heated debate. But what
what are your thoughts on things like VS Code and PyCharm? The more GUI based helpful, trying to help
anyway, editors versus some something like Emacs or Vim? I know a lot of people still use Emacs and Vim and
the more terminal based editors. I think both are good. I mean, I'm still using Vim if I want to do some
quick edits, and I'm in the terminal. So I don't really mind it. It's just VS Code or PyCharm are more
beginner friendly. So if you're new to programming, that's you're probably gonna start with one of
those. And what I really like about VS Code is that you can use it for different programming languages,
you're not limited to Python. While if you start using PyCharm, then you can use it for Python. But if
you want to switch to JavaScript, it's not very useful. Yeah, it's weird how to make Yeah,
yeah, VS Code is more general, right? It sort of equally does Node.js as well as it does.
Python. Exactly. And you can customize it with extensions. Yeah, cool. So one of the pieces of
advice you gave around is choosing a linter and not choosing the default linter. So two questions,
what is a linter? And then why did you want to change what VS Code wanted you to do with one?
Basically, a linter is a tool that if you make some mistakes in their code, it's gonna in your code,
it's gonna shout at you and tell you that you did it wrong. And some linters are more strict,
some linters are less strict. For example, for me, Flake 8 is the best linter that I use all the time,
because it complains about, for example, if you try to use a variable that you forgot to define,
it's going to complain about it. If you define a variable that it's not used in the function,
it's going to complain about it. So it gives you hints, it doesn't try to change your code,
but it gives you hints as you're coding. Well, some people use PyLint. And it's also a good
linter. For me, it's just more strict. And sometimes it's complaining that you have
too many public methods in a class and stuff like that. So that's why I didn't like it.
I see. Okay, yeah, cool. So you basically go through and show how to,
you know, when you set up the Python extension in VS Code, it says, oh, you don't have a linter.
Click here to install PyLint. And you're like, hold on, let's go over here and tell it that
we want it to use Flake 8. And then it'll say, actually, click here to install Flake 8, right?
Yeah, exactly. Because by default, VS Code tries to install everything kind of globally. And I wanted
to show people how to nicely isolate everything using virtual environment. So instead of just
letting VS Code to install Flake 8, I first show people how to use pipX to install Flake 8. And then
I point VS Code to use it. Yeah. Well, let's talk about pipX just for a second, because it's,
it is a pretty interesting way to install these kinds of tools. I'm a big fan of pipX. I think
it solves this really interesting problem. And to me, the way I think about it, this only really will
like maybe resonate with people on a Mac. But to me, it's like homebrew for Python utilities and library,
right? Maybe chocolatey on Windows is a similar type of thing, right? Some kind of central repository
where I can say install this tool. And it will just install it to my computer. That works well for
some things. But there's a bunch of things you would normally pip install, but then they have commands
like black or pytest or Flake 8 or whatever. And so pipX installs those. But usually those tools don't
necessarily belong in your project. You just want to run them against your project. And so this will create,
pipX will create a virtual environment and install that thing in isolation and sort of manage it like it's
like an application, if you will, like an app store or something.
Yeah. So there are basically two types of packages that you probably want to install on your computer.
One is packages for the project that you're currently working with. So for that, you would use a virtual
environment.
Right. Like requests or something. Yeah.
Yeah, exactly. Or like Django, you might be working on different versions of Django and you might be
switching between projects. But then there are also some packages like Flake 8, like black, maybe cookie
cutter. And for that, you probably want to have one version, preferably the most up to date version on
your computer. You don't need to have five different versions of Flake 8 for each of your project. You just
want to use the latest Flake 8 because you know it's going to catch up, catch most errors. So that's where you
would use PipX. So it lets you install package and it will act as a global package. But at the same time,
it's installed inside a virtual environment. So PipX handles all this virtual environment creation,
activation. And for you, you just see this package as normal, as if you would normally install it with
Pip.
Yeah. Yeah. It's super cool. I really like the way that that works and it manages and lets you upgrade
those things separately. So you might use that to install Flake 8 or various tools that you're going to
do, set that up in VS Code. And then another thing you talked about is by default, probably code is not
installed in the path. So you can run VS Code, but if you're on the terminal and you want to edit
something, you might want to be able to type like code space file name or code space directory name
and open those up. Right. So you've got to go and take an extra step to install that.
Yep. And I think right now VS Code will even give you a pop-up saying that, hey, you can actually do
this. And there is a command in the command palette to add VS Code to your shell. So it's getting even
more and more beginner friendly. Yeah. VS Code is interesting in that it does so many things and yet
it's not obvious what it's capable of. Right. It's, you know, if I go into PyCharm, there's a button,
you're like, oh, this button, what does it say? Oh, that means it's going to do like a profiling of my
code if I click that button. But there's no button for so many of these things in VS Code. Instead,
it uses this idea of the command palette. Right. Yeah. As you said, that's the thing, for example,
with testing, there is like no sidebar for tests. But once you set up testing tool, you have a
sidebar. Yeah. You talked about some of the commands that are useful there. And unfortunately, I only have
these for Mac. People have to translate probably command equals control. And that's pretty much it.
Open the command palette. So you hit command shift P, and then you can type all sorts of stuff. And this
is kind of how you get to all those features that we're talking about. Go to files, command P,
go to a symbol, command shift P, or command shift O, or you just command click a thing.
Go to symbol, control T, go to line, control G, command space, or sorry, control space to trigger
the IntelliSense. But you couldn't get that to work, right? If for some reason you had to like use them
alternate, like shift escape or something to make that come up. Yeah, I don't know what was the
problem. Yeah, yeah, yeah. So there's a bunch of cool commands. And I think anytime people switch into
an editor, that's not the one that they're used to using, it's really worthwhile to sit down and just
maybe even make a list of here are the top 20 most common hotkeys. Right? Because just because you're in
a GUI doesn't mean you should be using the mouse all day. Yeah. And it's interesting that you mentioned
it because before VS Code, I was a heavy sublime text user. And when I switched to VS Code, my muscle
memory was still with sublime. And actually for VS Code, you have those extensions that will bring the
keyboard bindings from other text editors. So there is an extension that will rebind some commands to
commands from sublime. There is an extension for atom and stuff like that. So for a long time, I was using
the sublime key binding. And even now, I recently reinstalled everything from scratch, I still had to
manually remap some commands because my muscle memory was just not fixable. Yeah, yeah. Sometimes it's like,
nope, this is I'm just going to always type this. So we're just going to make it do that. And that's
okay. Now, another thing that's not obvious, I think, in VS Code, especially to beginners, is how do you run
your project? Right? Like running a single file, maybe it's not too hard, right? You can there's a
little button for that. But if you open up a Flask app, and you want to use Flask run, rather than,
you know, just running the app.py or whatever, or you want to run some tests or so on, that can be
tricky. Do you want to talk people through that experience? Basically setting up a run configuration?
As you mentioned, if you want to run a single file, you can use it from the command palette, or you can
press this green arrow button, everything is fine. And you can actually even use it for a Flask server,
you just run the file that starts the Flask server, and it's going to be fine. But actual power,
better idea is to use the debug configuration, or how is it called? I don't have VS Code.
Something, yeah, launch, maybe launch configuration. I'm not sure. Something like that. Anyway, yeah.
Yeah, so it's under the sidebar for debugging. And if you don't have any launch configuration
configured, you're going to have like a button there saying configure launch file or something
like that. Once you click it, you can select one, you can select one of the popular frameworks that
are there. So you can select the Django or Flask or Pyramid configuration. And that's going to create
a launch file for you with some predefined commands. And for most cases, you should be good.
But if you want to modify, for example, some parameter that you specify at the run server command,
you can do this there. Once you have this launch file, you can actually launch a web server.
And now the cool thing is that if you put some breakpoints in your code, so you can click next to
the line number in the gutter, and it's going to put a breakpoint, this will trigger the breakpoint
in your server. So now if you go to your website, it's actually going to stop at that breakpoint,
and you can use VS Code to do debugging.
Yeah, yeah, that really unlocks the power if you set up the debug, whatever configuration to launch
those things. And yeah, because then you can do the breakpoints, watch the variables, the inspections,
all that kind of stuff. And again, the reason I think it's valuable to talk about these kind of
things is it's not obvious that this is what you should be doing to get the debugging support to work,
right? There's a lot of like implicit stuff. But once you know, it's fine. Another thing that's pretty
cool is code snippets.
Yeah, so VS Code comes with some code snippets by default. But if you install some extensions, for example,
if you install Python extensions, you're going to get even more extension, you're going to get even more
snippets, like a snippet to create a class to create a main function, let's call it like that. And then for example,
if you install a Django extension, you're going to get even more snippets like Django template tags and stuff like
that. And on top of that, you can define your own snippets. So just you just select a command, create a
snippet. And you're going to get a nice scaffolding with some information, what do you have to put in
each place, basically, that's a JSON file. So you define what's the abbreviation to trigger the snippet,
you define the description, and then you just define the code that you want to put there. And what's also
interesting is that you will have some placeholders that if you press tab, your mouse will move there.
So if you want to pre fill some values in a function, you can press tab and you will be moving between those
places. And there are some special variables that will insert, for example, the current date or the
current folder. So VS Code has this nicely documented in the documentation.
Yeah, you can build it's almost like a little scripting language. So that's super cool. And if you find
yourself just doing the same thing over and over again, maybe it should be a snippet. And then your last
thing on the VS Code topic before we move on, is you talked about a bunch of interesting extensions for VS Code.
And some of these I hadn't heard about are plugins. Let's maybe just talk about a couple of your
favorite, because I mean, obviously, the Python and the PyLance ones are there. But there's other
ones that people maybe haven't tried out, like there's a Django one, and there's a Telecode,
you want to hit the highlights there?
Sure. So apart from the obvious Python, then if you're working Django or in Flask, then there are some
plugins that will add additional snippets. And for example, for Django, it will also add some syntax,
highlighting support for Django HTML files. But apart from the obvious things, there is, for example,
a plugin called IntelliCode that will try to predict, it will try to make the auto-suggestion a bit more
smarter. So it will try to predict what's the item from the autocomplete list that you are most likely
to choose. And it will move this item to the top of this list and add this nice star. And it actually
works pretty well from what I noticed. So I can actually highly recommend it.
Yeah, cool. Yeah, uses machine learning across a bunch of open source libraries to say, well,
if you're using SQLAlchemy, these are the most common functions to be called in these situations.
And it tries to understand the context rather than just, here's the whole list, whatever.
Yeah. From other interesting stuff, there is, for example, autodoc string.
So if you define your function, you can just press enter, add triple quote, then press enter again,
and it will put some scaffolding for the documentation. It will also take the arguments
from the function signature and put it in the documentation. So it will make your life much
easier. If you're using type hints, it will also detect that it will also detect the type hints and put
them in the correct place. Oh, yeah, that's cool. That's also nice.
From non Python specific extensions, I really like bookmarks, because it makes my it makes moving
around the code much easier. So we just put a bookmark in different files, and you can easily
switch between them. And yeah, get lens is nice. Oh, yeah, get lens is a massive extension. I mean,
the amount of features it has, it's incredible. And just using very small subset, but really gives you a
really good overview of what's going on in your project, you can open repositories, comments,
branches, merge requests, whatever you want in the GitLab, GitHub, it has everything.
Yeah, I remember looking at some CPython source code with get lens installed in VS Code. And it had
something like this file change from really long ago by Gito Van Rossum or something like that. I'm like,
oh, okay, this is still like some of the original code right here. This is cool.
Talk Python to me is partially supported by our training courses. How does your team keep their
Python skills sharp? How do you make sure new hires get started fast and learn the Pythonic way?
If the answer is a series of boring videos that don't inspire, or a subscription service you pay way
too much for and use way too little, listen up. At Talk Python Training, we have enterprise tiers for
all of our courses. Get just the one course you need for your team with full reporting and monitoring.
Or ditch that unused subscription for our course bundles, which include all the courses and you pay about the
same price as a subscription once. For details, visit training. talkpython.fm/business or just
email sales at talkpython.fm.
Yeah, you also have error lens. Yeah, so error lens, you can use it to customize how you want to see the errors. So by default, you're going to have like a separate tab in VS Code that lists all the errors. But I like to see the squiggly brackets, the squiggly underscore, whenever an error happens. It will also let you customize if you don't want to see the info or hints, you can put the font size to zero and it will completely make them disappear.
So it's a nice way to customize your errors.
Yeah, yeah, very cool. Maybe the last one to just give a shout out to is Jumpy.
Oh, yeah, this is a really cool one. Basically, how Jumpy works is that you define a keyboard shortcut, you press it, and then it will show like a two letter, small two letter snippets, basically at every word in your current screen. And then if you press those two letters, your cursor will jump to that place.
So it's like incredibly convenient way to move around your code, as long as you remember to use it, because I always forget it.
Yes. And we were talking before, like, I always forget this as well. I have Ace Jump installed on PyCharm. And I can hit Ctrl semicolon and then type a few characters.
And it's kind of like find, it'll highlight all the stuff that would match that substring. But as you said, it puts one or two characters next to those things. And if you want to go to the third, like if you're doing a request.get, you could do the quick search for G.
And you want to go to the third get, it would highlight that there might be like, you might press, like why or something to get there. And it'll just immediately take you there with no mouse. It's beautiful.
And I never use it. I hardly ever use it, let's say. That's my new thing. I'm going to try to start using this more because it's awesome.
All right. Well, that's probably enough for the editor. But this is a whole lot of stuff, a whole lot of customization and specialization to really get the editor and the tools and the stuff more than just, hey, you should use VS Code, but really just set it up the way that you like.
So that's good advice. Then another problem, especially beginners are going to hit straight away.
And this one is tricky because it varies on the platform is, do you have Python? What version of Python is it? Is it the right version? Can you install packages into it? Should you install packages into it? And so on and so on, right?
Yeah. So that's the next topic I talk about after the VS Code, basically how to manage different Python versions and packages.
And why the first point is why you should not use the system version of Python, because once you mess it up, you're going to mess a lot of things on your computer.
So one of the first things that you want to do is to install some custom Python manager and switch Python version to this custom version.
I think back in the days when I was still using Linux and I was still a beginner programmer, I tried to update the global Python version, my operating system, and that broke a lot of stuff.
Yeah, I can imagine it does.
Yeah, these days when I'm on virtual machines, I will set up a virtual environment. Usually the virtual machine is meant to run one app anyway. So I'll set up a virtual environment for that app. And then in my RC file, when I log in, I'll have it activate that virtual environment. So if I accidentally type pip install something or mess with it, it's just going to mess with that virtual environment. It's very rare. I want to log in and change the system Python. It's very common. I want to log in and change the virtual environment for that particular app that server is dedicated to.
Actually, there is a setting for pip that will prevent pip running outside of the virtual environment.
There is an option.
Oh, really?
I said, yeah.
Oh, that's cool.
I don't remember it by heart, but I actually have it on my blog. I discovered it some time ago and I was super happy about it.
Nice. Yeah, that's really cool. A couple of suggestions that you have here. Obviously, we have VE and V, like Python 3-M VE and V type of thing, the built-in, which is nice that it's built-in. But you didn't start there. You started with PyE and V.
Tell us about why you chose that and what you're doing with it.
Yeah. So I wanted to show first how you can manage different Python versions and then how you can manage different Python dependencies. So basically, PyE and V is a tool that lets you install different Python versions on your computer.
So let's say you're on a Mac, so your system Python is 2.7. For whatever reason, it's still there.
That's crazy. And they're going to take it away in the next version. But they're not replacing it with 3. They're just going to take Python away or something to that effect. I remember them announcing.
Oh, cool.
So I don't know if that's better or worse. But anyway, it's not going to have Python 2 at some point.
It's probably going to break a lot of stuff.
Yeah, I think it probably will. That's Apple. All right. Sorry. Anyway, didn't mean to derail you. So you've got the wrong version of Python if you're on Mac. If you're on Windows, these days they have the Windows Store little pop-up thing, which is cool, but also confusing to people.
And on Linux, who knows? It depends on your version, right?
Yeah. For example, if you want to install a different version of Python, you can install it using, well, just some standard Python installation. But PyE is much better because it lets you easily switch between versions.
So if you're using just the latest Python all the time, it might not be a big of a deal for you. But for example, I'm working on a different project. One project is using Python 3.6. One is using Python 3.8. And I want to find a way to easily switch between those two versions.
That's what I would use PyEnf for. So you install PyEnf, and then you say PyEnf install 3.8.3. And that's going to install this version of Python. And then I say PyEnf install 2.7. And it's going to install 2.7 as well. It's going to be different than the system one. It's going to be managed by PyEnf.
And now let's say I'm working on a project with Python 2.7. All I have to do is to say PyEnf global 2.7. And just like that, my global Python version is 2.7.
Yeah, I wasn't aware of this feature of PyEnf, where you can basically shadow what Python means on your machine and cycle it. So with PyEnf, you install the different Pythons. You could have, you know, 3.7, 3.8, 3.9, maybe, if you get, if it'll pull the beta version 2.7.
But then you can say PyEnf global Python 3.6, PyEnf global Python 3.8. And then if you just type the word Python, that means that version you set it to, which is pretty awesome, actually.
Yeah, it is. And it's actually done in a pretty clever way. So it's using the system of shims, or how is it pronounced, that basically it prepends to your path variable, this PyEnf Python version.
So whenever you type Python, your computer is going to look for Python binary, and it's going to find on your path this shimmed version, this shadowed version, and it's going to use this one.
It's a pretty non-intrusive version.
So that lets you get some version that, the version that you want onto your system. And then you went on, I think you were using virtual, just VNV, not the built-in virtual environment capabilities of PyEnf, right?
I showed how to use the VNV module from Python, because, well, it's built in Python, so that's the easiest way. But I'm not using this in my daily work. And so I wanted to show, I think I'm using virtual fish right now, which is a wrapper over virtual environment.
Right, but it works in the fish shell, yeah.
Yeah, it's the same as virtual env and virtual env wrapper for non-fish shells. And what I really like about this is that it has a set of commands to list all your virtual environments, to create them, to remove them.
And so it's much easier to manage those virtual environments. And I prefer it to have like one separate folder for all of those.
Yeah.
And I also wanted to show people that there are different tools than the built-in Python module, because I see a lot of people who are teaching Python, they show the built-in Python tools, but there are a lot of other cool tools that you can use, and maybe you'll like them more.
Yeah, for sure. And then the last thing you talked about here was PipX, which we already covered, so I don't want to spend too much time on it, really. Although I do want to throw out one tip for people, because something that has been driving me crazy about PipX is I install Python through Homebrew, which I don't know if it's the best way. Maybe I should be using PyInf, but for whatever reason, that's what I've been doing. So it's that way.
And when Homebrew upgrades Python, PipX virtual environments become invalid for some reason. I don't know why. But with PipX, you can just do a PipX reinstall-all, no, reinstall-all, and it'll basically tear them all down and recreate them.
So that'll kind of fix when these shims get out of sync with PipX and whatnot. So maybe that'll help some folks out there. But yeah, that's a cool tool.
Next up, we've got our runtime, there's Python's pick, we've got our editor all set up. Now it's time to start creating projects. And you already mentioned cookie cutter, but yeah, cookie cutter was what came to mind here, right?
Yeah. So I was thinking, because people are wondering, people are often asking what's the best way to structure Python project. And pretty much there is no best way. But if you really don't know where to start, there is this tool called cookie cutter.
And basically what it does is, you're provided with some template that someone created before you. And it's going to generate a scaffolding of the project for you. But it's going to actually ask you some questions on the way. So if someone was giving you the option of different databases, cookie cutter is going to ask you, do you want to use MySQL? Do you want to use Postgres? Do you want to use SQLite?
And depending on what your answer is, cookie cutter will define a different config file. So all those things have to be first defined by the person who created the template. So cookie cutter doesn't do any magic.
But if you're new, you can go to cookie cutter website, and you will find some very popular templates, let's say for a Django project, or for a Python package, or for a Flask project, you can take the most popular one, provide it to the cookie cutter, and it will generate base for your project that many, many people before you have used that is usually very well documented.
It has some hints like, well, how to set up CI or how to deploy some stuff.
Yeah, those are really, really nice. And I guess it depends on what you're building. But often, you know, say you're building a package, you want to have a setup pie, and the various other things that go along with that, right? And it'll automatically pre populate that when you just run cookie cutter, you know, create me a packet, a Python package thing that's called this.
Yeah, it's really nice. I like it quite a bit. Again, those are super, they can be super opinionated. So depending on how, how much you align with what they're trying to do, it may work well or not. But there's many, many different templates. So you can even just search GitHub for them, and just point cookie cutter at a GitHub repo, long as that repo is public and has, basically is a template.
Yeah, exactly. And as you said, they are very opinionated. But in my opinion, it's opinionated. It's much easier to find a cookie cutter template and actually remove stuff than to start from scratch. And when you're staring at your blank editor, trying to come up with some ideas how to set up things.
Yeah, absolutely. I totally agree. Find a good cookie cutter template and roll with that as well. Maybe it even sets up things like black and pre commit hooks and so on for you.
Yeah, exactly. Many templates already follow some good practices for installing some linting and formatting tools for you.
Yeah. Another thing that you spoke about is the REPL, the redeveloped print loop, you know, what you get when you just type Python. And just typing Python is okay. But honestly, it's, it leaves a lot to be desired. Like even just up arrowing through your history, it repeats the line.
Like if you wrote a block of three, three lines, like you've got to like re arrow through them to get to if you want to go back to that group, right? That's kind of annoying. So there's other options that you can use for your rebels that you suggest or talk about at least I noticed that a lot of people who do some tutorials or show some presentations about Python, they use the default Python REPL.
And if imagine you're a new person who doesn't know there are different Python REPLs, you're gonna get stuck with Python REPL. And as you mentioned, it's not the most user friendly REPL. I mean, it's there, it's fine to use it. And it's not Python core developers job to improve it. But there's different tools that you can that are basically a drop in replacement.
And for example, one of them, and my favorite one is IPython. Basically, that's the Python REPL on steroids. So you just install it. And instead of typing Python, you just type IPython. So there is like almost no change for you.
And out of the box, you got like syntax highlighting, you got top auto completion, you it's much smarter about when you press enter, because when you start typing a function definition, and you press enter, it's gonna actually indent the next line and it's gonna detect that you're still typing this function, not that you just finished typing.
And for example, you can append a question mark. So you will see the signature of a function or you can append two question marks, you will see the whole whole code source code of a given function. So I'm actually using IPython to read the documentation. Also the time is really cool.
Yeah, I didn't know about this feature. This is really cool. So if I had a variable like x equals a string, something just quote something, right? Then I hit enter, then I can say x question mark, and I'll say this is a string.
It's string form is whatever the string is, its length is what it is, and it'll list out the doc string. And yeah, that's super nice that you can just ask it questions about what it can do. Yeah, very nice.
Yeah, and there's a lot of cool things that you can do. For example, you can edit a file. And for example, you can tell it to edit command or a function, and IPython will try to figure out where this function is defined. And if it's from like some package install with peep, it will actually open the file in this virtual environment that contains this given function, and it lets you edit it.
Oh, wow. That's pretty awesome. Cool. So IPython is definitely there. And it's got this kind of Jupyter feel, right? It's like, basically a little bit like Jupyter in the terminal or command prompt. But you also talked about two other ones, ptpython and bpython.
Yeah, so there is ptpython. I think the name stands for prompt toolkit Python, and bpython. They are both much lightweight approached in IPython. So they are between IPython and Python, they have syntax highlighting, they have some tab completion. But they don't have as many features as IPython. But for example, they have different features.
I think it's bpython that lets you rewind code. So if you type something and you realize you made a mistake, and you want to actually undo the last command, because I don't know, you close the file, or you just deleted something from the array, you can actually undo this, there is a keyboard shortcut to undo the last command, and then you can continue working on the file, or whatever. So both of them are much better than the build in Python REPL.
Yeah, I totally agree. And if you pull up ptpython, you can have an emacs mode, you can have it in VI mode, you can go and pull up all sorts of things like enable fuzzy completion and all sorts of cool stuff, syntax highlighting. Really, really nice. I haven't done as much with bpython, but they both seem, you know, much nicer to me, in that you get autocomplete, and you get better editing and whatnot.
Yeah, so certainly good recommendations there. I guess, probably not quite as much to discuss with tests in some regard, right? Like you said, okay, well, people are using pytest, you should probably use pytest. That seems to be pretty good consensus these days.
Yeah, it's a pretty solid choice. I also explained why it's better to use pytest than the build in unit test library. I mean, the unit test library is pretty good, but it also suffers from the same problem as the Python REPL. So it's kind of basic. And at the same time, I find it a bit over complicated, because you have all those different assert types.
While with pytest, you just have one assert. And basically, that's all you need for most of the time.
Yeah. One thing that maybe is not as obvious is how to get the test to run in VS Code.
Ah, yeah. So that's a pretty cool feature of VS Code.
I think when you install Python extension, it's going to ask you what test framework you want to use.
So you select pytest, and then you will have the sidebar with tests.
And then you can just click Discover Tests, and it will go through your project and try to find all the tests, all the files and all the functions that are matching the pytest convention.
And then in the sidebar, you will see all those tests, you can run them.
And you will see if it's passing or if it's failing, you can debug them.
So if the test was failing, it will actually stop in the place where the assertion is failing or when there is some problem.
And then actually, if you open this file in the VS Code, you will see like a small text above each test function that will tell you you can run it or you can debug it.
So you don't even have to go to the sidebar.
You can just click in your text in your code editor, and you can run the test directly from the test file.
Yeah, that's super cool. And it just runs that one test, right?
Yeah, exactly.
All right. Another thing that you suggested people do is that they write documentation.
And I guess this probably varies depending on what the goal of the project is.
If the goal is just like a small little app for just you, maybe it doesn't need documentation.
But a lot of times it will.
What was your advice there?
You already sort of mentioned Autodoc.
I know that was a plugin for the extensions, but there's also Autodoc for APIs, right?
Yeah. So for documentation, I recommend Pretty Standard Sphinx.
It's a very good tool used by many projects.
And even with the read the doc project, if your project is open source, then you can host your documentation for free.
So this is really nice.
So yeah, for documentation, I would use Sphinx.
And then as you mentioned, there is this Autodoc plugin that you can add to Sphinx.
And it will automatically generate the documentation for your API, which is really cool because it saves you a lot of repetitive code.
Yeah, right. If you're already going to write it for the help string in your library, you might as well just get it to write, you know, copy it over there.
Exactly. And there are also some other cool plugins.
So if you have an example of code in your doc string, quite often it can get out of date because, I mean, documentation is often, well, outdated.
So you can actually install a plugin that will try to detect code snippets inside the documentation and it will actually run those code snippets, comparing it to the next line in the documentation.
And if it's not matching, it's going to report you an error.
Yeah, that's cool. So if you've got an example in your doc string, like here's how it's supposed to work.
And if it doesn't do that, that's a problem. Nice.
So one other thing that you did talk about in this section that I thought was worth mentioning is what should you put into your documentation?
And you talked about someone, Daniela, I don't remember what the last name was, who talked about four different categories you might have,
like tutorials, how-to guides, explanations and references to help give you some structure to think about what you might write.
Yeah. So I saw this really good talk from Daniela Procida and he splits the documentation into four parts.
First is tutorials.
So people who are like complete beginner can come to your project and see, OK, how I can start using it.
Then you have how-to guides.
So recipes for solving a specific problem.
So if you have a very specific problem, you can find it probably there.
Then you have explanations.
So this part actually explains how your project works, how each parts interact with each other.
And then finally you have reference, which is like a Wikipedia page for your project.
So all the terms, all the classes, whatever they are defined there.
Maybe the why, like why did we put this here or the history of it or something.
Yeah.
OK, that's really helpful.
And then last thing on the documentation side, you mentioned that you can get a, you know, normally you would write this in restructured text.
But Markdown is the current thing that people like to use the most, I think.
There's a Markdown extension you can get as well.
Yeah, there is a Markdown extension for Sphinx that you can install and it will let you write documentation in Markdown.
Yeah.
We even have pypi.org supports Markdown these days.
Yeah.
Sweet.
And then I also mentioned that if you're writing some rest APIs, there are tools like Swagger or Redog that you can use and they will also generate like really nice documentation for your APIs.
Yeah.
If you use frameworks like FastAPI, it's like basically built in and stuff like that, which is really, really cool as well.
It depends on what you've got underneath the covers there.
Cool.
So last thing, got everything set up.
You've got your package structure.
You got your documentation, presumably written subcode.
The last thing, you've got to deploy your app.
Yeah.
So I was hesitating if I should include this section, but since it was pre-recorded, I still had some time.
So I decided to give people like a very simple Docker file that they can use to take this project that we are building and to deploy it as a Docker container.
So I explained like very briefly what Docker is.
I started by showing how to make a Docker file, which is intentionally kind of bad Docker file.
Then I explained what are problems with this Docker file.
And then finally, I showed them like a much better Docker file that they can use, but it's still a very basic one.
Yeah.
Cool.
Then you could put it in presumably in like a Kubernetes cluster or something like that if you want.
Yeah.
There's even this website played with Docker.
So at the end of the workshop, I'm showing how to use this website to actually deploy our application.
It's deployed in this like terrible URL that it's going to, and the container, it's only there for four hours, but it's there.
It's online.
You can share it with someone.
It gets you the idea.
Yeah, exactly.
That's awesome.
Well, that's quite a whirlwind tour.
All the tools that you might use, but I think it's a lot of good advice.
It exposes people who are maybe a little bit new to putting together these projects and closes that gap from, I know the language to now I know how to actually create and ship applications.
In a more professional way.
Yeah.
Pretty neat.
So thanks for putting that tutorial together.
And because it's on PyCon online, people can go take the workshop, right?
It's two and a half hours and it's easy to follow along and yeah, nicely produced.
All right.
Well, before we call it wraps, even though we are getting long on time here, two final questions.
First, maybe this is a hint.
I'm not sure how much time we spend on VS Code, but if you're going to write some Python code, what editor do you use?
That's a good question.
Now, so it's VS Code and for like some short edits, it's Vim.
Right, right on.
And then notable PyPI package, you know, something that maybe you've run across recently that's really cool that people don't know about.
So I'm pretty sure you already know what I'm going to say.
It's IPython.
So I'm not a contributor or anything, but I really love it.
And actually, I was writing a blog post a few weeks ago and I put an introduction, something along the lines, along those lines that I really like IPython.
And if I ever got invited to your podcast, not that I have anything to talk about, I will mention IPython.
So I'm here and I'm mentioning IPython.
But no, seriously, this is a really great package and I use it all the time.
Yeah, it's super cool.
And I really have not used it enough as a REPL.
So I should definitely be thinking more about that as well.
Awesome.
All right.
Well, final call to action.
People are excited about this, all the ideas we talked about in the tutorial.
How do they, what should they do now?
Well, start building.
So you can just, you can follow this tutorial and this will give you a pretty solid base for any projects that you want to build on, build from it.
And if you know someone who's learning Python and who's not sure how to start building stuff or who wants to build something in Python, just please tell them about this video.
I mean, it's quite long, but it's free.
And at the end, you'll have a very solid scaffolding of a Python project that you can use.
Yeah.
Good advice.
Thanks for being on the show and sharing it with everyone.
Thank you very much.
Thank you for inviting me here.
Of course.
Bye-bye.
This has been another episode of Talk Python to Me.
Our guest on this episode was Sebastian Witwoski, and it's been brought to you by Linode and us over at Talk Python Training.
Start your next Python project on Linode's state-of-the-art cloud service.
Just visit talkpython.fm/Linode, L-I-N-O-D-E.
You'll automatically get a $20 credit when you create a new account.
Want to level up your Python?
If you're just getting started, try my Python Jumpstart by Building 10 Apps course.
Or if you're looking for something more advanced, check out our new Async course that digs into all the different types of Async programming you can do in Python.
And of course, if you're interested in more than one of these, be sure to check out our Everything Bundle.
It's like a subscription that never expires.
Be sure to subscribe to the show.
Open your favorite podcatcher and search for Python.
We should be right at the top.
You can also find the iTunes feed at /itunes, the Google Play feed at slash Play, and the direct RSS feed at /rss on talkpython.fm.
This is your host, Michael Kennedy.
Thanks so much for listening.
I really appreciate it.
Now get out there and write some Python code.