I am trying to integrate the ThorlabsPM100 power meter on artiq:
I tried to add the python library ThorlabsPM100 on flake.nix in the 'List of desired python packages section'
But I'm still not able to import the package.

This is part of the code that I am trying to run on Artiq:

import time
import numpy as np
from artiq.experiment import *
from artiq.language.core import kernel, delay
import serial
from ThorlabsPM100 import ThorlabsPM100, USBTMC

class PM100D(object):
   def __init__(self, device="/dev/usbtmc0"):
       inst = USBTMC(device=device)
       self.dev = ThorlabsPM100(inst=inst)
       self.pd_bandwidth = self.dev.input.pdiode.filter.lpass.state
       self.avg_count = self.dev.sense.average.count
   @property
   def read(self):
       return self.dev.read
   @property
   def wavelength(self):
       return self.dev.sense.correction.wavelength
   @property
   def getconfigure(self):
       return self.dev.getconfigure

Please do let me know if there is some other way of integrating this hardware.

    Charu But I'm still not able to import the package.

    What is the exact error message?

    Charu This is part of the code that I am trying to run on Artiq:

    But you didn't post your exact modification to flakes.nix.

      sb10q

      My flake.nix looks like this:

      {
        inputs.artiq.url = "git+https://github.com/m-labs/artiq.git?ref=release-7";
        inputs.extrapkg.url = "git+https://git.m-labs.hk/M-Labs/artiq-extrapkg.git?ref=release-7";
        inputs.extrapkg.inputs.artiq.follows = "artiq";
        inputs.ThorlabsPM100.url = "git+https://github.com/clade/ThorlabsPM100";
        outputs = { self, artiq, extrapkg,ThorlabsPM100 }:
          let
            pkgs = artiq.inputs.nixpkgs.legacyPackages.x86_64-linux;
            aqmain = artiq.packages.x86_64-linux;
            aqextra = extrapkg.packages.x86_64-linux;
          in {
            defaultPackage.x86_64-linux = pkgs.buildEnv {
              name = "artiq-env";
              paths = [
                # ========================================
                # EDIT BELOW
                # ========================================
                (pkgs.python3.withPackages(ps: [
                  # List desired Python packages here.
                  aqmain.artiq
                  ThorlabsPM100
                  #ps.paramiko  # needed if and only if flashing boards remotely (artiq_flash -H)
                  #aqextra.flake8-artiq
      
                  # The NixOS package collection contains many other packages that you may find
                  # interesting. Here are some examples:
                  #ps.pandas
                  #ps.numpy
                  #ps.scipy
                  #ps.numba
                  #ps.matplotlib
                  # or if you need Qt (will recompile):
                  #(ps.matplotlib.override { enableQt = true; })
                  #ps.bokeh
                  #ps.cirq
                  #ps.qiskit
                ]))
                #aqextra.korad_ka3005p
                #aqextra.novatech409b
                # List desired non-Python packages here
                #aqmain.openocd-bscanspi  # needed if and only if flashing boards
                # Other potentially interesting packages from the NixOS package collection:
                #pkgs.gtkwave
                #pkgs.spyder
                #pkgs.R
                #pkgs.julia
                # ========================================
                # EDIT ABOVE
                # ========================================
              ];
            };
          };
        nixConfig = {  # work around https://github.com/NixOS/nix/issues/6771
          extra-trusted-public-keys = "nixbld.m-labs.hk-1:5aSRVA5b320xbNvu30tqxVPXpld73bhtOeH6uAjRyHc=";
          extra-substituters = "https://nixbld.m-labs.hk";
        };
      }

      I'm not able to develop the new nix file and my error message was:

      Traceback (most recent call last):
        File "/nix/store/ky5j6106mjc6qgf1l4607z3bpg3yclgl-python3.9-artiq-7.8173.ff97675/bin/.artiq_run-wrapped", line 9, in <module>
          sys.exit(main())
        File "/nix/store/j74mzbwqvf900r3ikdp6maxnzgspwhyn-python3-3.9.16-env/lib/python3.9/site-packages/artiq/frontend/artiq_run.py", line 224, in main
          return run(with_file=True)
        File "/nix/store/j74mzbwqvf900r3ikdp6maxnzgspwhyn-python3-3.9.16-env/lib/python3.9/site-packages/artiq/frontend/artiq_run.py", line 210, in run
          raise exn
        File "/nix/store/j74mzbwqvf900r3ikdp6maxnzgspwhyn-python3-3.9.16-env/lib/python3.9/site-packages/artiq/frontend/artiq_run.py", line 201, in run
          exp_inst = _build_experiment(device_mgr, dataset_mgr, args)
        File "/nix/store/j74mzbwqvf900r3ikdp6maxnzgspwhyn-python3-3.9.16-env/lib/python3.9/site-packages/artiq/frontend/artiq_run.py", line 176, in _build_experiment
          module = file_import(args.file, prefix="artiq_run_")
        File "/nix/store/j74mzbwqvf900r3ikdp6maxnzgspwhyn-python3-3.9.16-env/lib/python3.9/site-packages/artiq/tools.py", line 87, in file_import
          spec.loader.exec_module(module)
        File "/nix/store/j74mzbwqvf900r3ikdp6maxnzgspwhyn-python3-3.9.16-env/lib/python3.9/site-packages/artiq/compiler/import_cache.py", line 23, in hook_exec_module
          im_exec_module(self, module)
        File "<frozen importlib._bootstrap_external>", line 850, in exec_module
        File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed
        File "/home/cjain/quant-net-dev-control/dev_control/polar_ctrl.py", line 6, in <module>
          from ThorlabsPM100 import ThorlabsPM100, USBTMC
      ModuleNotFoundError: No module named 'ThorlabsPM100'

      Hi. I think this error occur because ThorlabsPM100 is not yet part of the nixpkgs. You can easily check the existing packages here https://search.nixos.org/packages. I can create PR for that to make it available soon. For a temporary fix can you try to setup your flake.nix like this:

      {
        inputs.artiq.url = "git+https://github.com/m-labs/artiq.git?ref=release-7";
        inputs.extrapkg.url = "git+https://git.m-labs.hk/M-Labs/artiq-extrapkg.git?ref=release-7";
        inputs.extrapkg.inputs.artiq.follows = "artiq";
        inputs.thorlabspm100.url = "git+https://github.com/clade/ThorlabsPM100.git"
      
        outputs = { self, artiq, extrapkg }:
          let
            pkgs = artiq.inputs.nixpkgs.legacyPackages.x86_64-linux;
            aqmain = artiq.packages.x86_64-linux;
            aqextra = extrapkg.packages.x86_64-linux;
          in {
            defaultPackage.x86_64-linux = pkgs.buildEnv {
              name = "artiq-env";
              paths = [
                # ========================================
                # EDIT BELOW
                # ========================================
                (pkgs.python3.withPackages(ps: [
                  # List desired Python packages here.
                  aqmain.artiq
                  thorlabspm100.pythonPackage
                  #ps.paramiko  # needed if and only if flashing boards remotely (artiq_flash -H)
                  #aqextra.flake8-artiq
      
                  # The NixOS package collection contains many other packages that you may find
                  # interesting. Here are some examples:
                  #ps.pandas
                  #ps.numpy
                  #ps.scipy
                  #ps.numba
                  #ps.matplotlib
                  # or if you need Qt (will recompile):
                  #(ps.matplotlib.override { enableQt = true; })
                  #ps.bokeh
                  #ps.cirq
                  #ps.qiskit
                ]))
                #aqextra.korad_ka3005p
                #aqextra.novatech409b
                # List desired non-Python packages here
                #aqmain.openocd-bscanspi  # needed if and only if flashing boards
                # Other potentially interesting packages from the NixOS package collection:
                #pkgs.gtkwave
                #pkgs.spyder
                #pkgs.R
                #pkgs.julia
                # ========================================
                # EDIT ABOVE
                # ========================================
              ];
            };
          };
        nixConfig = {  # work around https://github.com/NixOS/nix/issues/6771
          extra-trusted-public-keys = "nixbld.m-labs.hk-1:5aSRVA5b320xbNvu30tqxVPXpld73bhtOeH6uAjRyHc=";
          extra-substituters = "https://nixbld.m-labs.hk";
        };

      Then in the same directory of your flake.nix run to update the flakes:

      nix flake update

      Then run

      nix develop

      See if this works.

        I'm sorry I forgot to add thorlabspm100 to the outputs argument. Here's the updated setup:

        
          inputs.artiq.url = "git+https://github.com/m-labs/artiq.git?ref=release-7";
          inputs.extrapkg.url = "git+https://git.m-labs.hk/M-Labs/artiq-extrapkg.git?ref=release-7";
          inputs.extrapkg.inputs.artiq.follows = "artiq";
          inputs.thorlabspm100.url = "git+https://github.com/clade/ThorlabsPM100.git"
        
          outputs = { self, artiq, extrapkg, thorlabspm100 }:
            let
              pkgs = artiq.inputs.nixpkgs.legacyPackages.x86_64-linux;
              aqmain = artiq.packages.x86_64-linux;
              aqextra = extrapkg.packages.x86_64-linux;
            in {
              defaultPackage.x86_64-linux = pkgs.buildEnv {
                name = "artiq-env";
                paths = [
                  # ========================================
                  # EDIT BELOW
                  # ========================================
                  (pkgs.python3.withPackages(ps: [
                    # List desired Python packages here.
                    aqmain.artiq
                    thorlabspm100.pythonPackage
                    #ps.paramiko  # needed if and only if flashing boards remotely (artiq_flash -H)
                    #aqextra.flake8-artiq
        
                    # The NixOS package collection contains many other packages that you may find
                    # interesting. Here are some examples:
                    #ps.pandas
                    #ps.numpy
                    #ps.scipy
                    #ps.numba
                    #ps.matplotlib
                    # or if you need Qt (will recompile):
                    #(ps.matplotlib.override { enableQt = true; })
                    #ps.bokeh
                    #ps.cirq
                    #ps.qiskit
                  ]))
                  #aqextra.korad_ka3005p
                  #aqextra.novatech409b
                  # List desired non-Python packages here
                  #aqmain.openocd-bscanspi  # needed if and only if flashing boards
                  # Other potentially interesting packages from the NixOS package collection:
                  #pkgs.gtkwave
                  #pkgs.spyder
                  #pkgs.R
                  #pkgs.julia
                  # ========================================
                  # EDIT ABOVE
                  # ========================================
                ];
              };
            };
          nixConfig = {  # work around https://github.com/NixOS/nix/issues/6771
            extra-trusted-public-keys = "nixbld.m-labs.hk-1:5aSRVA5b320xbNvu30tqxVPXpld73bhtOeH6uAjRyHc=";
            extra-substituters = "https://nixbld.m-labs.hk";
          };

          fsagbuya For a temporary fix can you try to setup your flake.nix like this:

          Did you actually test this?

          fsagbuya Please ignore the code I gave. It returns the same error as yours. I'm looking into it and working on a tested solution.

          fsagbuya @sb10q Can I not create my own package in the flake.nix file?
          Something like this:

          {
            inputs.artiq.url = "git+https://github.com/m-labs/artiq.git?ref=release-7";
            inputs.extrapkg.url = "git+https://git.m-labs.hk/M-Labs/artiq-extrapkg.git?ref=release-7";
            inputs.extrapkg.inputs.artiq.follows = "artiq";
            #inputs.ThorlabsPM100.url = "git+https://github.com/clade/ThorlabsPM100";
            outputs = { self, artiq, extrapkg }:
              let
                pkgs = artiq.inputs.nixpkgs.legacyPackages.x86_64-linux;
                aqmain = artiq.packages.x86_64-linux;
                aqextra = extrapkg.packages.x86_64-linux;
              in {
                defaultPackage.x86_64-linux = pkgs.buildEnv {
                  name = "artiq-env";
                  paths = [
                    # ========================================
                    # EDIT BELOW
                    # ========================================
                    (pkgs.python3.withPackages(ps: [
                      # List desired Python packages here.
                      aqmain.artiq
                      #ThorlabsPM100
                      #ps.paramiko  # needed if and only if flashing boards remotely (artiq_flash -H)
                      #aqextra.flake8-artiq
          
                      # The NixOS package collection contains many other packages that you may find
                      # interesting. Here are some examples:
                      #ps.pandas
                      #ps.numpy
                      #ps.scipy
                      #ps.numba
                      #ps.matplotlib
                      # or if you need Qt (will recompile):
                      #(ps.matplotlib.override { enableQt = true; })
                      #ps.bokeh
                      #ps.cirq
                      #ps.qiskit
                    ]))
                    #aqextra.korad_ka3005p
                    #aqextra.novatech409b
                    # List desired non-Python packages here
                    #aqmain.openocd-bscanspi  # needed if and only if flashing boards
                    # Other potentially interesting packages from the NixOS package collection:
                    #pkgs.gtkwave
                    #pkgs.spyder
                    #pkgs.R
                    #pkgs.julia
                    # ========================================
                    # EDIT ABOVE
                    # ========================================
                  ];
                };
              };
              let
                ThorlabsPM100 = python3Packages.buildPythonPackage rec {
                  pname = "ThorlabsPM100";
                  version = "1.2.2";
          
                  src = python3Packages.fetchPypi {
                    inherit pname version;
                    sha256 = "881cec8149b17c77d4af0097ee3fdb40eaeb618c876d49f1bc42c87611737967";
                  };
          
                };
              in
              mkShell {
                buildInputs = [
                  (python3.withPackages(ps:
                    [ ThorlabsPM100 ]
                  ))
                ];
              }
            nixConfig = {  # work around https://github.com/NixOS/nix/issues/6771
              extra-trusted-public-keys = "nixbld.m-labs.hk-1:5aSRVA5b320xbNvu30tqxVPXpld73bhtOeH6uAjRyHc=";
              extra-substituters = "https://nixbld.m-labs.hk";
            };
          }

          Can you please tell me where I can add this in the flake.nix file? Because this is giving an error.
          Thank you so much!

            Charu
            Hi. This flake builds for me. Only import with the module name of thorlabspm100 instead of ThorlabsPM100.

            {
              inputs.artiq.url = "git+https://github.com/m-labs/artiq.git?ref=release-7";
              inputs.extrapkg.url = "git+https://git.m-labs.hk/M-Labs/artiq-extrapkg.git?ref=release-7";
              inputs.extrapkg.inputs.artiq.follows = "artiq";
              inputs.nixpkgs.url = github:NixOS/nixpkgs/nixos-23.05;
            
              outputs = { self, artiq, extrapkg, nixpkgs }:
                let
                  pkgs = artiq.inputs.nixpkgs.legacyPackages.x86_64-linux;
                  aqmain = artiq.packages.x86_64-linux;
                  aqextra = extrapkg.packages.x86_64-linux;
            
                  thorlabspm100 = pkgs.python3Packages.buildPythonPackage rec {
                    pname = "thorlabspm100";
                    version = "1.2.2";
                    format = "setuptools";
                    src = pkgs.fetchFromGitHub {
                      owner = "clade";
                      repo = "ThorlabsPM100";
                      rev = "v${version}";
                      hash = "sha256-X4qEow6u4aE0sbFwZfK3YEso2RS0c9j4iaWJPHaPQV4=";
                    };
                    propagatedBuildInputs = [ pkgs.python3Packages.setuptools-scm ];
                    doCheck = false;
                  };
                in {
                  defaultPackage.x86_64-linux = pkgs.buildEnv {
                    name = "artiq-env";
                    paths = [
                      # ========================================
                      # EDIT BELOW
                      # ========================================
                      (pkgs.python3.withPackages(ps: [
                        # List desired Python packages here.
                        aqmain.artiq
                        thorlabspm100
                        #ps.paramiko  # needed if and only if flashing boards remotely (artiq_flash -H)
                        #aqextra.flake8-artiq
            
                        # The NixOS package collection contains many other packages that you may find
                        # interesting. Here are some examples:
                        #ps.pandas
                        #ps.numpy
                        #ps.scipy
                        #ps.numba
                        #ps.matplotlib
                        # or if you need Qt (will recompile):
                        # (ps.matplotlib.override { enableQt = true; })
                        #ps.bokeh
                        #ps.cirq
                        #ps.qiskit
                      ]))
                      #aqextra.korad_ka3005p
                      #aqextra.novatech409b
                      # List desired non-Python packages here
                      #aqmain.openocd-bscanspi  # needed if and only if flashing boards
                      # Other potentially interesting packages from the NixOS package collection:
                      #pkgs.gtkwave
                      #pkgs.spyder
                      #pkgs.R
                      #pkgs.julia
                      # ========================================
                      # EDIT ABOVE
                      # ========================================
                    ];
                  };
                };
              nixConfig = {  # work around https://github.com/NixOS/nix/issues/6771
                extra-trusted-public-keys = "nixbld.m-labs.hk-1:5aSRVA5b320xbNvu30tqxVPXpld73bhtOeH6uAjRyHc=";
                extra-substituters = "https://nixbld.m-labs.hk";
              };
            }

            Also PR in upstream is already merged. Haven't tried that yet, but will post that solution as well after I tested it.

            This version fetching from nixpkgs upstream builds as well. Just use the unstable version of nixpkgs in the nixpkgs.url .

            {
              inputs.artiq.url = "git+https://github.com/m-labs/artiq.git?ref=release-7";
              inputs.extrapkg.url = "git+https://git.m-labs.hk/M-Labs/artiq-extrapkg.git?ref=release-7";
              inputs.extrapkg.inputs.artiq.follows = "artiq";
              inputs.nixpkgs.url = github:NixOS/nixpkgs?ref=nixpkgs-unstable;
            
              outputs = { self, artiq, extrapkg, nixpkgs }:
                let
                  pkgs = artiq.inputs.nixpkgs.legacyPackages.x86_64-linux;
                  aqmain = artiq.packages.x86_64-linux;
                  aqextra = extrapkg.packages.x86_64-linux;
                  thorlabspm100 = nixpkgs.legacyPackages.x86_64-linux.python310Packages.thorlabspm100;
            
                in {
                  defaultPackage.x86_64-linux = pkgs.buildEnv {
                    name = "artiq-env";
                    paths = [
                      # ========================================
                      # EDIT BELOW
                      # ========================================
                      (pkgs.python3.withPackages(ps: [
                        # List desired Python packages here.
                        aqmain.artiq
                        thorlabspm100
                        #ps.paramiko  # needed if and only if flashing boards remotely (artiq_flash -H)
                        #aqextra.flake8-artiq
            
                        # The NixOS package collection contains many other packages that you may find
                        # interesting. Here are some examples:
                        #ps.pandas
                        #ps.numpy
                        #ps.scipy
                        #ps.numba
                        #ps.matplotlib
                        # or if you need Qt (will recompile):
                        # (ps.matplotlib.override { enableQt = true; })
                        #ps.bokeh
                        #ps.cirq
                        #ps.qiskit
                      ]))
                      #aqextra.korad_ka3005p
                      #aqextra.novatech409b
                      # List desired non-Python packages here
                      #aqmain.openocd-bscanspi  # needed if and only if flashing boards
                      # Other potentially interesting packages from the NixOS package collection:
                      #pkgs.gtkwave
                      #pkgs.spyder
                      #pkgs.R
                      #pkgs.julia
                      # ========================================
                      # EDIT ABOVE
                      # ========================================
                    ];
                  };
                };
              nixConfig = {  # work around https://github.com/NixOS/nix/issues/6771
                extra-trusted-public-keys = "nixbld.m-labs.hk-1:5aSRVA5b320xbNvu30tqxVPXpld73bhtOeH6uAjRyHc=";
                extra-substituters = "https://nixbld.m-labs.hk";
              };
            }

            Thank you so much, I could fix it with some other method.