, myself included, begin their coding journey utilizing a Jupyter Pocket book. These information have the extension .ipynb, which stands for Interactive Python Pocket book. Because the extension identify suggests, it has an intuitive and interactive person interface. The pocket book is damaged down into ‘cells’ or small blocks of separated code or markdown (textual content) language. Outputs are displayed beneath every cell as soon as the code inside that cell has been executed. This promotes a versatile and interactive setting for coders to construct their coding expertise and begin engaged on knowledge science tasks.
A typical instance of a Jupyter Pocket book is under:

This all sounds nice. And don’t get me fallacious, to be used circumstances similar to conducting solo analysis or exploratory knowledge evaluation (EDA), Jupyter Notebooks are nice. The problems come up once you ask the next questions:
- How do you flip a Jupyter Pocket book into code that may be leveraged by a enterprise?
- Are you able to collaborate with different builders on the identical challenge utilizing a model management system?
- How are you going to deploy code to a manufacturing setting?
Fairly quickly, the constraints of solely utilizing Jupyter Notebooks inside a industrial context will begin to trigger issues. It’s merely not designed for these functions. The overall resolution is to organise code in a modular trend.
By the top of this text, you need to have a transparent understanding of methods to construction a small knowledge science challenge as a Python program and admire some great benefits of transitioning to a programming method. You’ll be able to try an instance template to complement this text in my github right here.
Disclaimer
The contents of this text are primarily based on my expertise of migrating away from solely utilizing Jupyter Notebooks to jot down code. Do notebooks nonetheless have a goal? Sure. Are there other ways to organise and execute code past the strategies I focus on on this article? Sure.
I wished to share this info to assist anybody desirous to make the transfer away from notebooks and in the direction of writing scripts and packages. If I’ve missed any options of Jupyter Notebooks that mitigate the constraints I’ve talked about, please drop a remark!
Let’s get again to it.
Programming: what’s the large deal?
For the aim of this text, I’ll be specializing in the Python programming language as that is the language I exploit for knowledge science tasks. Structuring code as a Python program unlocks a variety of functionalities which are troublesome to attain when working solely inside a Jupyter Pocket book. These advantages embrace collaboration, versatility and portability – you’re merely capable of do extra together with your code. I’ll clarify these advantages additional down – stick with me somewhat longer!
Python packages are usually organised into modules and packages. A module is a python script (information with a .py extension) that incorporates python code which might be imported into different information. A bundle is a listing that incorporates python modules. I’ll focus on the aim of the file __init__.py
later within the article.

Anytime you import a python library into your code, similar to built-in libraries like os
or third-party libraries like pandas
, you might be interacting with a python program that’s been organised right into a bundle and modules.
For instance, let’s say you wish to use the randint perform from numpy. This perform permits you to generate a random integer primarily based on specified parameters. You would possibly write:
from numpy.random import randint
Let’s annotate that import assertion to point out what you’re really importing.

On this occasion, numpy
is a bundle; random
is a module and randint
is a perform.
So, it seems you most likely work together with python packages frequently. This poses the query, what does the journey seem like in the direction of turning into a python programmer?
The nice transition: the place do you even begin?
The trick to constructing a practical python program is all within the file construction and organisation. It sounds boring nevertheless it performs a brilliant essential half in setting your self up for fulfillment!
Let me use an analogy to elucidate: each home has a drawer that has nearly all the things in it; instruments, elastic bands, medication, your hopes and goals, the lot. There’s no rhyme or motive, it’s a dumping floor of nearly all the things. Consider this as a Jupyter Pocket book. This one file usually incorporates all levels of a challenge, from importing knowledge, exploring what the info seems to be like, visualising developments, extracting options, coaching a mannequin and so forth. For a challenge that’s destined to be deployed on a manufacturing system or co-developed with colleagues, it’s going to trigger chaos. What’s wanted is a few organisation, to place all of the instruments in a single compartment, the drugs in one other and so forth.
An effective way to try this with code is to make use of a challenge template. One which I exploit ceaselessly is the Cookie Cutter Information Science template. You’ll be able to create a complete listing to your challenge with all of the related information wanted to do absolutely anything in just a few easy operations in a terminal window – see the hyperlink above for info on methods to set up and run Cookie Cutter.
Under are a few of the key options of the challenge template:
- bundle or src listing — listing for python scripts/modules, geared up with examples to get you began
- readme.md — file to explain utilization, setup and methods to run the bundle
- docs listing — containing information that allow seamless autodocumentation
- Makefile— for writing OS ambivalent bespoke run instructions
- pyproject.toml/necessities.txt — for dependency administration

