, myself included, begin their coding journey utilizing a Jupyter Notebook. These information have the extension .ipynb, which stands for Interactive Python Pocket book. Because the extension title 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 abilities and begin engaged on knowledge science tasks.
A typical instance of a Jupyter Pocket book is beneath:
This all sounds nice. And don’t get me incorrect, to be used circumstances resembling conducting solo analysis or exploratory knowledge evaluation (EDA), Jupyter Notebooks are nice. The problems come up while 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 undertaking utilizing a model management system?
- How are you going to deploy code to a manufacturing setting?
Fairly quickly, the constraints of completely 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 vogue.
By the top of this text, you must have a transparent understanding of the best way to construction a small knowledge science undertaking as a Python program and recognize the benefits of transitioning to a programming strategy. You possibly can try an instance template to complement this text in my github 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 objective? Sure. Are there alternative routes to organise and execute code past the strategies I talk about on this article? Sure.
I wished to share this data to assist anybody eager to make the transfer away from notebooks and in the direction of writing scripts and applications. 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 massive deal?
For the aim of this text, I’ll be specializing in the Python programming language as that is the language I take advantage of for knowledge science tasks. Structuring code as a Python program unlocks a spread of functionalities which are tough to attain when working completely inside a Jupyter Pocket book. These advantages embrace collaboration, versatility and portability – you’re merely in a position to do extra together with your code. I’ll clarify these advantages additional down – stick with me slightly longer!
Python applications are usually organised into modules and packages. A module is a python script (information with a .py extension) that incorporates python code which may be imported into different information. A package deal is a listing that incorporates python modules. I’ll talk about the aim of the file __init__.py
later within the article.

Anytime you import a python library into your code, resembling built-in libraries like os
or third-party libraries like pandas
, you’re interacting with a python program that’s been organised right into a package deal and modules.
For instance, let’s say you wish to use the randint operate from numpy. This operate 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 indicate what you’re truly importing.

On this occasion, numpy
is a package deal; random
is a module and randint
is a operate.
So, it seems you most likely work together with python applications frequently. This poses the query, what does the journey seem like in the direction of changing into a python programmer?
The good transition: the place do you even begin?
The trick to constructing a purposeful python program is all within the file construction and organisation. It sounds boring however it performs an excellent essential half in setting your self up for fulfillment!
Let me use an analogy to elucidate: each home has a drawer that has nearly every part in it; instruments, elastic bands, medication, your hopes and desires, the lot. There’s no rhyme or purpose, it’s a dumping floor of nearly every part. Consider this as a Jupyter Pocket book. This one file usually incorporates all levels of a undertaking, from importing knowledge, exploring what the information appears to be like like, visualising developments, extracting options, coaching a mannequin and many others. For a undertaking 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 medication in one other and so forth.
An effective way to do this with code is to make use of a undertaking template. One which I take advantage of steadily is the Cookie Cutter Data Science template. You possibly can create a complete listing in your undertaking with all of the related information wanted to do absolutely anything in a number of easy operations in a terminal window – see the hyperlink above for data on the best way to set up and run Cookie Cutter.
Under are among the key options of the undertaking template:
- package deal or src listing — listing for python scripts/modules, outfitted with examples to get you began
- readme.md — file to explain utilization, setup and the best way to run the package deal
- docs listing — containing information that allow seamless autodocumentation
- Makefile— for writing OS ambivalent bespoke run instructions
- pyproject.toml/necessities.txt — for dependency administration

