forked from mikeckennedy/talk-python-transcripts
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy path012_Deep_dive_into_Modules_and_Packages.txt
1952 lines (976 loc) · 60.6 KB
/
012_Deep_dive_into_Modules_and_Packages.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
00:00:00 Quick, what's the difference between a module, a package, and packaging in Python?
00:00:05 Okay, maybe you should listen to this episode of Talk Python to Me, number 12 with David Beasley.
00:00:10 It's all about packages, and it was recorded Monday, May 27, 2015.
00:00:15 Hello, and welcome to Talk Python to Me, a weekly podcast on Python, the language, the libraries, the ecosystem, and the personalities.
00:00:49 This is your host, Michael Kennedy.
00:00:51 Follow me on Twitter, where I'm @mkennedy, and keep up with the show and listen to past episodes at talkpythontome.com.
00:00:58 This episode, we'll be talking to David Beasley about the internals of modules and packages in Python.
00:01:05 I'm thrilled to tell you that this episode is brought to you by Codeship.
00:01:09 Codeship is a platform for continuous integration and continuous delivery,
00:01:13 as a service.
00:01:14 I'll talk more about them later in the show.
00:01:16 Please take a moment to check them out at codeship.com, or follow them on Twitter, where they're at Codeship.
00:01:23 This episode is also brought to you by Hired.
00:01:26 Hired has joined Talk Python as a sponsor because they want to help you find your dream job.
00:01:30 Hired is built specifically for developers looking for new opportunities.
00:01:34 You can sign up and expect to get five offers within the first week, and salary and equity presented right up front.
00:01:42 Check them out and get a very special offer at Hired.com slash Talk Python To Me.
00:01:47 Think them on Twitter, where they're at Hired underscore HQ.
00:01:51 The last show on Computer Vision with Adrian Rosebrock, that was show number 11,
00:01:56 we talked about a laptop sticker from years back, which really inspired him.
00:02:00 It said, Python will save the world.
00:02:03 I don't know how, but it will.
00:02:04 Well, I thought that was a great sticker, and I tracked down a picture of it for you.
00:02:08 Check it out at bit.ly slash Python save.
00:02:11 I invited David to come on the show and talk about this subject after watching his tutorial
00:02:17 online at PyCon 2015.
00:02:19 Remember, I have this list of my essential 30 presentations from PyCon 2015,
00:02:25 and of course, David's is on it.
00:02:27 Check it out at bit.ly slash PyCon 2015 MK.
00:02:32 Now, let's get to the interview.
00:02:34 Let me introduce David.
00:02:37 David Beasley is an independent software developer, teacher, and book author living in Chicago.
00:02:42 He primarily works on programming tools and teaches programming courses for software developers,
00:02:47 scientists, and engineers.
00:02:48 He's the author of the Python Essential Reference and Python Cookbook.
00:02:53 David, welcome to the show.
00:02:55 Hi, how are you?
00:02:57 I'm doing fantastic.
00:02:58 Thank you for being on the show.
00:02:59 I'm really glad to have you here.
00:03:01 Okay, thanks for having me.
00:03:02 Yeah, you bet.
00:03:03 I saw your packages and modules talk at PyCon 2015, and I thought it was really interesting.
00:03:10 Okay, you survived that.
00:03:12 I did survive.
00:03:13 Unfortunately, my wife was traveling, and we have small kids, so I saw it remotely over YouTube.
00:03:18 But I wish I was able to be there.
00:03:21 So I sort of saw it.
00:03:23 But yeah, it was really, really interesting.
00:03:25 You didn't put it on for the kids?
00:03:26 Oh, my kids love it when I put on Python instructional videos for them.
00:03:30 Yeah, they're like, Dora the Explorer, forget it.
00:03:32 Give me something to do with, like, async Python.
00:03:36 I'll do that.
00:03:37 Yeah, yeah.
00:03:38 Good.
00:03:39 So we're going to talk a lot about packages and other stuff that you've got going on as well.
00:03:43 But let's start at the beginning.
00:03:45 Where did you get started in programming and Python and all that stuff?
00:03:50 So I got started, it's hard for me to believe, but about 19 years ago with Python.
00:03:55 I was actually using it in the context of scientific computing.
00:04:00 So around that time, I was trying to figure out some way to script scientific software written in C.
00:04:05 And kind of, for lack of a better description, was trying to create my own version of MATLAB, actually.
00:04:11 So...
00:04:12 What kind of science was it?
00:04:13 Well, I was doing molecular dynamics, material science, sort of on supercomputers and had done a lot of C programming for that.
00:04:21 And, you know, kind of the biggest problem that we had in that project was not the scientific computing part of it, but it was everything else.
00:04:29 Like just moving files around and, like, ripping data formats apart.
00:04:34 Kind of like all the annoying day-to-day stuff.
00:04:37 And so I was looking for some way to kind of solve that problem.
00:04:41 Prior to discovering Python, I had actually written my own scripting language, for which I was sort of resoundingly flamed at graduate school.
00:04:50 They were sort of saying, Dave, like, why are you making your own programming language?
00:04:54 Why don't you just use Tickle or some, you know, some existing thing?
00:04:58 And then I had read about Python in a, I think, an article in Computers and Physics or something like that.
00:05:04 And just said, oh, I'm going to go check that out.
00:05:07 Yeah.
00:05:07 And you're like, hey, somebody else wrote a scripting language.
00:05:10 Look at that.
00:05:10 Yeah, it is way better than mine.
00:05:12 You know, the syntax was fairly similar.
00:05:15 So...
00:05:15 Did yours have white space?
00:05:16 Like significant white space?
00:05:18 I did not have significant white space.
00:05:20 But I didn't have dollar signs and other kind of, you know, crazy things either.
00:05:24 So it was...
00:05:25 Okay, so you started out in the scientific world.
00:05:27 And, you know, I think Python now is super big for scientific programming.
00:05:32 How was it back then?
00:05:33 Back then, it was pretty radical.
00:05:35 I mean, this was like 96.
00:05:38 And if you go back that far, there were basically no tools available.
00:05:43 I mean, or it's very minimal tools.
00:05:45 And then a lot of the...
00:05:46 A lot of what you faced was pushback.
00:05:49 Because people thought, well, you know, can't write real software in a scripting language.
00:05:55 You know, why are you fooling around with this scripting language?
00:05:58 This thing.
00:05:59 Instead of coding in C++.
00:06:01 Right.
00:06:01 Especially if it's computational, right?
00:06:03 Yeah.
00:06:04 I mean, I actually got quite a bit of negative response.
00:06:08 Because I was running interactive Python on a supercomputer.
00:06:11 And people were like, you're just burning up, like, hundreds of dollars of CPU cycles just typing at the keyboard at this prompt.
00:06:20 You know, like, what are you doing?
00:06:22 And they didn't really realize that doing that was actually saving us huge amounts of time later.
00:06:28 You know, like, preventing us from running bad simulations or taking problems that used to take, you know, like, 50 hours and reducing it down to 15 minutes.
00:06:37 That kind of stuff.
00:06:39 But, yeah, it was a lot of pushback.
00:06:41 That's really funny.
00:06:42 It's interesting how people can focus on the wrong things.
00:06:45 You know, like, oh, we've got to have another server for that.
00:06:47 Or, you know, otherwise we'd have to have another person.
00:06:50 Or something like this, right?
00:06:51 I actually got quite a few people sort of mad in talks.
00:06:56 I gave some talks about this, and every now and then I'd get a, you know, like a question, like, how did you get permission from management to do this project?
00:07:03 And I would just say, well, we didn't ask.
00:07:06 They didn't explicitly forbid it, so it's permitted.
00:07:09 Yeah, so it was very much kind of a, you know, I don't know whether hidden project.
00:07:15 It was not really an approved project.
00:07:17 Yeah, a guerrilla project, maybe.
00:07:18 Project.
00:07:19 Although, you know, the people involved, I mean, at the time I was, you know, I was doing this work in Los Alamos.
00:07:24 You know, there was a group in Livermore National Lab kind of doing the same things.
00:07:29 And a lot of that early work is sort of led to kind of the precursors of things like NumPy and the SciPy tools that people are using now.
00:07:37 So, yeah.
00:07:38 Think of how the world would have been different if you had that to start from, right?
00:07:43 It would have made a big difference for you guys, I'm sure.
00:07:45 You've got to start somewhere, I guess.
00:07:47 Somebody has to shake things up, I guess.
00:07:51 Yeah, well, that's really cool.
00:07:53 What are you doing today with Python?
00:07:54 So, right now, well, a variety of things.
00:07:58 I'm teaching classes.
00:07:59 I'm also involved with a startup company.
00:08:02 That's been a little bit secretive.
00:08:03 But I'm technically co-founder of a startup doing some education tech stuff.
00:08:09 And I'm actually coding with that.
00:08:12 I'm doing a lot of just back-end web programming, databases, SQLAlchemy, things like that.
00:08:19 I'm supposed to be working on a new version of the Python Essential Reference book.
00:08:24 I haven't really started that yet.
00:08:27 I hope my editor doesn't listen to this.
00:08:29 But, you know, that's coming soon as well.
00:08:32 Well, we'll make it up to him by promoting the book in the show notes or something like that.
00:08:38 How's that?
00:08:38 Right, right.
00:08:39 Yeah, the tech education scene is insanely hot right now.
00:08:43 And VC and people, it's really an amazing place to be.
00:08:47 And I'm doing a little bit of stuff there as well.
00:08:49 And just, you know, knowing what's going on there is just amazing right now.
00:08:52 Yeah, a lot of crazy, crazy things there.
00:08:55 I guess if it wasn't crazy, it wouldn't be, I don't know, it wouldn't be worth doing, I guess.
00:08:59 Exactly.
00:08:59 It'd be like going back to high school instead.
00:09:01 Right, right.
00:09:01 Yeah.
00:09:09 CodeChip is a hosted continuous delivery service focused on speed, security, and customizability.
00:09:16 You can set up continuous integration in a matter of seconds and automatically deploy when your tests have passed.
00:09:21 CodeChip supports your GitHub and Bitbucket projects.
00:09:25 You can get started with CodeChip's free plan today.
00:09:27 Should you decide to go with a premium plan, Talk Python listeners can save 20% off any plan for the next three months by using the code TALKPython.
00:09:36 All caps, no spaces.
00:09:38 Check them out at CodeChip.com and tell them thanks for sponsoring the show on Twitter where they're at CodeChip.
00:09:45 Awesome.
00:09:53 So what inspired you to do a tutorial on packages?
00:09:56 I mean, that seems like, you know, doing a tutorial on for loops or something.
00:10:01 But obviously, you know, it was a two, three hour tutorial and it was really great, I thought.
00:10:06 So, but, but what started you on this path?
00:10:09 Yeah.
00:10:09 A three hour tutorial on packages.
00:10:11 Import.
00:10:12 Okay.
00:10:13 Now you can just contemplate this for two and a half hours to get the zen of it, right?
00:10:17 Yeah.
00:10:17 So I guess it was like probably a, so packages, I mean, modules and packages.
00:10:22 I mean, one part of it, it's something that, that I've rarely thought about.
00:10:26 You know, it's like you learn Python, you know, 19 years ago.
00:10:29 I was like, yeah, there's the import statement.
00:10:31 And yeah, sometimes you have to fiddle with the system path and, and, and, and, and so forth.
00:10:36 You know, you sort of, you sort of get over it.
00:10:39 But in, you know, in teaching a lot of classes and working with other people, it seems like
00:10:45 the import statement is like this no end of just hell for people.
00:10:49 Like, you know, I'll be teaching a class somewhere and then, you know, kind of walk around and I'll
00:10:54 see somebody struggling.
00:10:55 And they're like, yeah, I've been fooling around with this for like half an hour or something.
00:10:59 And first, first of all, like, why didn't you get my attention earlier?
00:11:02 But then you, but then you go over there and you realize that they're just fiddling around with
00:11:05 some stupid thing with the import statement.
00:11:07 Like, you know, the file is named wrong or that it's in the wrong directory or they're trying to
00:11:12 reload something or.
00:11:13 It could be subtle as well.
00:11:16 It's like, you might have a number as the starting name for your directory or something like that.
00:11:19 Right.
00:11:20 Yeah.
00:11:20 Subtle things or, you know, or like, you know, you're working with people on a project, you know,
00:11:25 it's like four o'clock in the afternoon and somebody's like, why are they cursing so much
00:11:28 over there?
00:11:29 You know, I go walk over, walk over there and they're like cursing about like why Python isn't
00:11:33 running their code correctly only to, only to realize that they, they've forgotten that
00:11:38 module loading.
00:11:39 It's a one-time operation.
00:11:40 Right.
00:11:41 Exactly.
00:11:41 It's not reloading there.
00:11:43 I changed it, but it's not changing.
00:11:45 Loading.
00:11:46 So, so, so part of it was just, it's like, I should do something on modules and packages to
00:11:51 try and like, I don't know, clear confusion maybe.
00:11:54 But I think, I think also part of it is the, the whole module system in Python has,
00:11:59 has been basically rewritten.
00:12:01 I don't know whether it's like a, it's almost like a from scratch rewrite where all of the
00:12:05 mechanics of it have actually been pulled out of C and put up into the Python level.
00:12:11 So almost, almost all the internals of modules and imports and packages and stuff is, is sort
00:12:16 of way more accessible now.
00:12:19 And so I thought it'd be kind of, kind of interesting to, to, to look at that as well,
00:12:23 just sort of take like a totally modern take on it and just see, you know, like what's going
00:12:29 on with modules and packages.
00:12:30 Yeah.
00:12:31 Before we get into the details, I, I'm sure everybody kind of gets a sense of what are modules,
00:12:36 what are packages, but just to make sure we're all on the same page, I could import things.
00:12:41 And sometimes I import a module and sometime I'm importing a package.
00:12:45 What's the deal there?
00:12:46 Well, I mean, a module, you know, maybe a simple view of it is, you know, people think of it
00:12:51 as like a single file, you know, single, single, file of source code, package would
00:12:57 be more of a collection of files.
00:12:59 You know, maybe larger application or a framework or, or, or something like that.
00:13:05 I think one thing that can be a little bit muddled is sometimes things look like modules
00:13:09 when in fact they're big packages and, you know, it can be kind of muddled sometimes.
00:13:16 So it seems to me like with modules, I'm getting just a file, right?
00:13:19 Like, Hey, I've written some code over here.
00:13:21 I've written some code over there.
00:13:22 Let me grab the code in the one file and use it.
00:13:25 And with packages, it seems like there's a lot more going on potentially, right?
00:13:31 I've got the, the under, under init.py and I can sort of declare this all under, under all
00:13:37 variable.
00:13:38 Can you maybe talk a little bit about the structure of a package and why you might build one?
00:13:43 Okay.
00:13:43 Well, I think, you know, the, so, so one thing with packages is, well, let me, let me back
00:13:49 up for a second here.
00:13:50 I think like, like if you're thinking about module, typically you're thinking about something
00:13:54 kind of small, like a single file, maybe a small library, you know, maybe, maybe not a
00:14:00 huge amount of functionality, but you know, it's just like a, you know, like just a single
00:14:04 point of, of, of code that, that is very easy to use.
00:14:08 I think with a package, you're getting into something much more complicated, you know, it's
00:14:12 like much larger code base.
00:14:14 And part of what you, what you want to avoid is just putting all of your code in one file.
00:14:19 Like nobody wants to have a file where it's 50,000 lines of Python code in there.
00:14:23 Especially not if you inherit that file.
00:14:26 Yeah.
00:14:26 Well, yeah.
00:14:26 So especially if it's given to you.
00:14:28 So with a, you know, with a package, you're really thinking about kind of breaking up your
00:14:31 code into different, you know, different sub modules, different parts of the application
00:14:36 and so forth.
00:14:36 It's more of an organizational, you know, organizational tool than, than anything.
00:14:41 Also, also just keeping your code separate from the, from the rest of the Python universe,
00:14:46 you know, avoiding like naming clashes with other bits of, bits of code.
00:14:51 I think the thing that gets complicated with a package is once you do that, you know, once
00:14:56 you separate your code out into multiple files, you have to start worrying about all sorts of
00:15:02 side issues.
00:15:03 Like, like how do those files interact with each other?
00:15:06 You know, like how does one sub module refer to another sub module or how do they, you know,
00:15:10 combine pieces together and, and so forth.
00:15:14 And that's actually where a lot of the, I think a lot of the tricky parts of packages come into
00:15:17 play is that just, you know, relationships within the package causes a lot of complexity that
00:15:23 you wouldn't see with a single, you know, a single file module.
00:15:27 Right.
00:15:27 I totally agree.
00:15:28 There's a lot of stuff you can do in that init file to sort of make it explicit about
00:15:34 what you want to export.
00:15:35 And I think of these packages as like reusable libraries that you're going to grab and they
00:15:40 have a bunch of functionality.
00:15:41 Whereas modules, I kind of feel like, Hey, I'm just, I'm reusing a file that I wrote.
00:15:44 This file wants access to that part of my code.
00:15:46 So I pull that in.
00:15:47 And so you talked about a lot of cool tips and tricks that you can kind of do there.
00:15:53 You talked about like the all, all variable.
00:15:56 You talked about importing sub modules in the main level module and stuff like that.
00:16:02 Yeah.
00:16:02 I think one of the, one of the things that, it might be more of a personal complaint,
00:16:06 but you know, when you use packages a lot, you can sometimes get a pull, like a huge number
00:16:11 of imports that start showing up in your code where, you know, instead of just importing a
00:16:16 single file, all of a sudden you're, you're putting in like 20 import statements and not a,
00:16:22 I'm not a huge fan of doing that.
00:16:24 So, you know, these are knit files that you sometimes see in packages.
00:16:27 I mean, those, one, one use of those is, is to basically coalesce the pieces into one
00:16:33 place.
00:16:34 And, you know, sometimes you can cut down on the number of imports that you have to do.
00:16:38 Right.
00:16:39 Maybe you import the top level thing, like import SQLAlchemy.
00:16:43 And then you could say SQLAlchemy dot and get to the sub packages, something like that.
00:16:47 Right.
00:16:47 Right.
00:16:48 Right.
00:16:48 You don't have to worry about, you know, how they organize that under the covers of SQLAlchemy.
00:16:53 It could be, you know, spread across, you know, 20 different files for all you care.
00:16:56 Yeah, exactly.
00:16:58 But you have to manually do that in the init file, right?
00:17:00 In order to sort of force the top level import to bring in the sub packages.
00:17:04 Is that correct?
00:17:04 You, you have to take some steps there.
00:17:07 Yeah.
00:17:07 I mean, it's, you know, if you're the author of a package, you have to figure out
00:17:10 some way to, some way to do that.
00:17:12 I've done some tricks involving decorators doing that.
00:17:16 Like, you know, they, being one of the applications I'm working on now, I, I mean, I
00:17:21 have a, I have a decorator I can put on functions that will automatically sort of hoist them up
00:17:27 into the init file.
00:17:28 So, I mean, there are, there are tricks that you can do to, to, to do that kind of thing.
00:17:33 That's really cool.
00:17:34 Is that a decorator that you wrote?
00:17:35 I just cooked it up myself.
00:17:37 Yeah.
00:17:37 Is it on GitHub or something?
00:17:38 It's, I don't know if it's on GitHub.
00:17:41 it is in the tutorial though.
00:17:43 Right.
00:17:44 Okay.
00:17:44 A module.
00:17:45 It's somewhere in that tutorial.
00:17:46 So.
00:17:46 Nice.
00:17:47 one thing that I thought was interesting that you spoke about the tutorial was that
00:17:52 structure guidelines, the PEP 8 guidelines sometimes make your code more brittle in packages
00:17:57 than otherwise should be.
00:17:58 Oh yeah.
00:18:01 Yes.
00:18:01 One of the things with PEP 8, I think is it predates some of the work that went on in
00:18:06 packages.
00:18:07 I mean, you know, like, like PEP 8 sort of talks that you should, you know, well, it
00:18:11 sort of says things like, oh, you should just do like an absolute import from kind of a top
00:18:16 level package name.
00:18:18 So if we were writing like the, the, a package called math and it had a, a class called library,
00:18:24 calculator, something like that, we might in the init file say import math dot calculator
00:18:30 where math is the actual name of the package.
00:18:32 Right.
00:18:33 I don't know that I'd use math as the best.
00:18:35 Okay.
00:18:36 Yeah.
00:18:36 So I'm lacking creativity.
00:18:38 Yeah.
00:18:38 Pick another example.
00:18:39 Yeah.
00:18:39 I, it's, it's more concerning things like imports within the package.
00:18:43 Like if you, you know, if you had a package where, you know, you had a, like there was a
00:18:48 sub module called graphics and there was another module called data or something like that.
00:18:53 You would end up importing from the top level name down.
00:18:57 So if you're, if your package was named spam, for instance, you'd have to say, you know, import
00:19:01 spam dot graphics or import spam dot data.
00:19:04 And you would, you would do that within the package, within the package itself.
00:19:10 but the thing that I don't, that I don't like about that is it ends up hard coding the
00:19:14 package name.
00:19:15 Right.
00:19:16 For example, if you rename the package, what happens?
00:19:18 Well, then you have to go change all of your code.
00:19:20 Great.
00:19:22 See, that's the part I don't like.
00:19:23 Is there a fix?
00:19:23 Well, you could use, one of the things that you can use are these package relative imports.
00:19:28 This is this form.
00:19:30 I, it's, I'm actually surprised how many people have not seen it sometimes, but it's, what it
00:19:35 looks like is you have these dots where you would say something like, oh, from dot import
00:19:39 graphics or from dot import data.
00:19:41 And the, the dot is just telling Python that you want to load relative to your current location,
00:19:47 for instance.
00:19:48 Right.
00:19:49 Exactly.
00:19:49 So then it doesn't matter what you call the package.
00:19:52 Right.
00:19:52 Right.
00:19:52 Right.
00:19:52 That's really excellent.
00:19:53 I'm sort of thinking about things like versioning and stuff.
00:19:56 I mean, it's, you know, I could imagine situations where, you know, I have code where I, I need
00:20:01 to have an old version of some package coexisting with a modern version of the same package.
00:20:06 You know, one of the ways that you could deal with that is just to rename the old one.
00:20:11 Right.
00:20:12 Exactly.
00:20:13 Exactly.
00:20:13 Graphics dot, graphics dot old or underscore old or something like that.
00:20:16 Right.
00:20:17 Right.
00:20:17 Right.
00:20:17 As long as you don't have the package name hard coded in there, that, that, that all works
00:20:22 fine.
00:20:23 So.
00:20:23 Yeah.
00:20:24 I think it's, I think it's really good advice.
00:20:26 I propose we amend PEP 8 to have the package relative imports.
00:20:31 I wonder when that part was written in PEP 8.
00:20:34 I mean, it's, I have to, I hate to admit this on like a podcast, but I'm sort of a flagrant
00:20:40 PEP 8 violator.
00:20:42 Don't worry, you, nobody can see the code, so they won't really know about it.
00:20:45 It's fine.
00:20:46 Yeah.
00:20:46 It's, I, I, I don't mean to violate it, but I think it's, you know, PEP 8 comes after
00:20:53 the point at which I got involved with Python.
00:20:55 So it never really entered my consciousness of something that I would pay attention to.
00:21:00 I mean, it's.
00:21:01 Your style was already set by the time.
00:21:02 Yes.
00:21:02 Yeah.
00:21:03 I haven't, I have enough trouble just with the, people are always giving me a bad time
00:21:08 for using double quotes on all my strings.
00:21:11 Even, you know, I think like, you know, it's, that's fine in Python, but you know, most people
00:21:15 tend to use like the single quote and instead I'm kind of locked into double quotes just because
00:21:20 I've done so much C programming.
00:21:21 Exactly.
00:21:22 The, the single quote in C doesn't do the same thing.
00:21:25 I just can't get out of C programming, even though that's not my day-to-day job.
00:21:29 So yeah, I have the same problem.
00:21:31 I'm kind of back and forth on it.
00:21:41 This episode is brought to you by hired hired.
00:21:46 Hired is a two-sided curated marketplace that connects the world's knowledge workers to the
00:21:52 best opportunities.
00:21:53 Each offer you receive has salary and equity presented right up front, and you can view the
00:21:58 offers to accept or reject them before you even talk to the company.
00:22:02 Typically candidates receive five or more offers in just the first week.
00:22:06 And there are no obligations ever sounds pretty awesome.
00:22:10 Doesn't it?
00:22:10 Well, did I mention there's a signing bonus?
00:22:12 Everyone who accepts a job from hired gets a $2,000 signing bonus.
00:22:16 And as talk Python listeners, it gets way sweeter.
00:22:21 Use the link hired.com slash talk Python to me and hired will double the signing bonus to
00:22:27 $4,000 opportunities.
00:22:30 Knocking.
00:22:30 Visit hired.com slash talk Python to me and answer the call.
00:22:34 One thing that goes, you know, hand in hand with packages and trying these things out and
00:22:48 so on is virtual environments.
00:22:50 Do you use those often?
00:22:52 This is going to sound really shocking, but I almost never use them.
00:22:55 Okay.
00:22:56 I don't know why I never use them.
00:23:00 It's like I tend to, when I want to do something custom, I tend to just kind of build Python
00:23:06 myself.
00:23:07 And I might just put it in its own directory somewhere.
00:23:10 I don't know.
00:23:12 You know, that's probably just a bad thing to be doing, but maybe it's more historical.
00:23:16 Just having used Python for a while, that's something that's never been that hard to do.
00:23:22 Right.
00:23:22 Well, you understand it well enough.
00:23:24 You just do what virtual environment does more or less, right?
00:23:26 Yeah.
00:23:27 I do it the inefficient way, you know.
00:23:29 The explicit way.
00:23:31 How's that?
00:23:32 The explicit way.
00:23:33 You just download from source.
00:23:34 Yeah.
00:23:36 One of the things I do like about, well, I think is that I've actually have started using
00:23:41 them a little bit more ever since they got built into Python 3.
00:23:46 So that's actually a big win, I think, for sort of the newer versions of Python is they
00:23:51 have the virtual environment feature kind of just baked into the language.
00:23:55 I think that is really nice.
00:23:58 You know, we could go into a whole side conversation on Python 2 versus Python 3.
00:24:04 Yeah, we probably don't want to go there.
00:24:05 We probably don't want to go there.
00:24:07 But I think it's interesting that things like that are like, hey, here's a little less friction
00:24:11 if you go down the Python 3 path.
00:24:13 I talked with Kenneth Reitz on show number six.
00:24:15 We talked a little bit about that.
00:24:17 And he's like, I think what we really need is a killer feature that only appears in Python
00:24:22 3 in order to get people really to switch.
00:24:24 I mean, there's small gains that are making around like Django switching their documentation
00:24:29 by default to Python 3.
00:24:30 And that made a measurable dent in the world.
00:24:34 But, you know, things like maybe in Python 3, there's no such thing as a global interpreter
00:24:39 lock.
00:24:40 Well, that would be a big win.
00:24:42 I don't know whether that's going to happen anytime soon.
00:24:45 Of course.
00:24:45 You know, possibly with something like PyPy, right?
00:24:48 Or Pyston.
00:24:48 Maybe.
00:24:49 I don't know.
00:24:50 Yeah, maybe.
00:24:50 Anyway, you know, I think it's interesting.
00:24:52 But yeah, so these things like the virtual environment stuff being built into Python 3,
00:24:57 it's really cool.
00:24:58 And it's, you know, one more check in the box of, hey, you know, consider Python 3.
00:25:01 So another thing that you talked about is like splitting modules into a bunch of multiple files.
00:25:08 And I really like to do this.
00:25:10 Like you sort of started out the conversation.
00:25:12 I really, really dislike large files, right?
00:25:16 I would much rather have 10 50 line files than one 500 line file or two 250 line, you know,
00:25:24 whatever the math works out to be.
00:25:25 It's a bunch of smaller files.
00:25:27 It seems like Python kind of dislikes me doing this.
00:25:30 You know, it's the more I break stuff into small files, the more I have to put a whole bunch of imports at the top.
00:25:39 Right.
00:25:39 And that's that that is one thing.
00:25:40 I mean, you can definitely solve that with those init file.
00:25:43 I mean, if you put it all in a package, you can kind of stitch it all back together in a knit.
00:25:48 Right.
00:25:49 So I could have like all those in a subfolder, which has an under under init.
00:25:53 So it's a sub package.
00:25:55 And in that under under init, I would have like import and say from my class import my class or whatever.
00:26:04 And then they would sort of drop into that top level namespace.
00:26:07 Right.