r/learnpython • • Jul 12 '20

Jupyter Notebooks!

Jupyter Notebooks are the tool that turned the tables for me learning python. I'm just here to evangelize them. You can run your code line by line and debug as you go in a comfortable web browser interface. It's an excellent tool, and it made it so much more efficient to learn. Check it out.

425 Upvotes

127 comments sorted by

View all comments

57

u/[deleted] Jul 12 '20

It’s great for experimenting. But beware of the way it’ll influence the way you code, especially if you’re a beginner. I’ve seen people overuse it to the point that they fail to write efficient code because their code just works well in their notebook. However, none of it becomes reproducible outside that notebook. In order to make it useful beyond that notebook, you do need to at least convert the code into more useful format using classes, functions, etc .

9

u/StillTop Jul 12 '20

glad someone mentioned this, part of the reason why i use pycharm with anaconda distribution

5

u/synthphreak Jul 12 '20 edited Jul 12 '20

Can you provide any concrete examples of someone’s coding style being influenced by the affordances of the notebook environment in this way?

9

u/[deleted] Jul 12 '20

Yes. I once had a coworker who wrote mostly in notebooks. When it came to making production code, they struggled to get everything put together. They ran into a problem where they didn’t know how to encapsulate half their code in something reproducible because the notebook had become very messy and hard for them to even understand what was going on. I’m sure if they were given more time to figure it out, they would have. But we didn’t always have that time, so we had to sit down together and write it from scratch- this time in a script as a class from the start. They were so not used to the process that I ultimately had to do it myself. They still have similar habits now, but they’re trying to write more in scripts. It just causes ineffeciency for everyone if one team member is inefficient.

I use notebooks myself, but I actually write my classes and functions in notebook cells first and then immediately paste those into scripts in an appropriate manner. I then import those classes and functions instead of having them in my notebook. This is to ensure my notebook doesn’t become one giant hot mess.

5

u/MurderMelon Jul 12 '20

I actually write my classes and functions in notebook cells first and then immediately paste those into scripts in an appropriate manner. I then import those classes and functions instead of having them in my notebook.

This is exactly what I do and it's worked perfectly for me. I basically just use notebooks to prototype and debug. Then I package each discrete part into its own module, which I will then call from a master notebook (which usually ends up getting converted to a script, since all the dependencies are external anyway)

2

u/nomos Jul 12 '20

I do this too, but still havent found the sweet spot for when to move code over to modules. If that function you move over isnt quite debugged fully, at a minimum you have to restart the kernel for your changes to take place as opposed to rerunning a cell, which can take time. Even worse when you are executing code in a remote environment and you have to pull your local changes back into that environment after making changes. Agree that this is still better than having all code in notebook, though.

1

u/Socrato Jul 12 '20

For me, and maybe this isn't good practice, I try to encapsulate the notebook itself. I tend to create many directories, and my notebooks are "<function name> DEV" and I import what's necessary, usually my class I've developing along with the entire import list in those scripts, and then develop the new function in that. Once it's done, I save that notebook and move that over to my class main script. Then I go back to my "master" class testing notebook and make sure everything runs correctly. Then for the next function/process I start a new notebook.

Not sure how great that is but it's been pretty successful for all my projects which tend to be backend data processing and etl type things. I don't write any web code for production but I populate lots of tables that are used in prod.

4

u/synthphreak Jul 12 '20

I imagine that at a minimum, periodically restarting the kernel and running all cells will go some way toward fixing this problem.

As a notebook grows in size, complexity, and age, it becomes easy to lose track of what’s defined and where, what isn’t defined, and in what order cells need to run to work. But periodically rerunning everything and making sure it still runs will help keep your organization in check, forcing you to write your notebooks in a more “script-like” manner.

But TBH I really don’t use notebooks that much. So I’m curious what others think about this.

2

u/g0lem Jul 12 '20

You've pretty much hit the nail on the head. The problems you mention are quite hard or at least frustrating for a beginner to debug. Notebooks can be used correctly if you often refactor (groups of) cells in functions or simply delete them (if not useful for the end goal), but on the other hand, there are good debuggers in Pycharm and other IDEs. I don't think there are definitive arguments for and against one particular tool/environment for development. However, notebooks shouldn't grow too much in size/complexity/age, because at that point they need to be refactored/packaged in modules.

2

u/zylog413 Jul 12 '20

Most IDEs have the ability to jump to function and class definitions, and to have them open in a separate file in a separate tab/window. This can help encourage code reuse and abstractions. Without this, there can be a tendency to just dump the definitions right into the current notebook, and if the notebook becomes too long, do away with functions and classes entirely.

Also without functions, you probably don't end up with unit tests for them.