Prime tip. Be sure that to maintain Cookie Cutter updated. With each launch, new options are added in accordance with the ever-evolving knowledge science universe. I’ve learnt fairly a number of issues from exploring a brand new file or characteristic within the template!
Alternatively, you should utilize different templates to construct your undertaking resembling that offered by Poetry. Poetry is a package deal supervisor which you should utilize to generate a undertaking template that’s extra light-weight than Cookie Cutter.
One of the simplest ways to work together together with your undertaking is thru an IDE (Built-in Growth Atmosphere). This software program, resembling Visual Studio Code (VS Code) or PyCharm, embody quite a lot of options and processes that allow you to code, check, debug and package deal your work effectively. My private desire is VS Code!
From cells to scripts: let’s get coding
Now that now we have a growth setting and a properly structured undertaking template, how precisely do you write code in a python script in the event you’ve solely ever coded in a Jupyter Pocket book? To reply that query, let’s first contemplate a number of industry-standard coding Best Practices.
- Modular — comply with the software program engineering philosophy of ‘Single Responsibility Principle’. All code must be encapsulated in capabilities, with every operate performing a single job. The Zen of Python states: ‘Easy is healthier than advanced’.
- Readable — if code is readable, then there’s a very good likelihood it will likely be maintainable. Make sure the code is filled with docstrings and feedback!
- Trendy — format code in a constant and clear means. The PEP 8 guidelines are designed for this objective to advise how code must be introduced. You possibly can set up autoformatters resembling Black in an IDE in order that code is mechanically formatted in compliance with PEP 8 every time the python script is saved. For instance, the suitable stage of indentation and spacing shall be utilized so that you don’t even have to consider it!
- Versatile — if code is encapsulated into capabilities or courses, these may be reused all through a undertaking.
For a deeper dive into coding greatest observe, this article is a incredible overview of rules to stick to as a Knowledge Scientist, be sure you test it out!
With these greatest practices in thoughts, let’s return to the query: how do you write code in a python script?
Module construction
First, separate the totally different levels of your pocket book or undertaking into totally different python information. And ensure to call them in accordance with the duty. For instance, you may need the next scripts in a typical machine studying package deal: knowledge.py
, preprocess.py
, options.py
, practice.py
, predict.py
, consider.py
and many others. Relying in your undertaking construction, these would sit inside the package deal
or src
listing.
Inside every script, code must be organised or ‘encapsulated’ right into a courses and/or capabilities. A function is a reusable block of code that performs a single, well-defined job. A class is a blueprint for creating an object, with its personal set of attributes (variables) and strategies (capabilities). Encapsulating code on this method permits reusability and avoids duplication, thus retaining code concise.
A script would possibly solely want one operate if the duty is straightforward. For instance, a knowledge loading module (e.g. knowledge.py
) might solely comprise a single operate ‘load_data’ which masses knowledge from a csv file right into a pandas
DataFrame. Different scripts, resembling a knowledge processing module (e.g. preprocess.py
) will inherently contain extra duties and therefore requires extra capabilities or a category to encapsulate these duties.

Prime tip. Transitioning from Jupyter Notebooks to scripts might take a while and everybody’s private journey will look totally different. Some Knowledge Scientists I do know write code as python scripts immediately and don’t contact a pocket book. Personally, I take advantage of a pocket book for EDA, I then encapsulate the code into capabilities or courses earlier than porting to a script. Do no matter feels best for you.
There are a number of instruments that may assist with the transition. 1) In VS Code, you possibly can choose a number of traces, proper click on and choose Run Python > Run Choice/Line in Python Terminal. That is just like operating a cell in Jupyter Pocket book. 2) You possibly can convert a pocket book to a python script by clicking File > Obtain as > Python (.py). I wouldn’t advocate that strategy with giant notebooks for concern of making monster scripts, however the possibility is there!
The ‘__main__’ occasion
At this level, we’ve established that code must be encapsulated into capabilities and saved inside clearly named scripts. The subsequent logical query is, how will you tie all these scripts collectively so code will get executed in the suitable order?
The reply is to import these scripts right into a single-entry level and execute the code in a single place. Throughout the context of growing a easy undertaking, this entry level is often a script named foremost.py
(however may be referred to as something). On the high of foremost.py
, simply as you’d import mandatory built-in packages or third-party packages from PyPI, you’ll import your individual modules or particular courses/capabilities from modules. Any courses or capabilities outlined in these modules shall be accessible to make use of by the script they’ve been imported into.
To do that, the package deal listing inside your undertaking must comprise a __init__.py
file, which is often left clean for easy tasks. This file tells the python interpreter to deal with the listing as a package deal, which means that any information with a .py extension get handled as modules and may subsequently be imported into different information.
The construction of foremost.py
is undertaking dependent, however it can typically be dictated by the required order of code execution. For a typical machine studying undertaking, you’d first want to make use of the load_data operate 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 characteristic 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 foremost.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 totally different values relying on how the script is run:
- If the script is run immediately 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 title 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 data on this may be discovered here.
Given this course of, you’ll have to reference the grasp operate inside the if '__name__=='__main__':
conditional assertion in order that it’s executed when foremost.py
is run. Alternatively, you possibly can place the code beneath if '__name__=='__main__':
to attain the identical final result.

foremost.py
(or any python script) may be executed in terminal utilizing the next syntax:
python3 foremost.py
Upon operating foremost.py
, code shall be executed from all of the imported modules within the specified order. This is identical as clicking the ‘run all’ button on a Jupyter Notebook 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 capabilities.
You may also add CLI (command-line interface) arguments to your code utilizing instruments resembling argparse and typer, permitting you to toggle particular variables when operating foremost.py
within the terminal. This supplies a substantial amount of flexibility throughout code execution.
So we’ve now reached the very best half. The pièce de résistance. The actual explanation why, past having fantastically organised and readable code, you must go to the trouble of Programming.
The top sport: what’s the purpose of programming?
Let’s stroll by means of among the key advantages of transferring past Jupyter Notebooks and transitioning to writing Python scripts as an alternative.

- Packaging & distribution — you possibly can package deal and distribute your python program so it may be shared, put in and run on one other laptop. Bundle managers resembling pip, poetry or conda can be utilized to put in the package deal, simply as you’d set up packages from PyPI, resembling
pandas
ornumpy
. The trick to efficiently distributing your package deal is to make sure that the dependencies are managed accurately, which is the place the informationpyproject.toml
ornecessities.txt
are available. Some helpful sources may be discovered here and here. - Deployment — while there are a number of strategies and platforms to deploy code, utilizing a modular strategy will put you in good stead to get your code manufacturing prepared. Instruments resembling Docker allow the deployment of applications or purposes in remoted environments referred to as containers, which may be simply managed by means of CI/CD (steady integration & deployment) pipelines. It’s value noting that whereas Jupyter Notebooks may be deployed utilizing JupyterLab, this strategy lacks the pliability and scalability of adopting a modular, script-based workflow.
- Model management — transferring away from Jupyter Notebooks opens up the fantastic worlds of model management and collaboration. Model management techniques resembling Git are very a lot {industry} commonplace and provide a wealth of advantages, offering you employ them appropriately! Observe the motto ‘incremental modifications are key’ and make sure that you make small, common commits with logical commit messages in crucial language everytime you make purposeful modifications while growing. This can make it far simpler to maintain observe of modifications and check code. Here is an excellent helpful information to utilizing git as a knowledge scientist.
Enjoyable reality. It’s typically discouraged to commit Jupyter Notebooks to model management techniques as it’s tough 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 greatest observe so as to add docstrings to capabilities 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 entire undertaking within the type of html information. Instruments resembling Sphinx allow you to do that in a fast and straightforward means. You possibly can learn my earlier article which takes you thru this course of step-by-step.
- Reusability — adopting a modular strategy promotes the reuse of code. There are lots of widespread duties inside knowledge science tasks, resembling cleaning knowledge or scaling options. There’s little level in reinventing the wheel, so in the event you can reuse capabilities or courses with minor modification from earlier tasks, so long as there are not any confidentiality restrictions, then save your self that point! You may 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 observe to make use of configuration administration for a python program. Configuration administration refers to organising and managing a undertaking’s parameters and variables in a centralised means. As an alternative of defining variables all through the code, they’re saved in a file that sits inside the undertaking listing. Because of this you don’t want to interrogate the code to vary a parameter. An summary of this may be discovered here.
Notice. In case you use a YAML file (.yml) for configuration, this requires the python package deal
yaml
. Be sure that to put in the pyyaml package deal (not ‘yaml’) utilizingpip set up pyyaml
. Forgetting this may result in “package deal not discovered” errors—I’ve made this error, possibly greater than as soon as..
- Logging — utilizing loggers inside a python program lets you simply observe code execution, present debugging data 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. By utilizing python’s logger module, you possibly can format a logging object to your liking. It has 5 totally different messaging ranges (information, debug, warning, error, crucial) relative to the severity of the occasions being logger. You possibly can embrace logging messages all through the code to offer perception into code execution, which may be printed to terminal and/or written to a file. You possibly can study extra about logging here.
When are Jupyter Notebooks helpful?
As I eluded originally 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 beneath:
- Conducting exploratory knowledge evaluation on a dataset through the preliminary levels of a undertaking.
- Creating an interactive useful resource or report back to exhibit analytical findings. Notice there are many instruments on the market that you should utilize on this nature, however a Jupyter Pocket book also can do the trick.
Last ideas
Thanks for sticking with me to the very finish! I hope this dialogue has been insightful and has shed some mild on how and why to begin programming. As with most issues in Knowledge Science, there isn’t a single ‘right’ method to clear up an issue, however a thought of multi-faceted strategy relying on the duty at hand.
Shout out to my colleague and fellow knowledge scientist Hannah Alexander for reviewing this text 🙂
Thanks for studying!