Unable to make tensorflow work

Hi everyone,

I am trying the following configuration:

let
  # Unstable packages
  unstableTarball = fetchTarball https://github.com/NixOS/nixpkgs/archive/nixos-unstable.tar.gz;

  # Custom Python package with all the (Python) imports I need
  my-python-packages = python-packages: with python-packages; [
    ...
    tensorflow
  ]; 
  python-with-my-packages = pkgs.python3.withPackages my-python-packages;
in
{
  ...
  environment.systemPackages = with pkgs; [
  ...
  python310Packages.tensorflow
  ]
}

I tried different combinations - placing tensorflow within my-python-packages; placing python310Packages.tensorflow within environment.systemPackages; or both. In any case, I got the following behaviour:

~ python3
Python 3.10.8 (main, Oct 11 2022, 11:35:05) [GCC 11.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import tensorflow as tf
2022-12-31 09:09:08.469691: I tensorflow/core/platform/cpu_feature_guard.cc:193] This TensorFlow binary is optimized with oneAPI Deep Neural Network Library (oneDNN) to use the following CPU instructions in performance-critical operations:  SSE3 SSE4.1 SSE4.2 AVX AVX2 FMA
To enable them in other operations, rebuild TensorFlow with the appropriate compiler flags.
>>> from tensorflow.keras import Sequential
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'tensorflow.keras'

Thanks :slight_smile:
Davide

I also tried the following setting

hardware.opengl.setLdLibraryPath = true;

but to no avail

Hi, problem is that config isn’t actually using the python-with-my-packages you declared.
So if you replace python310Packages.tensorflow with python-with-my-packages it should work as expected.
BTW unstableTarball isn’t being used anywhere either.

1 Like

Hi, I tried as you suggested:

let
  my-python-packages = python-packages: with python-packages; [
    # ...
    # no tensorflow here
  ]; 
in
{
  hardware.opengl.setLdLibraryPath = true;
  environment.systemPackages = with pkgs; [
    python-with-my-packages
    python-with-my-packages.tensorflow
    # ...
    # no python310Packages.tensorflow here
  ];
}

but I got the error message

error: attribute 'tensorflow' missing`
           84|     python-with-my-packages.tensorflow
             |     ^

I use the unstableTarball only for a few packages not listed above, I copy-pasted it by mistake :upside_down_face:

python-with-my-packages already includes both Python and Tensorflow.
Plus, as the error says, it doesn’t have a tensorflow attribute.
So just remove that python-with-my-packages.tensorflow line.
You’ll also need to add keras to your definition of my-python-packages (i.e. below tensorflow)

1 Like

Thank you, that solves my problems