简体   繁体   中英

execution process in Jupyter Notebook

I have some questions about the way that Jupyter Notebook reads python code lines. (Sorry for not being able to upload code image. my reputation level is low.) there exists csv file named 'train.csv' and I allocate this file to the variable named 'titanic_df'

import pandas as pd
titanic_df=pd.read_csv('train.csv')
print(titanic_df)

this goes well when it is executed. However, my questioin is,

import pandas as pd
# titanic_df=pd.read_csv('train.csv')
print(titanic_df)

this also goes well contrary to my intention. Even though I commented out the reading csv file step, titanic_df prints datas. As I run same code on python installed on my computer and second code doesn't work, I guess there are some differences on the way Jupyter Notebook executes codes. How Jupyter Notebook works?


Jupyter can be somewhat confusing at first, but I will explain what's going on here.

A sequence of events occurred after the following code was run in Jupyter:

import pandas as pd
titanic_df=pd.read_csv('train.csv')
print(titanic_df)

In that first line of code, you imported the pandas module and loaded the pandas into memory. The pandas module is available to use. In the second line, you access the pd.read_csv function within the pandas module.

The pandas module and it's functions are available whenever called and loaded into memory. The pandas functions will be available to be used until pandas is removed from memory.

Therefore, to answer this question: When the pd.read_csv line of code is commented-out like such:

# titanic_df=pd.read_csv('train.csv')

this pandas function has not been removed from memory. Pandas is still loaded in memory. The only thing that changes is the commented line of code will not be executed again, or any time you run this block of code. But the pandas module and the pandas features will remain in memory and available and ready to be used.

Even if the first line of code were to be commented out, the pandas module and its features would still remain active in memory and ready to use in Jupyter. But if Jupyter is restarted, then the panda module would not be reloaded into memory.

Also, know about restarting the kernel. If you were to comment-out the first line of code but not the second line of code, and then you were to select in Jupyter "Restart kernel and run all cells", then two things would happen. The pandas module would not be loaded and then calling the pd.read_csv line of code would cause an error. The error would occur because your code would call for a pandas function, but the pandas module had not been installed.

A saved Jupyter Notebook file will run all the cells in the file whenever the existing file is opened.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM