0

I've created a virtual environment in my working folder and a jupyter notebook but it seems like my jupyter notebook is not connected to Python.

I've created a virtual environment inside this directory(C:\Users\MyName\Documents\DataScience\Code) by running the following commands:

pip install virtualenv
virtualenv .venv
.venv\Scripts\activate

Then, I've installed these two following packages.

pip install jupyter
pip install jupyterlab

After installing jupyter, I opened the jupyter notebook by typing in "jupyter notebook" in the command prompt and it worked. When I type in "Jupyter Notebook", the browser pops up. But if I try to create a new notebook in the jupyter browser by clicking the "New" dropdown menu and selecting "Python 3" it gives me an error. It let me open a new notebook but it throws a Kernel error. It still says Python 3 (ipykernel) on the top right corner but with a black dot that looks like a bomb icon.

Normally, when I create a new jupyter notebook, it just says Python 3 without ipykernel. I've tried "pip(or conda) install pywin32" but it didn't work. How do I solve this error and connect my notebook to Python? I have Python3.8 on my computer and would like to connect jupyter notebook to regular Python 3 without ipykernel.

FYI, this is an error message I see when I click on the kernel error.

Traceback (most recent call last):
  File "c:\users\MyName\Documents\DataScience\Code\.venv\lib\site-packages\tornado\web.py", line 1704, in _execute
    result = await result
  File "c:\users\MyName\Documents\DataScience\Code\.venv\lib\site-packages\tornado\gen.py", line 769, in run
    yielded = self.gen.throw(*exc_info)  # type: ignore
  File "c:\users\MyName\Documents\DataScience\Code\.venv\lib\site-packages\notebook\services\sessions\handlers.py", line 69, in post
    model = yield maybe_future(
  File "c:\users\MyName\Documents\DataScience\Code\.venv\lib\site-packages\tornado\gen.py", line 762, in run
    value = future.result()
  File "c:\users\MyName\Documents\DataScience\Code\.venv\lib\site-packages\tornado\gen.py", line 769, in run
    yielded = self.gen.throw(*exc_info)  # type: ignore
  File "c:\users\MyName\Documents\DataScience\Code\.venv\lib\site-packages\notebook\services\sessions\sessionmanager.py", line 98, in create_session
    kernel_id = yield self.start_kernel_for_session(session_id, path, name, type, kernel_name)
  File "c:\users\MyName\Documents\DataScience\Code\.venv\lib\site-packages\tornado\gen.py", line 762, in run
    value = future.result()
  File "c:\users\MyName\Documents\DataScience\Code\.venv\lib\site-packages\tornado\gen.py", line 769, in run
    yielded = self.gen.throw(*exc_info)  # type: ignore
  File "c:\users\MyName\Documents\DataScience\Code\.venv\lib\site-packages\notebook\services\sessions\sessionmanager.py", line 110, in start_kernel_for_session
    kernel_id = yield maybe_future(
  File "c:\users\MyName\Documents\DataScience\Code\.venv\lib\site-packages\tornado\gen.py", line 762, in run
    value = future.result()
  File "c:\users\MyName\Documents\DataScience\Code\.venv\lib\site-packages\notebook\services\kernels\kernelmanager.py", line 176, in start_kernel
    kernel_id = await maybe_future(self.pinned_superclass.start_kernel(self, **kwargs))
  File "c:\users\MyName\Documents\DataScience\Code\.venv\lib\site-packages\jupyter_client\multikernelmanager.py", line 186, in start_kernel
    km.start_kernel(**kwargs)
  File "c:\users\MyName\Documents\DataScience\Code\.venv\lib\site-packages\jupyter_client\manager.py", line 337, in start_kernel
    kernel_cmd, kw = self.pre_start_kernel(**kw)
  File "c:\users\MyName\Documents\DataScience\Code\.venv\lib\site-packages\jupyter_client\manager.py", line 286, in pre_start_kernel
    self.write_connection_file()
  File "c:\users\MyName\Documents\DataScience\Code\.venv\lib\site-packages\jupyter_client\connect.py", line 466, in write_connection_file
    self.connection_file, cfg = write_connection_file(self.connection_file,
  File "c:\users\MyName\Documents\DataScience\Code\.venv\lib\site-packages\jupyter_client\connect.py", line 136, in write_connection_file
    with secure_write(fname) as f:
  File "c:\users\MyName\anaconda3\lib\contextlib.py", line 113, in __enter__
    return next(self.gen)
  File "c:\users\MyName\Documents\DataScience\Code\.venv\lib\site-packages\jupyter_core\paths.py", line 461, in secure_write
    win32_restrict_file_to_user(fname)
  File "c:\users\MyName\Documents\DataScience\Code\.venv\lib\site-packages\jupyter_core\paths.py", line 387, in win32_restrict_file_to_user
    import win32api
ImportError: DLL load failed while importing win32api: The specified procedure could not be found.



Thank you.

Karina1117
  • 69
  • 1
  • 4
  • Try this [Try this , this may help you ](https://stackoverflow.com/questions/58612306/how-to-fix-importerror-dll-load-failed-while-importing-win32api) – Alama1 Jul 25 '21 at 14:10

1 Answers1

0

Jupyter Notebook makes sure that the IPython kernel is available, but you have to manually add a kernel with a different version of Python or a virtual environment.

pip install --user ipykernel

Next you can add your virtual environment to Jupyter by typing:

python -m ipykernel install --user --name=myenv

This should print the following:

Installed kernelspec myenv in /home/user/.local/share/jupyter/kernels/myenv

In this folder you will find a kernel.json file which should look the following way if you did everything correctly:

 {
 "argv": [
   "/home/user/anaconda3/envs/myenv/bin/python",
   "-m",
   "ipykernel_launcher",
   "-f",
   "{connection_file}"
  ],
  "display_name": "myenv",
  "language": "python"
 }
bill
  • 118
  • 5