Brought to you by Michael and Brian - take a Talk Python course or get Brian's pytest book


Transcript #11: Django 2.0 is dropping Python 2 entirely, pipenv for profile functionality, and Pythonic home automation

Return to episode page view on github
Recorded on Monday, Jan 30, 2017.

00:00 This is Python Bytes, Python headlines and news delivered directly to your earbuds.

00:05 It's episode 11, recorded January 30th. This is Michael Kennedy. I'm here with Brian Okken.

00:10 Hey Brian. Hey. Hey, you ready to talk about Python news and some cool things that we found this week?

00:15 I am. I've got a little bit of a cold, but I think we can get through it.

00:18 Yeah, I think you're going to be just fine. You sound totally good to me.

00:21 So hang in there for a short bit while we talk about these things.

00:25 So one of the things that makes Python fabulous is this so-called batteries included story, right?

00:31 And sometimes that means standard libraries. Sometimes that means PyPI and the 97,000 packages there. And we're not done improving how we work with those, are we?

00:41 Yeah, no, we're not. And one of the things that came up this recently, I think it was last week, is Kenneth Reitz announced a new tool that he wrote, I guess, over the weekend called PIPENV, P-I-P-E-N-V, and we'll link to both his announcement and a Reddit thread where people are talking about it. Because there are other projects out there that do the same thing, but this is, or similar things, this is a way to bring pip and virtual environments together in the same toolchain and also pip file, which since we talked about pip file, I think it was last week or... Yeah, I think it was last time, yeah.

01:26 But I wanted to, this is something I've just started using. I just started using this pipenv and so far the normal, even just the simple environment of you know setting up a new environment, a new directory for a project and setting up a virtual environment, this has helped me just make that faster with just pipenv install something and it creates a virtual environment and I like the pipenv script which or pipenv shell which brings you into a shell terminal within that environment.

02:06 And that works with my workflow pretty good.

02:10 So supposedly, it automatically finds your project home recursively by looking for a pip file.

02:15 Yeah, so one of the things that's really cool about that is it will create a virtual directory and one of these pip files.

02:22 And if I'm reading this right, that means you don't have to activate your virtual environment to manage it.

02:27 Right in the particular shell, right?

02:28 you just have to be in the sub directory somewhere and then your package management commands with pipinf will apply to that virtual environment dedicated to that project.

02:39 - Yes.

02:40 - Which is really nice.

02:41 - Yeah, and in order to, well, the magic of virtual environments where it like switches out the different, a different Python for you and different paths, that's, you still have to be activated with that, but that's where the PIPFM shell command comes in handy.

03:01 But you also don't actually have to be in that environment, like you said, to update the pip file.

03:06 And there's a run command where you can not go into the environment with your shell, but just run a command within the environment.

03:15 - Yeah, that's pretty sweet.

03:16 And it automatically adds basically to what would be the equivalence of your requirements at TXT when you install a package and automatically removes that requirement from the pip file, the new replacement for requirements.txt.

03:30 When you uninstall it and keep your pip up to date, yeah, it's very cool.

03:33 And leave it to Kenneth to say, instead of watching television this weekend, I completely rewrote how you manage packages in Python.

03:41 - Yeah, well, we still depend on, we're not, the entire tool chain for Python isn't gone over to pip file yet.

03:49 So, I mean, I don't know, we still have to deal with requirements.txt, So I'm not sure how, I'm still not sure how this fits together, but we'll, I'll give it some more playing and we'll work on it.

04:03 So. - Yeah.

04:04 Sounds good.

04:05 It definitely looks promising and I'm excited to start using it myself.

04:09 You know, one of the continuous messages that I think we are getting known for probably is our advocacy of everybody moving forward to Python 3.

04:19 - Yes.

04:21 - You know, that's where all the core developers are focusing their effort.

04:25 That's where Guido van Rossum is focusing his effort.

