pixie icon indicating copy to clipboard operation
pixie copied to clipboard

Python API immediately raises an exception on import

Open PSalant726 opened this issue 3 years ago • 2 comments

Describe the bug

Importing the pxapi Python package into a project immediately raises a ModuleNotFoundError.

To Reproduce

Steps to reproduce the behavior:

  1. Optionally create a new Python virtual environment
  2. Install pxapi with pip install pxapi==0.6.0
  3. Start a new Python REPL
  4. Attempt to import pxapi
  5. Observe the raised ModuleNotFoundError exception

Expected behavior

The pxapi package should be importable.

Screenshots

$ python

Python 3.9.13
[Clang 13.1.6 (clang-1316.0.21.2.5)] on darwin
Type "help", "copyright", "credits" or "license" for more information.

>>> import pxapi
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/{{ REDACTED }}/.pyenv/versions/pixie-test/lib/python3.9/site-packages/pxapi/__init__.py", line 19, in <module>
    from .data import Row
  File "/Users/{{ REDACTED }}/.pyenv/versions/pixie-test/lib/python3.9/site-packages/pxapi/data.py", line 23, in <module>
    from src.api.proto.vizierpb import vizierapi_pb2 as vpb
ModuleNotFoundError: No module named 'src.api.proto'

App information (please complete the following information):

  • Pixie version: 0.6.0
  • K8s cluster version: N/A
  • Node Kernel version: N/A
  • Browser version: N/A

Additional context

The Python API is sharing protobuf definition files with the Go API, and so the Python API source files attempt to import resources from beyond the top-level package directory. This issue is likely resolved by publishing a compliant package to PyPI, and either 1) including the required protobuf definition files in the resulting MANIFEST.in file, or 2) packaging the entire /api directory and excluding the Go API files in the resulting MANIFEST.in.

See Python's offical package publishing guide for more information on package requirements.

PSalant726 avatar Aug 24 '22 23:08 PSalant726

Thanks for letting us know.

I wasn't able to reproduce following your instructions, here's what I did.

 pyenv virtualenv pxapi
 pyenv activate pxapi
 pip install pxapi==0.6.0
 python -c "import pxapi"

Did you use a different pip install command or change something in the environment?

philkuz avatar Aug 25 '22 20:08 philkuz

Apologies - I left out one very important detail. The issue only arises in projects importing pxapi that also have their own src/ directory housing their code (likely most projects, and all of mine). Because the pxapi imports from outside the /python directory are stored in their own relative src/ directory, the interpreter is unable to resolve the correct path.

PSalant726 avatar Aug 25 '22 21:08 PSalant726