How to use JuptyerWith with poetry2nix?

How can I use my poetry2nix environment with JupyterWith?

{poetry2nix}:
let

  poetryenv = poetry2nix.mkPoetryEnv {projectDir=./.;};

  jupyter = import (builtins.fetchGit {
    url = https://github.com/tweag/jupyterWith;
    rev = "";
  }) {};

  iPython = jupyter.kernels.iPythonWith {
    name = "python";
    packages = p: with p; [ numpy ];
  };

  jupyterEnvironment =
    jupyter.jupyterlabWith {
      kernels = [ iPython ];
    };
in
  jupyterEnvironment.env

Minimal example here. GitHub - energizah/poetry2nix-withJupyter-demo The problem is that the kernel does not contain any references to the dependencies.

There is an example in the repo for doing so right here: https://github.com/tweag/jupyterWith/tree/master/example/Python/poetry2nix

Thanks for the link. I’ve updated my repo to use that example and include the resulting error.

I cloned the repo and used the same command as you. I got the following:

<module 'requests' from '/nix/store/iwqvxb95wf5bp05r46g9i462z0wcf6d0-python3.7-requests-2.22.0/lib/python3.7/site-packages/requests/__init__.py'>

which seems correct. Do you have more details about your system (is it a Mac?). Did you try with --pure?

Here it is with --pure, same result. I’m on NixOS, specifically nixos-unstable.

It is interesting that the traceback mentions two different versions of Python: 3.7 and 3.8.

To reproduce:

git clone git@github.com:energizah/poetry2nix-withJupyter-demo.git  
cd poetry2nix-withJupyter-demo
nix-shell --pure --command 'jupyter run --kernel ipython_poetry  <<<"import requests; print(requests)"' 
% git clone git@github.com:energizah/poetry2nix-withJupyter-demo.git                         
Cloning into 'poetry2nix-withJupyter-demo'...
remote: Enumerating objects: 25, done.
remote: Counting objects: 100% (25/25), done.
remote: Compressing objects: 100% (18/18), done.
remote: Total 25 (delta 10), reused 22 (delta 7), pack-reused 0
Receiving objects: 100% (25/25), 5.20 KiB | 5.20 MiB/s, done.
Resolving deltas: 100% (10/10), done.
% cd poetry2nix-withJupyter-demo 
.git  poetry.lock  pyproject.toml  README.md  shell.nix
% nix-shell --pure --command 'jupyter run --kernel ipython_poetry  <<<"import requests; print(requests)"' 
/nix/store/p1j3cz54y4jz2s1ajwqpx6mq6kqn9igh-python3.7-traitlets-4.3.3/lib/python3.7/site-packages/traitlets/config/loader.py:795: SyntaxWarning: "is" with a literal. Did you mean "=="?
  if len(key) is 1:
/nix/store/p1j3cz54y4jz2s1ajwqpx6mq6kqn9igh-python3.7-traitlets-4.3.3/lib/python3.7/site-packages/traitlets/config/loader.py:804: SyntaxWarning: "is" with a literal. Did you mean "=="?
  if len(key) is 1:
