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.
igel
July 22, 2021, 12:01pm
5
if i run python
instead of ipython
Type "help", "copyright", "credits" or "license" for more information.
>>> import toolz
>>> import ortools
>>>
one obvious difference is
{ lib
, buildPythonPackage
, fetchPypi
, nose
}:
buildPythonPackage rec {
pname = "toolz";
version = "0.11.1";
src = fetchPypi {
inherit pname version;
sha256 = "1grz3zvw5ixwqqlbv0n7j11mlcxb66cirh5i9x9zw8kqy0hpk967";
};
checkInputs = [ nose ];
, zlib
, lsb-release
, which
, protobuf
, cbc
, ensureNewerSourcesForZipFilesHook
, python
, swig4
}:
stdenv.mkDerivation rec {
pname = "or-tools";
version = "9.0";
disabled = python.pythonOlder "3.6"; # not supported upstream
src = fetchFromGitHub {
owner = "google";
repo = "or-tools";
rev = "v${version}";
sha256 = "0yihrsg8wj4b82xwg1hbn97my8zqd7xhw7dk7wm2axsyvqd6m3b3";
};
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
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