High tip. Make certain to maintain Cookie Cutter updated. With each launch, new options are added based on the ever-evolving knowledge science universe. I’ve learnt fairly just a few issues from exploring a brand new file or function within the template!
Alternatively, you should use different templates to construct your challenge similar to that offered by Poetry. Poetry is a bundle supervisor which you should use to generate a challenge template that’s extra light-weight than Cookie Cutter.
One of the simplest ways to work together together with your challenge is thru an IDE (Built-in Growth Atmosphere). This software program, similar to Visible Studio Code (VS Code) or PyCharm, embody quite a lot of options and processes that allow you to code, check, debug and bundle your work effectively. My private desire is VS Code!
From cells to scripts: let’s get coding
Now that now we have a improvement setting and a properly structured challenge template, how precisely do you write code in a python script for those who’ve solely ever coded in a Jupyter Pocket book? To reply that query, let’s first take into account just a few industry-standard coding Finest Practices.
- Modular — comply with the software program engineering philosophy of ‘Single Accountability Precept’. All code must be encapsulated in features, with every perform performing a single activity. The Zen of Python states: ‘Easy is best than advanced’.
- Readable — if code is readable, then there’s a superb likelihood it will likely be maintainable. Make sure the code is stuffed with docstrings and feedback!
- Fashionable — format code in a constant and clear means. The PEP 8 tips are designed for this goal to advise how code must be introduced. You’ll be able to set up autoformatters similar to Black in an IDE in order that code is robotically formatted in compliance with PEP 8 every time the python script is saved. For instance, the appropriate stage of indentation and spacing will likely be utilized so that you don’t even have to consider it!
- Versatile — if code is encapsulated into features or courses, these might be reused all through a challenge.
For a deeper dive into coding finest apply, this text is a improbable overview of rules to stick to as a Information Scientist, make sure to test it out!
With these finest practices in thoughts, let’s return to the query: how do you write code in a python script?
Module construction
First, separate the completely different levels of your pocket book or challenge into completely different python information. And ensure to call them based on the duty. For instance, you might need the next scripts in a typical machine studying bundle: knowledge.py
, preprocess.py
, options.py
, practice.py
, predict.py
, consider.py
and so forth. Relying in your challenge construction, these would sit inside the bundle
or src
listing.
Inside every script, code must be organised or ‘encapsulated’ right into a courses and/or features. A perform is a reusable block of code that performs a single, well-defined activity. A class is a blueprint for creating an object, with its personal set of attributes (variables) and strategies (features). Encapsulating code on this method permits reusability and avoids duplication, thus retaining code concise.
A script would possibly solely want one perform if the duty is easy. For instance, an information loading module (e.g. knowledge.py
) could solely include a single perform ‘load_data’ which masses knowledge from a csv file right into a pandas
DataFrame. Different scripts, similar to an information processing module (e.g. preprocess.py
) will inherently contain extra duties and therefore requires extra features or a category to encapsulate these duties.

High tip. Transitioning from Jupyter Notebooks to scripts could take a while and everybody’s private journey will look completely different. Some Information Scientists I do know write code as python scripts immediately and don’t contact a pocket book. Personally, I exploit a pocket book for EDA, I then encapsulate the code into features or courses earlier than porting to a script. Do no matter feels best for you.
There are just a few instruments that may assist with the transition. 1) In VS Code, you’ll be able to choose a number of traces, proper click on and choose Run Python > Run Choice/Line in Python Terminal. That is just like working a cell in Jupyter Pocket book. 2) You’ll be able to convert a pocket book to a python script by clicking File > Obtain as > Python (.py). I wouldn’t suggest that method with giant notebooks for worry of making monster scripts, however the possibility is there!
The ‘__main__’ occasion
At this level, we’ve established that code must be encapsulated into features and saved inside clearly named scripts. The following logical query is, how will you tie all these scripts collectively so code will get executed in the appropriate order?
The reply is to import these scripts right into a single-entry level and execute the code in a single place. Inside the context of growing a easy challenge, this entry level is usually a script named major.py
(however might be referred to as something). On the prime of major.py
, simply as you’d import vital built-in packages or third-party packages from PyPI, you’ll import your individual modules or particular courses/features from modules. Any courses or features outlined in these modules will likely be obtainable to make use of by the script they’ve been imported into.
To do that, the bundle listing inside your challenge must include a __init__.py
file, which is usually left clean for easy tasks. This file tells the python interpreter to deal with the listing as a bundle, that means that any information with a .py extension get handled as modules and might subsequently be imported into different information.
The construction of major.py
is challenge dependent, however it should typically be dictated by the mandatory order of code execution. For a typical machine studying challenge, you’d first want to make use of the load_data perform from the module knowledge.py
. You then would possibly instantiate the preprocessor class that’s imported from the module preprocess.py
and apply quite a lot of class strategies to the preprocessor object. You’ll then transfer onto function engineering and so forth till you’ve gotten the entire workflow written out. This workflow would usually be contained or referenced inside a conditional assertion on the backside of major.py
.
Wait….. who talked about something a few conditional assertion? The conditional assertion is as follows:
if __name__ == '__main__':
# add code right here
__name__
is a particular python variable that may have two completely different values relying on how the script is run:
- If the script is run straight in terminal, the interpreter assigns the
__name__
variable the worth'__main__'
. As a result of the assertionif '__name__=='__main__':
is true, any code that sits inside this assertion is executed. - If the script is run as an imported module, the interpreter assigns the identify of the module as a string to the
__name__
variable. As a result of the assertion ifif '__name__=='__main__':
is fake, the contents of this assertion is just not executed.
Some extra info on this may be discovered right here.
Given this course of, you’ll have to reference the grasp perform inside the if '__name__=='__main__':
conditional assertion in order that it’s executed when major.py
is run. Alternatively, you’ll be able to place the code beneath if '__name__=='__main__':
to attain the identical end result.

major.py
(or any python script) might be executed in terminal utilizing the next syntax:
python3 major.py
Upon working major.py
, code will likely be executed from all of the imported modules within the specified order. This is identical as clicking the ‘run all’ button on a Jupyter Pocket book the place every cell is executed in sequential order. The distinction now could be that the code is organised into particular person scripts in a logical method and encapsulated inside courses and features.
You may as well add CLI (command-line interface) arguments to your code utilizing instruments similar to argparse and typer, permitting you to toggle particular variables when working major.py
within the terminal. This supplies a substantial amount of flexibility throughout code execution.
So we’ve now reached the perfect half. The pièce de résistance. The actual explanation why, past having fantastically organised and readable code, you need to go to the trouble of Programming.
The tip recreation: what’s the purpose of programming?
Let’s stroll via a few of the key advantages of shifting past Jupyter Notebooks and transitioning to writing Python scripts as an alternative.