04:28 And to say we're all gonna stay on Python 2 is really just kind of silly, I think.

04:33 We just gotta kind of suck it up and go.

04:35 There have been some interesting watershed moments that have made big step changes in the percentage of adoption of Python 3.

04:42 Obviously, the wall of superpowers and a lot of the packages that are becoming Python 3 supported or even having Python 3 only as an option for some of those is a big one.

04:52 Another one that comes to mind is something that Django did, I can't remember when it was, it was like a couple years ago, where they switched the default instructions in their getting started docs and tutorials to Python 3.

05:04 So they just, you know, said, Okay, well, what you do is you start Python three, do the step step step step, right?

05:10 That actually made I was looking for the article, I couldn't find it, I think Donald stuff for an article, but it made something like, like a 7% difference or some some significant difference and the amount of Python 3 deployments just by switching the tutorial and the getting started docs to Python 3 as default, which I thought was really, really cool.

05:29 And so my next item is something from the Django team as well.

05:33 And this is like that turned to 11.

05:37 It's one thing to say the default documentation is to be in Python 3.

05:40 And yeah, you can do that.

05:41 You can do Python 2 as well if you want.

05:44 But the next major release of Django, Django 2.0 is dropping support for legacy Python.

05:51 There will not be support for Python 2 at all.

05:54 And they're actually cutting out the code to support it.

05:56 And they've said that that actually makes the code more maintainable, easier to evolve because there's not like these two worlds they're living in.

06:03 They're just doing Python 3 and it's going to be great.

06:05 Yeah, I think that's a good actually I think that's a good call.

06:09 I'm not sure how long they will support the 1x branch.

06:12 They send the the next release is Django 1.11.

06:18 And that's an LTS long term support release.

06:21 And that's the last one to support Python 2.7.

06:24 And they have a support policy for the various versions of LTS versions.

06:30 And I believe that it's, it's on the page that we're linking to from the show notes.

06:35 But I think the statement is like, we're willing to support one month longer than Python, the Python version that we're aiming for.

06:43 So there's one LTS that was targeted at Python 3.3 and that drops security fix support in February, it's sometime in 2017.

06:53 So that's when they're dropping support for that version of LTS Django.

06:58 - Okay.

06:59 - And so there is a policy, I don't remember exactly what it is.

07:02 And it's like, so this would be something to the effect of 2020, 'cause that's when Python 2 is stopping its support and security fixes and so on.

07:12 - Well, that totally makes sense.

07:14 - This is good news though, right?

07:14 I mean, this is a major, major step in the right direction.

07:17 - Yeah, I think it is.

07:19 The sum of the code I've seen that try to support both two and three sometimes just don't make sense.

07:23 So I think it'll actually help the readability of the Django code, so.

07:28 - Yeah, absolutely.

07:29 And if you wanna add a feature, there's less to do.

07:31 You wanna evolve it, there's less to do.

07:32 It's gonna be good news all around.

07:34 - Yeah, speaking of readability, one of the things I came across was, I was reading an article, actually it was a blog article about requests, and since we've talked about that a lot, I'm just going to skip that article.

07:49 But we all love Kenneth, so we know that.

07:52 But there was a, the article referenced, and it had some code that referenced a package called ATTRS.

07:58 It's from, do you want to try to attempt this name?

08:03 - Heineck Shalawak.

08:05 good person in the Python community, he's helped out, done quite a bit of good things. But this, I hadn't run across this before and since I come from a, what it is, is it's a library that helps you write objects and classes, I guess, a more natural way. You can have small, tight classes and it doesn't take up a ton of code and they act normal. Things like adding a whole bunch of attributes to a class that have default values. So if if they're if the class is constructed without any, any parameters, the some of the values just show up already with some default values without, and you don't have to write all the dunder methods. Since I come from C++ background, this seems very natural. And I'm like, similar to pip and I'm just starting to use this, but it's a it's pretty clean. I wanted to highlight it. Yeah, it's really nice. I've definitely heard people rave about how effective it's been for their projects.

