4

I have anaconda base environment and 1 other environment where i have tensorflow installed which i am trying to import in my jupyter notebook after changing the kernel.

i installed jupyter notebook in my conda base environment using the following command:

conda install -c conda-forge jupyterhub and by simply doing: pip install jupyter

after that i added my new environment with tensorflow and some other additional packages with this command: python -m ipykernel install --user --name env_cod --display-name "Python (env_cod)"

So now i launch jupyter notebook by simple (jupyter-notebook) in the desired directory and i also tried launching jupyter-notebook after activating the env_cod environment from terminal. But still i am not able to import tensorflow in my jupyter notebook after changing the kernal by selecting from the kernel tab also when i create new jupyter notebook by selecting env_cod environment i am not able to import tensorflow and get this error : no module named tensorflow

I already tried uninstalling and installing jupyter in my both base and env_cod environment.

Also there is nothing wrong with my env_cod environment and tensorflow is working perfectly fine along with other packages when i run my scripts through PyCharm after selecting env_cod environment. The Problem is Only with Jupyter Notebook.

DevPy
  • 234
  • 4
  • 16
  • Check your interpreter path in the notebook via `import sys; print(sys.executable)`. If it does not point to your new environment, that is the issue. – James Mar 01 '20 at 13:03
  • 1
    Yeah it still points to the base environment as expected even after changing the kernel. – DevPy Mar 01 '20 at 14:07

1 Answers1

3

Solved it by editing the .jason file and providing the right path to the environment executable.

Edit.

all i did was to go into C:\Users\YOUR_USERNAME\AppData\Roaming\jupyter\kernels and you will find all the kernels which you added into Jupyter Notebook.
Now just go to folder which is not loading the correct Python Executable and you will find kernel.json file in that folder.
Open that file

{
 "argv": [
  "D:\\Path\\To\\Correct\\python.exe",
  "-m",
  "ipykernel_launcher",
  "-f",
  "{connection_file}"
 ],
 "display_name": "ENV_NAME",
 "language": "python"
}

For some reason the path to python executable was of my Base Python Environment so i changed it to my Virtual Environment's Python Executable Path and after that it loads the correct environment in my Jupyter Notebook.

DevPy
  • 234
  • 4
  • 16
  • could you be more specific please? – yo1122 Sep 01 '21 at 15:09
  • 2
    Answer Edited for explanation hope it helps. – DevPy Sep 01 '21 at 16:17
  • hello! can you provide an example of your correct environment path python executable? I believe I am having the same issue and changed my path from C:\\Users\\name\\miniconda3\\python.exe to C:\\Users\\name\\miniconda3\\envs\\env_name\\python.exe and its not liking it! – mcat Jun 27 '22 at 17:54