- Packaging & distribution — you’ll be able to bundle and distribute your python program so it may be shared, put in and run on one other laptop. Package deal managers similar to pip, poetry or conda can be utilized to put in the bundle, simply as you’d set up packages from PyPI, similar to
pandas
ornumpy
. The trick to efficiently distributing your bundle is to make sure that the dependencies are managed accurately, which is the place the informationpyproject.toml
ornecessities.txt
are available in. Some helpful sources might be discovered right here and right here. - Deployment — while there are a number of strategies and platforms to deploy code, utilizing a modular method will put you in good stead to get your code manufacturing prepared. Instruments similar to Docker allow the deployment of packages or functions in remoted environments referred to as containers, which might be simply managed via CI/CD (steady integration & deployment) pipelines. It’s price noting that whereas Jupyter Notebooks might be deployed utilizing JupyterLab, this method lacks the flexibleness and scalability of adopting a modular, script-based workflow.
- Model management — shifting away from Jupyter Notebooks opens up the fantastic worlds of model management and collaboration. Model management techniques similar to Git are very a lot {industry} customary and supply a wealth of advantages, offering you employ them appropriately! Comply with the motto ‘incremental modifications are key’ and be sure that you make small, common commits with logical commit messages in crucial language everytime you make practical modifications while growing. This may make it far simpler to maintain monitor of modifications and check code. Right here is a brilliant helpful information to utilizing git as an information scientist.
Enjoyable truth. It’s typically discouraged to commit Jupyter Notebooks to model management techniques as it’s troublesome to trace modifications!
- (Auto)Documentation — everyone knows that documenting code will increase its readability thus serving to the reader perceive what the code is doing. It’s thought of finest apply so as to add docstrings to features and courses inside python scripts. What’s actually cool is that we will use these docstrings to construct an index of formatted documentation of your complete challenge within the type of html information. Instruments similar to Sphinx allow you to do that in a fast and straightforward means. You’ll be able to learn my earlier article which takes you thru this course of step-by-step.
- Reusability — adopting a modular method promotes the reuse of code. There are various frequent duties inside knowledge science tasks, similar to cleaning knowledge or scaling options. There’s little level in reinventing the wheel, so for those who can reuse features or courses with minor modification from earlier tasks, so long as there are not any confidentiality restrictions, then save your self that point! You might need a
utils.py
orcourses.py
module which incorporates ambivalent code that can be utilized throughout modules. - Configuration administration — while that is attainable with a Jupyter Pocket book, it’s common apply to make use of configuration administration for a python program. Configuration administration refers to organising and managing a challenge’s parameters and variables in a centralised means. As a substitute of defining variables all through the code, they’re saved in a file that sits inside the challenge listing. Because of this you do not want to interrogate the code to vary a parameter. An outline of this may be discovered right here.
Notice. In the event you use a YAML file (.yml) for configuration, this requires the python bundle
yaml
. Make certain to put in the pyyaml bundle (not ‘yaml’) utilizingpip set up pyyaml
. Forgetting this may result in “bundle not discovered” errors—I’ve made this error, possibly greater than as soon as..
- Logging — utilizing loggers inside a python program allows you to simply monitor code execution, present debugging info and monitor a program or utility. While this performance is feasible inside a Jupyter Pocket book, it’s typically thought of overkill and is fulfilled with the print() assertion as an alternative. Through the use of python’s logger module, you’ll be able to format a logging object to your liking. It has 5 completely different messaging ranges (data, debug, warning, error, crucial) relative to the severity of the occasions being logger. You’ll be able to embrace logging messages all through the code to offer perception into code execution, which might be printed to terminal and/or written to a file. You’ll be able to be taught extra about logging right here.
When are Jupyter Notebooks helpful?
As I eluded in the beginning of this text, Jupyter Notebooks nonetheless have their place in knowledge science tasks. Their easy-to-use interface makes them nice for exploratory and interactive duties. Two key use circumstances are listed under:
- Conducting exploratory knowledge evaluation on a dataset through the preliminary levels of a challenge.
- Creating an interactive useful resource or report back to show analytical findings. Notice there are many instruments on the market that you should use on this nature, however a Jupyter Pocket book may do the trick.
Remaining ideas
Thanks for sticking with me to the very finish! I hope this dialogue has been insightful and has shed some gentle on how and why to start out programming. As with most issues in Information Science, there isn’t a single ‘right’ approach to resolve an issue, however a thought of multi-faceted method relying on the duty at hand.
Shout out to my colleague and fellow knowledge scientist Hannah Alexander for reviewing this text 🙂
Thanks for studying!