Which one to choose?
It Depends. What is it that we are trying to achieve. Python is a interpretive language and it makes quick answers possible. .ipynb files manage to exploit this perks at it’s best. However we see .py are also effective to use. It’s ability to run all the code at once also benefits us in some way. Let’s first see what both the methods are offering.
.py File
- The codes in .py file executed in a single run.
- .py files are easy to transfer to multiple platforms.
Ideal Situation
- To build a script-based process and run it whenever you want.
- To execute a process where every step is clear and known.
- To build modules
.ipynb
- Codes can be executed one by one.
- Will need Jupyter plugin support to run.
Ideal Situation
- To build an experimental solution
- Approach where you are not sure of the outcome, and you want to change or update parameters at every step.
- Experimental Work
Another Important feature of .ipynb is it can make your code in a presentable format for non-coders. You can add styled titles and make your code in a blog format which can explain all the steps along the way with diagrams etc.
As we can see clearly both of them are good in their own ways. So, what does it depend on for you to make the right choice? The answer is “Nature of the Task and how you will be approaching it.”
Task can be modular, which means the code knows where exactly to fit and what to do. Task like building a section of a software or a part of another code can be categorized under Modular Task’s.
Then there is also Experimental Tasks where the next step is dependent on the previous step and there is no fix flow for the execution of code. Task like training a Neural network fall under this category.
The golden rule is to balance both.
When creating a model or let’s say we are testing a new logic for our code, It’s better to use .ipynb as we would be running the same code again and again. The nature of the task would be repetitive as we are experimenting with different possible scenarios.
When you are done with your experimentation and confident with your new logic you can transfer it to a python file and make it run as a normal python program.
I usually train my model with .ipynb modes and once I am confident with the preprocessing and network layers I convert them to a py file and keep it as a template.
Now whenever I get a new data, I basically change the input parameters and just train a new model with just a few click. This did save me time in the longer run as you cannot go executing cell by cell for every new data you get.
Yes there is a button to run all, Still a script file seemed more robust as I could move it across deployment systems which just has python and no additional packages installed.
Conclusion
If you are working on a data analysis or machine learning project that requires a lot of interactive exploration and visualization, then .ipynb files are a great choice. They allow you to write code and see the results in the same document, as well as share your findings with others easily.
However, if you are working on a software development project that requires a more structured and modular approach, then .py files are better suited. They allow you to write reusable and testable code that can be run from the command line or imported by other modules.
Of course, you can also use both types of files in the same project, depending on the task at hand.
The bottom line is: there is no one right answer, but rather a trade-off between convenience and flexibility. Choose the file type that best suits your needs and preferences, and