How to install python in project idx

I am a new project IDX, and I have created an empty project. I was trying to have a Python notebook and just run Hello World. But for the love of god, I can’t install Python in this environment. Any help is appreciated, and thank you.

1 Like

Hi Pranay - one option is to create a Gemini API Notebook project:

This will create a Python-based notebook. To see how we provisioned this environment, you can look at the dev.nix file generated by it:

# 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-23.11"; # or "unstable"
  # Use https://search.nixos.org/packages to find packages
  packages = [
    pkgs.python311
    pkgs.python311Packages.pip
  ];
  # Sets environment variables in the workspace
  env = {};
  idx = {
    # Search for the extensions you want on https://open-vsx.org/ and use "publisher.id"
    extensions = [
      "ms-toolsai.jupyter"
      "ms-python.python"
    ];
    workspace = {
      # Runs when a workspace is first created with this `dev.nix` file
      onCreate = {
        create-venv = ''
          python -m venv .venv
          source .venv/bin/activate
          pip install -r requirements.txt
        '';
      };
      # To run something each time the environment is rebuilt, use the `onStart` hook
    };
    # Enable previews and customize configuration
    previews = {};
  };
}

These Nix settings are what tell the workspace to be setup with the Python libraries, command line tools, and so on. You could replicate this in your blank workspace by adding the missing details into that workspace’s dev.nix file.

Cheers,
Kirupa

2 Likes

Awesome thank you so much for letting me know and I will give it a try now and let you know the progress

So basically I did this but I was not able to install pip packages

1 Like

@Hemansh_Patel guessing that you used python3.xx<11 or 12 .
I faced this issue also, but there’s a fix, just do this

python3.X -m pip install bla bla #where X is the python version, -m means message

for some weird reason it shows “No module named pip”

1 Like

  1. Try clicking the run button instead, I don’t think you’re inside the virtualenv, so you can’t access some of the installed module.
    Alternatively, you can run source .venv/bin/activate in the terminal before running the python program (or before running pip)

  2. Pygame will not work on IDX as there are no way to stream native application for the time being
    Pygame/PySimpleGUI setup help - #6 by kirupa

so here is a full tutorial to install jupyter in idx:

  1. create a empty project.
  2. open the dev.nix file.
# 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-23.11"; # or "unstable"
  # Use https://search.nixos.org/packages to find packages
  packages = [
    pkgs.python311
    pkgs.python311Packages.pip
  ];
  # Sets environment variables in the workspace
  env = {};
  idx = {
    # Search for the extensions you want on https://open-vsx.org/ and use "publisher.id"
    extensions = [
      "ms-toolsai.jupyter"
      "ms-python.python"
    ];
    workspace = {
      # Runs when a workspace is first created with this `dev.nix` file
      onCreate = {
        create-venv = ''
          python -m venv .venv
          source .venv/bin/activate
          pip install -r requirements.txt
        '';
      };
      # To run something each time the environment is rebuilt, use the `onStart` hook
    };
    # Enable previews and customize configuration
    previews = {};
  };
}

use this as dev.nix
4. once done rebuild the enviroment.
5. after rebuilding open terminal and run these commands:

python3 -m venv myenv
source myenv/bin/activate

  1. by now you should be inside your enviroment.
  2. open terminal and run pip install jupyter
  3. also get the jupyter extension.
  4. open a terminal (inside your enviroment) and run this command jupyter notebook
  5. your idx will reload. if not autometically do it manually.
  6. open the terminal again. and there should be a server running and you hould see a url similer to this: http://127.0.0.1:8888/tree?token=4eca8862f7fb141d28d8a7623c...
  7. create a ipynb file and run it it will ask for the url for the jupyter server.
  8. paste the url: http://127.0.0.1:8888/tree?token=4eca8862f7fb141d28d8a7623c2c7eb87598... there. and hit enter.
  9. now you should be able to run notebook in idx.
1 Like