Skip to content

Commit ba0b526

Browse files
authored
Merge pull request #254 Fix check module exists
2 parents dcdd1ed + 3c04e43 commit ba0b526

File tree

4 files changed

+25
-2
lines changed

4 files changed

+25
-2
lines changed

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
* Fixed bug with ModuleNotFoundError while import ydb
2+
13
## 3.1.0 ##
24
COMPLETE CHANGELOG FROM v2:
35
* Add api for topic service

ydb/_grpc/common/__init__.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
import sys
2-
import importlib.util
32

43
import google.protobuf
54
from packaging.version import Version
5+
from ... import _utilities
66

77
# generated files are incompatible between 3 and 4 protobuf versions
88
# import right generated version for current protobuf lib
99
# sdk code must always import from ydb._grpc.common
1010
protobuf_version = Version(google.protobuf.__version__)
1111

1212
# for compatible with arcadia
13-
if importlib.util.find_spec("ydb.public.api"):
13+
if _utilities.check_module_exists("ydb.public.api"):
1414
from ydb.public.api.grpc import * # noqa
1515

1616
sys.modules["ydb._grpc.common"] = sys.modules["ydb.public.api.grpc"]

ydb/_utilities.py

+10
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# -*- coding: utf-8 -*-
2+
import importlib.util
23
import threading
34
import codecs
45
from concurrent import futures
@@ -76,6 +77,15 @@ def decorator(*args, **kwargs):
7677
return decorator
7778

7879

80+
def check_module_exists(path: str) -> bool:
81+
try:
82+
if importlib.util.find_spec(path):
83+
return True
84+
except ModuleNotFoundError:
85+
pass
86+
return False
87+
88+
7989
def get_query_hash(yql_text):
8090
try:
8191
return hashlib.sha256(str(yql_text, "utf-8").encode("utf-8")).hexdigest()

ydb/_utilities_test.py

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import pytest
2+
3+
from ydb._utilities import check_module_exists
4+
5+
6+
@pytest.mark.parametrize(
7+
"path,result",
8+
[("sys", True), ("ydb", True), ("ydb.some.module.unexisted.test", False)],
9+
)
10+
def test_check_module_exists(path, result):
11+
assert check_module_exists(path) == result

0 commit comments

Comments
 (0)