Numpy and Tensorflow not working

# To learn more about how to use Nix to configure your environment
# see: https://developers.google.com/idx/guides/customize-idx-env
{ pkgs, ... }: {
  # Which nixpkgs channel to use.
  channel = "stable-24.05"; # or "unstable"
  # Use https://search.nixos.org/packages to find packages
  packages = [

    pkgs.python311
    pkgs.python311Packages.pip
    pkgs.python311Packages.tensorflow
    pkgs.python311Packages.numpy

  ];
  # Sets environment variables in the workspace
  env = {};
  idx = {
    # Search for the extensions you want on https://open-vsx.org/ and use "publisher.id"
    extensions = [
      # "vscodevim.vim"
    ];
    # Enable previews
    previews = {
      enable = true;
      previews = {
        # web = {
        #   # Example: run "npm run dev" with PORT set to IDX's defined port for previews,
        #   # and show it in IDX's web preview panel
        #   command = ["npm" "run" "dev"];
        #   manager = "web";
        #   env = {
        #     # Environment variables to set for your server
        #     PORT = "$PORT";
        #   };
        # };
      };
    };
    # Workspace lifecycle hooks
    workspace = {
      # Runs when a workspace is first created
      onCreate = {
        # Example: install JS dependencies from NPM
        # npm-install = "npm install";
        # Open editors for the following files by default, if they exist:
        default.openFiles = [ ".idx/dev.nix" "README.md" ];
      };
      # Runs when the workspace is (re)started
      onStart = {
        # Example: start a background task to watch and re-build backend code
        # watch-backend = "npm run watch-backend";
      };
    };
  };
}

This is the code for my dev.nix file for my workspace. For some reason, I always have trouble installing the proper modules for my program. I was dinking around with a basic neural network program:

import numpy as np
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers

mnist = keras.datasets.mnist
(x_train, y_train), (x_test, y_test) = mnist.load_data()

x_train = x_train.astype('float32') / 255
x_test = x_test.astype('float32') / 255

x_train = x_train.reshape(-1, 784)
x_test = x_test.reshape(-1, 784)

y_train = keras.utils.to_categorical(y_train, 10)
y_test = keras.utils.to_categorical(y_test, 10)

model = keras.Sequential([
    layers.Dense(512, activation='relu', input_shape=(784,)),
    layers.Dropout(0.2),
    layers.Dense(10, activation='softmax')
])

model.compile(optimizer='adam',
              loss='categorical_crossentropy',
              metrics=['accuracy'])
history = model.fit(x_train, y_train, epochs=10, batch_size=128, validation_split=0.2)
test_loss, test_acc = model.evaluate(x_test, y_test)
print('Test accuracy:', test_acc)
predictions = model.predict(x_test)
print(np.argmax(predictions[0]))

and whenever I try to run it, it always tells me one of the libraries isn’t installed, even though I’ve built them into the workspace. Anyone else have this issue?

I can’t see why it won’t work. Try using pip install instead. Personally I’ve used poetry and it worked for me.
You can just do pip install.
The reason it didn’t work is because you installed the package in the system. But you are actually in a venv environment, so you’ll have to install packages inside the venv.
You can either: Use poetry or run these command

source .venv/bin/activate
pip install numpy

and etc.

It doesn’t work. Pip can’t modify immutable files.

I’m having the same issue. Unable to install any python packages. Numpy, pandas, matplotlib are all no go.

  • Tried via dev.nix. when importing get an error module not found.
  • Tried pip install. Get error externally-managed-environment This command has been disabled as it tries to modify the immutable /nix/store filesystem

Even tried google’s gemini, sadly not helpful.

Try using poetry? Poetry works for me, although it’s not without it’s own problem.
Here’s my setup if that help

[tool.poetry]
name = "python-flask"
version = "0.1.0"
description = "Start a simple web app with the Flask framework for Python"
authors = ["Google LLC"]
packages = [{ include = "src" }]

[tool.poetry.dependencies]
python = "^3.11"
Flask = "^3.0.1"
jax = { extras = ["cpu"], version = "^0.4.30" }
jaxtyping = "^0.2.33"
matplotlib = "^3.9.1"

[tool.poetry.scripts]
app = 'src.main:main'

[build-system]
requires = ["poetry-core"]
build-backend = "poetry.core.masonry.api"

(just realised that I forgot to delete flask)

Made a new project and installed numpy and friends via pip within a virtual environment. All seems to work ok. Don’t know what the issue was, but in the new project I decided not to change the channel to 24.05 nor use python 312. Also turned off ai indexing. So not sure if any of those where the problem or just a coincidental corrupted environment in the previous project.