09:03 A couple of things that it does, you know, you can easily say class person, colon, you know, dunder in it, self dot name, self dot age, whatever, right, and you pass those in, you can store them.

09:14 That's fine.

09:15 But then there's a bunch of other stuff that you might need to do.

09:18 So what if you want a human readable representation?

09:21 Well, you've got to implement repper, and maybe stir as well.

09:27 If you want to take two of those things and compare them to see if they're equal or not, by comparing the values, that's another thing you've got to write.

09:34 Well, once you write the comparers, you also probably need to have a not equals.

09:39 And then you've got to do the hashing, and you've got to do all sorts of...

09:41 There's just so many different things.

09:43 You have to have an initializer to set all of them.

09:45 And all of that stuff is handled by adders, which is pretty slick.

09:48 Yeah, and it also does all those methods, and the time cost is at construction time.

09:55 And so there's runtime, there's not really any, your objects are just as fast as not using this.

10:03 That's pretty good.

10:04 Yeah, that's really, that's really, really cool.

10:05 Excellent.

10:06 So speaking of going fast, there's an article that I ran across that is called Go Faster Python.

10:14 And we all want our Python to go faster.

10:16 In fact, I think it's really interesting that some of the core developers coming back to Python three, we're talking about what is the thing that we could primarily do to like absolutely make people say, okay, fine, I'll, I'll do the extra work to switch my project over to Python three and making you code faster was one of the things that they were focused on Python three, six goes a lot faster, use less memory than than previously, you know, three, five, and before that, and so on.

10:41 So people are really interested in making their Python code go faster.

10:45 And so this article is kind of an introduction article, it's not super deep, but it shows you how to do profiling for your Python code and benchmarking and timing and stuff, both for regular Python code and then code that's executing inside Jupyter notebooks.

11:01 Oh, wow.

11:02 So there's like special commands like %timeit that you can run to time a little block of code in a Jupyter notebook, or you can run timeit, you know, directly and things like that.

11:12 So they also talk about, you know, how do you figure out how long an overall set of of code is taken with benchmarking, how do you, you know, do function profiling to say which ones you're spending your time in actually, and then you can actually get down to line profiling with an external package called line profiler.

11:27 And all that is cool.

11:28 And I think, you know, the, the reason I put this in here is I feel like it's, it's good to remind ourselves that we should use these tools.

11:36 And we should look at how fast our code runs.

11:40 It's not it's not intuitive sometimes.

11:43 And we always tell, a regular thing to tell people now is to not do premature optimization.

11:49 Write code that looks clean and is easy to maintain.

11:52 And then after you've got things working, then look at, profile it and see where you can speed things up and where things are surprising to you.

12:00 That's great advice, yeah.

12:03 But it's not obvious how to do that.

12:05 So I welcome all tutorials like this to help people with this.

12:09 And I think the takeaway is it's like a few commands and it's not major, right?

12:13 You can totally do this and things like PyCharm have like graphical profilers built into them.

12:17 So you can it kind of does the same thing, but like, you know, gives you a slightly different representation.

12:22 Yeah, I mean, I've certainly worked on projects where I thought, okay, this thing right here, this is the part of my code that is too slow.

12:30 And thankfully, before I tried to optimize it, I've run a profiler against it.

12:34 And like, wow, that part's actually really fast.

12:36 Good thing I didn't make it unreadable.

12:38 You know, it's definitely worth measuring before you make changes.

12:43 Yeah, definitely.

12:45 So you know, one of the things that I think is a challenge for us around Python 3 and getting things moved forward in that space, we're going to keep beating that drum for a few years, I suspect.

12:57 But you know, Django, the newest, most awesome version of Django that's coming out is only going to support Python 3.

13:03 But what if we want to run that on like Red Hat Enterprise Linux or something like this?

13:09 Yeah, and actually, so that's somebody contacted us and said he's really grateful for what we're doing here and wants to be part of the community, but he feels like we're creating a community that's just Python 3.

13:22 And because he can't in his environment use Python 3 yet.

13:26 And yeah, so that's really what we want to highlight right now is just to say that we We know that some people, it's not their choice to run Python 3.

13:35 They have to run 2.7 right now because of their environment.

13:39 I guess I kind of feel like since I'm somebody that writes tutorials also about Python, I try to focus on, I'm trying to focus on 3 now.

13:47 But I think it's similar to the Django model.

13:50 It's not, I will try to make sure, like the book I'm writing, I'm going to try to make sure it runs on 2.7 also.

13:56 The wanting tutorials by default to show three is to try to encourage people to use three.

14:02 It's not up to people sometimes.

14:04 So I'm hoping that everybody like the people at Red Hat and everywhere else that does a distribution will try to give those choices to users and ship with both of them at the very least.

14:14 Yeah, absolutely.

14:15 At least have, you know, Python three as not the main option, but as an option, right?

14:22 Yeah.

14:23 There's tutorials out there to try on how you can get 3 running on some of these environments.

14:30 And I just hope that people, the right people, the sysadmins or whoever's in charge of what the development environments are, to try to get that figured out so that some of the projects can be on 3 also.

14:43 Yeah, I think that's a great message.

14:46 And the more that we can at least move those things forward.

14:49 I hear a lot of from people like, hey, we just finished upgrading from 2.6 to 2.7, right?

14:54 What is this Python 3 business you're talking about?

14:57 - Yeah.

14:58 - So yeah, like more options, definitely, definitely better.

15:01 - And I don't think that we've really covered that much stuff in this podcast so far in all of our episodes that really is three only.

15:10 We have covered it a lot, but a lot of the things like the, for instance, the ATTRS, the adders, that's two seven.

15:18 - Yep, absolutely.

15:19 So the next thing I'm going to talk about I think actually is Python 3 only.

15:24 Yeah, it is.

15:25 >> Okay.

15:25 >> So it's pretty cool.

15:27 Now, this is a project that you can take and you can make part of your smart home or you can actually make it the hub of your smart home.

15:37 So we're all getting more devices that are living in our house.

15:41 We can, you know, we maybe have a Nest or maybe we have some Arduino or we have a Chromecast or, you know, things like that, right?

15:49 Well, there's this place at home-assistant.io and what this is this is a web application that you can run and it will written in Python 3 and it will let you track and control all of your devices and home automation.

16:06 That's neat.

16:07 Isn't that cool?

16:08 So instead of saying like, well, the way I work with my home automation is I open up like my iPhone and in there I've got my home kit or whatever that thing is called.

16:16 I don't have a smart home so I don't really mess with that.

16:18 But you know, it's got a few things that it can do.

16:20 Like here's a full on open source Python web app that does all sorts of cool things.

16:26 So it says you can install it in less than a minute.

16:28 You can track the state of all of your devices in your house like so things like smart, smart lights, garage doors, television, cameras, you name it.

16:39 You control them all from like a mobile friendly little web app you can do.

16:42 So you set up automation.

16:44 So you can do things like have your lights turn on when the sun sets and you're home.

16:50 Or have them turn on when somebody comes up and it's dark, comes to your house.

16:54 Or you can have them automatically dim when you start watching a movie on your Chromecast.

16:59 And all sorts of stuff like that.

17:00 Isn't that sweet?

17:01 Yeah, it's like if this, if this, then that, but for your house.

17:04 Yes, exactly.

17:05 For all your smart things.

17:06 And it's written in Python and that's cool.

17:08 You can, you know, to get a sense of what it's like, there's a demo, home-assistant.io/demo And you can just go straight there and check it out.

17:15 And there's some pretty cool stuff you can play with there and get a sense.

17:18 The other notable thing, well, many notable things here, but one of the things that is notable to me is this is based on AIO HTTP.

