Fix #18714: [Docs] python -c "import tvm; print(tvm.file)" fail#19407
Conversation
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
There was a problem hiding this comment.
Code Review
This pull request updates the TVM source installation documentation to include the installation of the tvm-ffi dependency and its addition to the PYTHONPATH. The reviewer suggested using an editable installation for tvm-ffi to ensure the package remains synchronized with the source tree and to avoid redundant entries in the PYTHONPATH.
| pip install $TVM_HOME/3rdparty/tvm-ffi | ||
| export PYTHONPATH=$TVM_HOME/python:$TVM_HOME/3rdparty/tvm-ffi/python:$PYTHONPATH |
There was a problem hiding this comment.
Using an editable install (pip install -e) for tvm-ffi is a more robust approach for a source-based installation. It registers the necessary package metadata to resolve the PackageNotFoundError while keeping the package linked to the source directory. This ensures that tvm-ffi stays in sync with your source tree (e.g., after a git pull) and removes the need to manually add its path to PYTHONPATH. Having the same package in both site-packages and PYTHONPATH is redundant and can lead to import conflicts or version mismatches.
| pip install $TVM_HOME/3rdparty/tvm-ffi | |
| export PYTHONPATH=$TVM_HOME/python:$TVM_HOME/3rdparty/tvm-ffi/python:$PYTHONPATH | |
| pip install -e $TVM_HOME/3rdparty/tvm-ffi | |
| export PYTHONPATH=$TVM_HOME/python:$PYTHONPATH |
Closes #18714
Before
Following the source installation docs, users who set up
PYTHONPATHwith only$TVM_HOME/pythonwould hit animportlib.metadata.PackageNotFoundError: apache-tvm-ffiwhen runningpython -c "import tvm". This happened becausetvm_ffi/__init__.pycallslibinfo.load_lib_ctypes("apache-tvm-ffi", ...), which usesimportlib.metadata.distribution("apache-tvm-ffi")to locate the shared library — a lookup that requires theapache-tvm-ffipackage to be registered in the Python environment, not just present onPYTHONPATH.After
The
tvm-ffipackage at3rdparty/tvm-ffiis installed into the active Python environment viapip installbefore anyimport tvmis attempted. ThePYTHONPATHexport indocs/install/from_source.rstis also updated to include$TVM_HOME/3rdparty/tvm-ffi/pythonalongside$TVM_HOME/python, matching the environment layout expected by the FFI loader.Changes
docs/install/from_source.rst(line ~165–166): Addedpip install $TVM_HOME/3rdparty/tvm-ffistep before theexport PYTHONPATHline in the "set environment variable" path. Updated thePYTHONPATHexport to append$TVM_HOME/3rdparty/tvm-ffi/python, ensuring both the package metadata (from the pip install) and the Python sources are resolvable.Testing
Manual verification on a from-source build with the updated instructions:
No
PackageNotFoundErroris raised;importlib.metadata.distribution("apache-tvm-ffi")resolves correctly after the pip install step.This PR was created with AI assistance (Claude). The changes were reviewed by quality gates and a critic model before submission.