Traceback (most recent call last):
  File "/nix/store/346skv0d24rqnf4npknbp9h5bs14j8zy-python3-3.8.6/lib/python3.8/runpy.py", line 194, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "/nix/store/346skv0d24rqnf4npknbp9h5bs14j8zy-python3-3.8.6/lib/python3.8/runpy.py", line 87, in _run_code
    exec(code, run_globals)
  File "/nix/store/rgg707my9m2qicm2jxihrl21cmh50mx6-python3.7-ipykernel-5.1.2/lib/python3.7/site-packages/ipykernel_launcher.py", line 15, in <module>
    from ipykernel import kernelapp as app
  File "/nix/store/rgg707my9m2qicm2jxihrl21cmh50mx6-python3.7-ipykernel-5.1.2/lib/python3.7/site-packages/ipykernel/__init__.py", line 2, in <module>
    from .connect import *
  File "/nix/store/rgg707my9m2qicm2jxihrl21cmh50mx6-python3.7-ipykernel-5.1.2/lib/python3.7/site-packages/ipykernel/connect.py", line 18, in <module>
    import jupyter_client
  File "/nix/store/5m9yhim8n97iihy1dz2cx3dl3fvajx0n-python3.7-jupyter_client-5.3.4/lib/python3.7/site-packages/jupyter_client/__init__.py", line 4, in <module>
    from .connect import *
  File "/nix/store/5m9yhim8n97iihy1dz2cx3dl3fvajx0n-python3.7-jupyter_client-5.3.4/lib/python3.7/site-packages/jupyter_client/connect.py", line 24, in <module>
    import zmq
  File "/nix/store/rmds624lhg7drbkxys28q61szpxid68k-python3.7-pyzmq-18.1.0/lib/python3.7/site-packages/zmq/__init__.py", line 47, in <module>
    from zmq import backend
  File "/nix/store/rmds624lhg7drbkxys28q61szpxid68k-python3.7-pyzmq-18.1.0/lib/python3.7/site-packages/zmq/backend/__init__.py", line 40, in <module>
    reraise(*exc_info)
  File "/nix/store/rmds624lhg7drbkxys28q61szpxid68k-python3.7-pyzmq-18.1.0/lib/python3.7/site-packages/zmq/utils/sixcerpt.py", line 34, in reraise
    raise value
  File "/nix/store/rmds624lhg7drbkxys28q61szpxid68k-python3.7-pyzmq-18.1.0/lib/python3.7/site-packages/zmq/backend/__init__.py", line 27, in <module>
    _ns = select_backend(first)
  File "/nix/store/rmds624lhg7drbkxys28q61szpxid68k-python3.7-pyzmq-18.1.0/lib/python3.7/site-packages/zmq/backend/select.py", line 28, in select_backend
    mod = __import__(name, fromlist=public_api)
  File "/nix/store/rmds624lhg7drbkxys28q61szpxid68k-python3.7-pyzmq-18.1.0/lib/python3.7/site-packages/zmq/backend/cython/__init__.py", line 6, in <module>
    from . import (constants, error, message, context,
ImportError: cannot import name 'constants' from partially initialized module 'zmq.backend.cython' (most likely due to a circular import) (/nix/store/rmds624lhg7drbkxys28q61szpxid68k-python3.7-pyzmq-18.1.0/lib/python3.7/site-packages/zmq/backend/cython/__init__.py)
Traceback (most recent call last):
  File "/nix/store/5m9yhim8n97iihy1dz2cx3dl3fvajx0n-python3.7-jupyter_client-5.3.4/lib/python3.7/site-packages/jupyter_client/runapp.py", line 95, in init_kernel_info
    reply = self.kernel_client.get_shell_msg(timeout=1)
  File "/nix/store/5m9yhim8n97iihy1dz2cx3dl3fvajx0n-python3.7-jupyter_client-5.3.4/lib/python3.7/site-packages/jupyter_client/client.py", line 77, in get_shell_msg
    return self.shell_channel.get_msg(*args, **kwargs)
  File "/nix/store/5m9yhim8n97iihy1dz2cx3dl3fvajx0n-python3.7-jupyter_client-5.3.4/lib/python3.7/site-packages/jupyter_client/blocking/channels.py", line 57, in get_msg
    raise Empty
_queue.Empty

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/nix/store/5m9yhim8n97iihy1dz2cx3dl3fvajx0n-python3.7-jupyter_client-5.3.4/bin/.jupyter-run-wrapped", line 9, in <module>
    sys.exit(RunApp.launch_instance())
  File "/nix/store/g1rb184825mg3jpxzncblcll0ca1mxrc-python3.7-jupyter_core-4.6.1/lib/python3.7/site-packages/jupyter_core/application.py", line 268, in launch_instance
    return super(JupyterApp, cls).launch_instance(argv=argv, **kwargs)
  File "/nix/store/p1j3cz54y4jz2s1ajwqpx6mq6kqn9igh-python3.7-traitlets-4.3.3/lib/python3.7/site-packages/traitlets/config/application.py", line 663, in launch_instance
    app.initialize(argv)
  File "</nix/store/y7hl497mxfnkbf4xfgd0qqnmrh806lm7-python3.7-decorator-4.4.0/lib/python3.7/site-packages/decorator.py:decorator-gen-7>", line 2, in initialize
  File "/nix/store/p1j3cz54y4jz2s1ajwqpx6mq6kqn9igh-python3.7-traitlets-4.3.3/lib/python3.7/site-packages/traitlets/config/application.py", line 87, in catch_config_error
    return method(app, *args, **kwargs)
  File "/nix/store/5m9yhim8n97iihy1dz2cx3dl3fvajx0n-python3.7-jupyter_client-5.3.4/lib/python3.7/site-packages/jupyter_client/runapp.py", line 78, in initialize
    self.init_kernel_info()
  File "/nix/store/5m9yhim8n97iihy1dz2cx3dl3fvajx0n-python3.7-jupyter_client-5.3.4/lib/python3.7/site-packages/jupyter_client/runapp.py", line 98, in init_kernel_info
    raise RuntimeError("Kernel didn't respond to kernel_info_request")
RuntimeError: Kernel didn't respond to kernel_info_request

Edit: It works on another computer. I’m not yet sure what’s different. The success is seen in the appearance of the requests module path (<module 'requests' from '/nix/store/...).

$ nix-shell --pure --command 'jupyter run --kernel ipython_poetry  <<<"import requests; print(requests)"'                                                                                                                                            


bash: warning: setlocale: LC_CTYPE: cannot change locale (en_US.UTF-8): No such file or directory
bash: warning: setlocale: LC_COLLATE: cannot change locale (en_US.UTF-8): No such file or directory
bash: warning: setlocale: LC_MESSAGES: cannot change locale (en_US.UTF-8): No such file or directory
bash: warning: setlocale: LC_NUMERIC: cannot change locale (en_US.UTF-8): No such file or directory
bash: warning: setlocale: LC_TIME: cannot change locale (en_US.UTF-8): No such file or directory
/home/user/.nix-profile/bin/manpath: can't set the locale; make sure $LC_* and $LANG are correct
/home/user/.nix-profile/bin/manpath: invalid option -- 'w'
Try 'manpath --help' or 'manpath --usage' for more information.
[IPKernelApp] WARNING | Unknown error in handling startup files:
<module 'requests' from '/nix/store/iwqvxb95wf5bp05r46g9i462z0wcf6d0-python3.7-requests-2.22.0/lib/python3.7/site-packages/requests/__init__.py'>

Hello,

I tried the same/equal thing weeks ago and opened a ticket at JupyterWith
… nothing happened …

so I did it with mach-nix and according to that template