17:27 So that's an asynchronous HTTP client server framework in Python.

17:32 All the cool asyncio, the async and await stuff, you can apply that to things like Flask and Pyramid and so on because basically the WSGI protocol doesn't really, you know, it was built before this concept of asynchrony. This framework is built on top of that.

17:50 So it's a, it's an interesting implementation of this modern Python web server framework as well.

17:56 So, you know, even if you're not interested in like home automation, it's still kind of cool to look through the source code on GitHub for that reason alone.

18:03 Yeah, definitely. I want to look through this because I do see the Arduino's part of it, but I haven't seen so far if you can hook up Raspberry Pi stuff or...

18:11 I don't know either, but definitely something I'd like to start playing with.

18:14 It looks cool.

18:15 Yeah, very neat. So it's open source. You guys can all check it out.

18:19 All right. Well, that's it for the news this week, Brian.

18:21 Anything else you got going on you want to chat about?

18:25 No, just trying to get over this cold. How about you?

18:28 I was on a mission this weekend.

18:30 I'm on a mission to get my next course done.

18:32 So my Python for Entrepreneurs course, I kind of...

18:35 I've done as much work as I can do and I've handed off.

18:37 It's now in the pipeline to be finished.

18:39 And so now I'm writing a new course called Consuming Services in Python for like REST services and SOAP services and working with all sorts of data on the wire basically from Python.

18:51 - Wow, that'll be interesting.

18:52 - Yeah, it's fun.

18:53 So I worked on that all weekend and I'm making good progress.

18:55 - Now, didn't you announce like some extra thing that you guys were giving away with the last course?

19:01 - Oh yeah, with the Python for Entrepreneurs course?

19:03 - Yeah, we're partnering with DigitalOcean and PyCharm.

19:07 And so everybody who takes the Python for Entrepreneurs course gets $50 of credit for DigitalOcean for new customers and about something to the effect of $25 worth of PyCharm licenses.

19:20 So it's pretty cool.

19:21 Like the course costs $59, but you get like $75 worth of stuff for free just by signing up.

19:28 So it's really cool.

19:29 I'm super happy to be able to partner with those guys.

19:32 - And I like that you've started to do a highlight page to highlight projects that people have finished that have gone through the course.

19:39 - Yeah, yeah, I added a, yeah, so what I wanna do is all the students that are taking classes with me, I wanna make sure that if they build something they're proud of, they can share it with other people and let everyone know.

19:49 So I think it's training.talkpython.fm/showcase and people are starting, I got a few more submissions now, starting to submit things and I gotta get them entered there so people are creating their websites or their businesses or just open source projects, and those will be featured there.

20:03 - And if anybody's curious, this wasn't a canned plug.

20:06 I'm actually just interested in what Michael's up to.

20:08 - Thanks, Brian.

20:10 Yeah, I appreciate it.

20:11 It's cool stuff.

20:12 It's really fun to be working on this.

20:13 You know, it's like, you know, my life is a dream.

20:15 I get to work on all these things all day long.

20:17 It's fabulous. - Awesome.

20:18 Well, I enjoyed talking with you again this week.

20:20 - Yeah, as always, and we will be back next week with another round of Python goodness for everyone.

20:26 - Thanks a lot.

20:27 - Yeah, bye, Brian.

20:28 Bye, everyone.

20:30 Thank you for listening to Python Bytes.

20:31 Follow the show on Twitter via @pythonbytes.

20:34 That's Python Bytes as in B-Y-T-E-S.

20:37 And get the full show notes at pythonbytes.fm.

20:40 If you have a news item you want featured, just visit pythonbytes.fm and send it our way.

20:44 We're always on the lookout for sharing something cool.

20:48 On behalf of myself and Brian Auchin, this is Michael Kennedy.

20:51 Thank you for listening and sharing this podcast with your friends and colleagues.

Back to show page