What do you mean by ‘selected cell’ there? It is hard to follow exactly what you are asking here. Is your code block two cells or one cell? You need to make that clearer. Either add comments or make them separate blocks. And state the exact errors you see not just that it doesn’t work.
Anyway, I’m guess you aren’t understanding environments or a kernel’s namespace. Or one of those. And you aren’t providing us much information about how you are access Jupyter or your initial environment.
If you are accessing a fresh environment lacking the tensorflow package each time you start things up then installing tensorflow each time is expected or you’ll see an error that the Module doesn’t exist. That may not be it though. The problem may be with what happens to that environment at the end of your work session. For example if it is Docker based, then things may be essentially reset so you need to install each time. We cannot tell much without more information.
The other possible issue that may be causing you confusion here is that a namespace isn’t persistent. You start up a namespace where the Python computational kernel is running each time and so you have to run the import statements each time first again or you won’t be able to run the code using those functions and methods.
Also, going forward with modern Jupyter you should be using %pip install tensorflow
. See here to understand better why you want to use the modern install magic when running installations in a running .ipynb
file. It has to deal with the concept of environments where the computational kernel is running as well.