Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot import protovalidate ModuleNotFoundError: No module named 'buf' #116

Closed
ramilmsh opened this issue Dec 20, 2023 · 5 comments
Closed
Assignees
Labels
Bug Something isn't working

Comments

@ramilmsh
Copy link

Description

Steps to Reproduce

python3.10 -m venv venv
source ./venv/bin/activate
python --version
# Python 3.10.13
pip install protovalidate --no-cache
# Collecting protovalidate
#   Using cached protovalidate-0.3.1-py3-none-any.whl.metadata (19 kB)
# Collecting cel-python (from protovalidate)
#   Using cached cel_python-0.1.5-py3-none-any.whl (87 kB)
# Collecting protobuf (from protovalidate)
#   Using cached protobuf-4.25.1-cp37-abi3-macosx_10_9_universal2.whl.metadata (541 bytes)
# Collecting jmespath>=0.10.0 (from cel-python->protovalidate)
#   Using cached jmespath-1.0.1-py3-none-any.whl (20 kB)
# Collecting lark-parser>=0.10.1 (from cel-python->protovalidate)
#   Using cached lark_parser-0.12.0-py2.py3-none-any.whl (103 kB)
# Collecting python-dateutil>=2.8.1 (from cel-python->protovalidate)
#   Using cached python_dateutil-2.8.2-py2.py3-none-any.whl (247 kB)
# Collecting pyyaml>=5.4.1 (from cel-python->protovalidate)
#   Using cached PyYAML-6.0.1-cp310-cp310-macosx_11_0_arm64.whl.metadata (2.1 kB)
# Collecting requests>=2.25.1 (from cel-python->protovalidate)
#   Using cached requests-2.31.0-py3-none-any.whl.metadata (4.6 kB)
# Collecting urllib3>=1.26.4 (from cel-python->protovalidate)
#   Using cached urllib3-2.1.0-py3-none-any.whl.metadata (6.4 kB)
# Collecting babel>=2.9.0 (from cel-python->protovalidate)
#   Using cached Babel-2.14.0-py3-none-any.whl.metadata (1.6 kB)
# Collecting six>=1.5 (from python-dateutil>=2.8.1->cel-python->protovalidate)
#   Using cached six-1.16.0-py2.py3-none-any.whl (11 kB)
# Collecting charset-normalizer<4,>=2 (from requests>=2.25.1->cel-python->protovalidate)
#   Using cached charset_normalizer-3.3.2-cp310-cp310-macosx_11_0_arm64.whl.metadata (33 kB)
# Collecting idna<4,>=2.5 (from requests>=2.25.1->cel-python->protovalidate)
#   Using cached idna-3.6-py3-none-any.whl.metadata (9.9 kB)
# Collecting certifi>=2017.4.17 (from requests>=2.25.1->cel-python->protovalidate)
#   Using cached certifi-2023.11.17-py3-none-any.whl.metadata (2.2 kB)
# Using cached protovalidate-0.3.1-py3-none-any.whl (22 kB)
# Using cached protobuf-4.25.1-cp37-abi3-macosx_10_9_universal2.whl (394 kB)
# Using cached Babel-2.14.0-py3-none-any.whl (11.0 MB)
# Using cached PyYAML-6.0.1-cp310-cp310-macosx_11_0_arm64.whl (169 kB)
# Using cached requests-2.31.0-py3-none-any.whl (62 kB)
# Using cached urllib3-2.1.0-py3-none-any.whl (104 kB)
# Using cached certifi-2023.11.17-py3-none-any.whl (162 kB)
# Using cached charset_normalizer-3.3.2-cp310-cp310-macosx_11_0_arm64.whl (120 kB)
# Using cached idna-3.6-py3-none-any.whl (61 kB)
# Installing collected packages: lark-parser, urllib3, six, pyyaml, protobuf, jmespath, idna, charset-normalizer, certifi, babel, requests, python-dateutil, cel-python, protovalidate
# Successfully installed babel-2.14.0 cel-python-0.1.5 certifi-2023.11.17 charset-normalizer-3.3.2 idna-3.6 jmespath-1.0.1 lark-parser-0.12.0 protobuf-4.25.1 protovalidate-0.3.1 python-dateutil-2.8.2 pyyaml-6.0.1 requests-2.31.0 six-1.16.0 urllib3-2.1.0
# 
# [notice] A new release of pip is available: 23.3.1 -> 23.3.2
# [notice] To update, run: pip install --upgrade pip
python
# Python 3.10.13 (main, Aug 24 2023, 12:59:26) [Clang 15.0.0 (clang-1500.0.40.1)] on darwin
# Type "help", "copyright", "credits" or "license" for more information.
# >>> import protovalidate
# Traceback (most recent call last):
#   File "<stdin>", line 1, in <module>
#   File "/Users/r/tmp/2/venv/lib/python3.10/site-packages/protovalidate/__init__.py", line 15, in <module>
#     from protovalidate import validator
#   File "/Users/r/tmp/2/venv/lib/python3.10/site-packages/protovalidate/validator.py", line 19, in <module>
#     from buf.validate import expression_pb2  # type: ignore
# ModuleNotFoundError: No module named 'buf'
# >>>

here is an example in google colab (https://colab.research.google.com/drive/1QuCI88VCUmkIT7-AfrqxPU50OuoePRXO?usp=sharing)

Expected Behavior

import is successful

ModuleNotFoundError: No module named 'buf'

Environment

  • Operating System: macos
  • Version: 14.0 (23A344)
  • Protovalidate Version: 0.3.1, 0.2.1 (didn't try with 0.3.0)

Additional Context

It seems to have started recently (past couple of days, way working fine before). Not sure what changed

@ramilmsh ramilmsh added the Bug Something isn't working label Dec 20, 2023
@sanyuanya
Copy link

I also encountered the same problem, is there any solution?

@Alfus
Copy link
Collaborator

Alfus commented Jan 23, 2024

Hi! This is expected when the generated code for the protovalidate protobuf files is not available. There are several ways of generating the needed files (which is why the code is not bundled). See https://github.com/bufbuild/protovalidate-python?tab=readme-ov-file#generating-code-with-buf on how to generate the code using Buf.

@Alfus Alfus closed this as completed Jan 23, 2024
@ramilmsh
Copy link
Author

@Alfus why was i able to import before?

@ramilmsh
Copy link
Author

@Alfus i have a use-case. I would like to create a library that will attempt to parse a message and then handle the error (one example is return a pretty-formatted 400 error from the server). If I cannot import it that will prove difficult. Is there a workaround like importing on runtime?

@ramilmsh
Copy link
Author

also, how does this work with multiple generated libraries? which one will be imported? is it generated differently for every proto? and if not, why not link it statically in the library?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants