Skip to content

suggest add numpy to dependance of codon-jit so that it can be installed automatically #808

Open

Description

@l1t1

pip install codon-jit didn't install numpy, but import codon failed due to lack of numpy

pip install codon-jit 
...
Successfully installed astunparse-1.6.3 codon-jit-0.4.6 cython-3.2.4 packaging-26.2 six-1.17.0 wheel-0.47.0

python ../test-codonjit.py
Traceback (most recent call last):
  File "/par/tpy3145/../test-codonjit.py", line 1, in <module>
    import codon
  File "/par/tpy3145/codonenv/lib/python3.14/site-packages/codon/__init__.py", line 7, in <module>
    from .decorator import jit, convert, execute, JITError, JITWrapper, _jit_register_fn, _jit_callback_fn, _jit
  File "/par/tpy3145/codonenv/lib/python3.14/site-packages/codon/decorator.py", line 13, in <module>
    import numpy as np
ModuleNotFoundError: No module named 'numpy'

Activity

  1. BI71317 commented on May 15, 2026

    @BI71317
    Contributor

    MRE

    import codon
    from time import time
    
    
    @codon.jit(debug=True)
    def codon_numpy_sum(n: int) -> float:
        import numpy as np
    
        arr = np.arange(n, dtype=np.float64)
        return ((arr * 2.0) + 1.0).sum()
    
    
    def main() -> None:
        n = 1_000_000
    
        t0 = time()
        ans = codon_numpy_sum(n)
        t1 = time()
        print(f"[codon numpy only] result={ans:.1f} time={t1 - t0:.6f}s")
    
    
    if __name__ == "__main__":
        main()
    

    Result

    After uninstalling CPython NumPy:

    $ python3 -m pip uninstall numpy
    Found existing installation: numpy 2.4.4
    Uninstalling numpy-2.4.4:
      Would remove:
        /home/swchoi/.venvs/codon311/bin/f2py
        /home/swchoi/.venvs/codon311/bin/numpy-config
        /home/swchoi/.venvs/codon311/lib/python3.11/site-packages/numpy-2.4.4.dist-info/*
        /home/swchoi/.venvs/codon311/lib/python3.11/site-packages/numpy.libs/libgfortran-040039e1-0352e75f.so.5.0.0
        /home/swchoi/.venvs/codon311/lib/python3.11/site-packages/numpy.libs/libquadmath-96973f99-934c22de.so.0.0.0
        /home/swchoi/.venvs/codon311/lib/python3.11/site-packages/numpy.libs/libscipy_openblas64_-32a4b2a6.so
        /home/swchoi/.venvs/codon311/lib/python3.11/site-packages/numpy/*
    Proceed (Y/n)? Y
      Successfully uninstalled numpy-2.4.4
    

    Running the MRE fails before the Codon-compiled function body is actually used:

    $ python3 codon_numpy_only_demo.py 
    Traceback (most recent call last):
      File "/home/swchoi/src/test_code/python_codon_dsl/codon_numpy_only_demo.py", line 7, in <module>
        import codon
      File "/home/swchoi/src/codon/install/python/codon/__init__.py", line 7, in <module>
        from .decorator import jit, convert, execute, JITError, JITWrapper, _jit_register_fn, _jit_callback_fn, _jit
      File "/home/swchoi/src/codon/install/python/codon/decorator.py", line 13, in <module>
        import numpy as np
    ModuleNotFoundError: No module named 'numpy'
    (codon311) swchoi@swc-WIN:~/src/test_code/python_codon_dsl$ ```
    Running the MRE fails before the Codon-compiled function body is actually used:
    

    Root Cause

    This failure happens even when the user code only intends to use Codon's native NumPy support inside the JIT-compiled function, not CPython's NumPy module directly.

    Looking at codon/install/python/codon/decorator.py, the decorator currently refers to CPython NumPy scalar types:

    pod_conversions = {
        type(None): "pyobj",
        int: "int",
        float: "float",
        bool: "bool",
        str: "str",
        complex: "complex",
        slice: "slice",
        np.bool_: "bool",
        np.int8: "i8",
        np.uint8: "u8",
        np.int16: "i16",
        np.uint16: "u16",
        np.int32: "i32",
        np.uint32: "u32",
        np.int64: "int",
        np.uint64: "u64",
        np.float16: "float16",
        np.float32: "float32",
        np.float64: "float",
        np.complex64: "complex64",
        np.complex128: "complex",
    }
    

    Because of this, importing codon through the Python JIT path implicitly depends on the CPython NumPy package being installed,

    even if Codon Native NumPy is only imported inside the Codon-compiled function body.

    My understanding is that these entries are used to map Python / NumPy scalar types to Codon's statically typed representation.

    Since Python's builtin numeric types do not always encode the exact fixed-width type information needed here, NumPy scalar types are being used as convenient concrete type markers.

    A possible direction may be to avoid requiring CPython NumPy at module import time by using another internal/builtin representation for fixed-width scalar type markers.

    Alternatively, if this dependency is intentional, it may be worth documenting that codon-jit currently requires CPython NumPy for NumPy scalar type conversion support, or just add module dependency,

    even when the compiled function body uses Codon's native NumPy implementation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions