0

I have put several days into sorting out the problem of conda's python virtual environments not recognizing the kernels they were build around. The problem manifests as both Spyder and Jupyter-notebook failing to start with a valid kernel. Here is an example of a conda environment built for kernel 3.8.10 specifically to run pymc3 (per pymc3 environment requirements) and how it fails in the jupyter-notebook also selected and installed specifically in that environment.

The only thing I know for sure: the error message is totally misleading. The win3api dll seems to have nothing to do with the actual problem

Does anyone recognize this? Are there tweaks that will get me going? Or is this another case of Rip-out-and-reinstall-Anaconda3? Which is a familiar refrain.

##########################

Failed to start the kernel
Unhandled error

Traceback (most recent call last):
  File "C:\Users\Peter\AppData\Roaming\Python\Python38\site-packages\tornado\web.py", line 1704, in _execute
    result = await result
  File "C:\Users\Peter\AppData\Roaming\Python\Python38\site-packages\tornado\gen.py", line 769, in run
    yielded = self.gen.throw(*exc_info)  # type: ignore
  File "C:\Users\Peter\anaconda3\lib\site-packages\notebook\services\sessions\handlers.py", line 69, in post
    model = yield maybe_future(
  File "C:\Users\Peter\AppData\Roaming\Python\Python38\site-packages\tornado\gen.py", line 762, in run
    value = future.result()
  File "C:\Users\Peter\AppData\Roaming\Python\Python38\site-packages\tornado\gen.py", line 769, in run
    yielded = self.gen.throw(*exc_info)  # type: ignore
  File "C:\Users\Peter\anaconda3\lib\site-packages\notebook\services\sessions\sessionmanager.py", line 88, in create_session
    kernel_id = yield self.start_kernel_for_session(session_id, path, name, type, kernel_name)
  File "C:\Users\Peter\AppData\Roaming\Python\Python38\site-packages\tornado\gen.py", line 762, in run
    value = future.result()
  File "C:\Users\Peter\AppData\Roaming\Python\Python38\site-packages\tornado\gen.py", line 769, in run
    yielded = self.gen.throw(*exc_info)  # type: ignore
  File "C:\Users\Peter\anaconda3\lib\site-packages\notebook\services\sessions\sessionmanager.py", line 100, in start_kernel_for_session
    kernel_id = yield maybe_future(
  File "C:\Users\Peter\AppData\Roaming\Python\Python38\site-packages\tornado\gen.py", line 762, in run
    value = future.result()
  File "C:\Users\Peter\anaconda3\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\Peter\AppData\Roaming\Python\Python38\site-packages\jupyter_client\multikernelmanager.py", line 186, in start_kernel
    km.start_kernel(**kwargs)
  File "C:\Users\Peter\AppData\Roaming\Python\Python38\site-packages\jupyter_client\manager.py", line 337, in start_kernel
    kernel_cmd, kw = self.pre_start_kernel(**kw)
  File "C:\Users\Peter\AppData\Roaming\Python\Python38\site-packages\jupyter_client\manager.py", line 286, in pre_start_kernel
    self.write_connection_file()
  File "C:\Users\Peter\AppData\Roaming\Python\Python38\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\Peter\AppData\Roaming\Python\Python38\site-packages\jupyter_client\connect.py", line 136, in write_connection_file
    with secure_write(fname) as f:
  File "C:\Users\Peter\anaconda3\lib\contextlib.py", line 113, in __enter__
    return next(self.gen)
  File "C:\Users\Peter\AppData\Roaming\Python\Python38\site-packages\jupyter_core\paths.py", line 461, in secure_write
    win32_restrict_file_to_user(fname)
  File "C:\Users\Peter\AppData\Roaming\Python\Python38\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.
​```
  • Please elaborate on your setup. It looks like Jupyter is installed natively (not through Conda), but you are trying to run a Conda environment as kernel. How did you register the Conda environment as a kernel? – merv Aug 04 '21 at 18:37

1 Answers1

0

First, make sure your environment is activated with conda activate myenv

Next, install ipykernel which provides the IPython kernel for Jupyter:

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"
}

That's it!

Sup
  • 191
  • 5
  • I followed your advice and it worked without incident. I even made a fresh environment and pymc3 in that environment. The steps worked as advertised. I also had to conda install -c conda-forge jupyter as that didn't come with the vanilla 3.8 kernel kit. All files were present and configured as you described. And the fresh new jupyter-notebook died out of the box. Same kernel error. – Peter Leopold Aug 04 '21 at 21:33
  • Perhaps jupyter is not designed to function in virtual environments. That is consistent with all observations so far. – Peter Leopold Aug 04 '21 at 21:34
  • I think the problem is that the anaconda/conda utility cannot be trusted to make a virtual environment that contains a working version of the kernel it purports to support and/or the various utilities it purports to add. New anaconda environments with installed utilities like jupyter-notebook just don't configure right out of the box. This seems like a major limitation for the anaconda project itself. – Peter Leopold Aug 05 '21 at 03:02
  • I abandoned windows and virtual environments. I have a default environment in linux that gives me no problems. Jupyter works with the default kernel 3.8.10. Venv not apparently supported, but I'm happy I don't need it. – Peter Leopold Aug 09 '21 at 17:32