Ortools module not found under nix-shell

Hello,
Starting a nix-shell as follows

nix-shell -p python39Packages.ipython python39Packages.ortools --run ipython

Followed by

 import ortools

results in

ModuleNotFoundError                       Traceback (most recent call last)
<ipython-input-1-0c521df57bb5> in <module>
----> 1 import ortools

ModuleNotFoundError: No module named 'ortools'

Any advice on how to approach this appreciated!

1 Like

python39.withPackages (p: with p; [ ipython ortools ])

https://nixos.wiki/wiki/Python

To elaborate, let’s include an additional python package, toolz:

nix-shell -p  python39Packages.toolz python39Packages.ipython python39Packages.ortools --run ipython

and import

import toolz # success

But then

import ortools

fails as above.
It appears this can be repeated with arbitrary other python packages - just not ortools.

I’m kinda shocked import toolz works. The proper way to create a python environment is with withPackages, though.

Someone smarter than I am can answer your question why one works but not the other.

if i run python instead of ipython

  • both work
Type "help", "copyright", "credits" or "license" for more information.
>>> import toolz
>>> import ortools
>>>

one obvious difference is

Yes, thanks for that. I suspect the behavior is only observed in the nix-shell.
I also suspect that, looking at @igel 's post above, that the reason import ortools fails in the above nix-shell command is due to most python packages (the arbitrary python packages I refer to) being built with buildPythonPackage, whereas ortools uses stdenv.mkDerivation . Are you able to write the nix-shell command that would install ortools and toolz using withPackages?

No such luck for me … which is a good thing really :slight_smile:

Reviewing @aanderse 's post the following does indeed work

nix-shell -p 'python39.withPackages(ps: with ps; [ toolz ortools ipython])'

As does the following derivation

{ pkgs ? import <nixpkgs> {} }:
let
  my-python-packages = python-packages: with python-packages; [
    ipython
    toolz
    ortools
  ]; 
  python-with-my-packages = pkgs.python39.withPackages my-python-packages;
in
pkgs.stdenv.mkDerivation {
  name = "baz";
  buildInputs = [ python-with-my-packages ];
}

using

nix-shell --run ipython