Skip to content

Commit 1b89fcf

Browse files
authored
Split some requirements into extras (#36749)
* Split some requirements into extras * comma * test fixes
1 parent 5e04af8 commit 1b89fcf

File tree

5 files changed

+26
-8
lines changed

5 files changed

+26
-8
lines changed

sdks/python/apache_beam/io/tfrecordio.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,6 @@
2424
import struct
2525
from functools import partial
2626

27-
import crcmod
28-
2927
from apache_beam import coders
3028
from apache_beam.io import filebasedsink
3129
from apache_beam.io.filebasedsource import FileBasedSource
@@ -35,6 +33,16 @@
3533
from apache_beam.io.iobase import Write
3634
from apache_beam.transforms import PTransform
3735

36+
try:
37+
import crcmod
38+
except ImportError:
39+
logging.warning(
40+
'crcmod package not found. This package is required if '
41+
'python-snappy or google-crc32c are not installed. To ensure crcmod is '
42+
'installed, install the tfrecord extra: pip install '
43+
'apache-beam[tfrecord]')
44+
crcmod = None
45+
3846
__all__ = ['ReadFromTFRecord', 'ReadAllFromTFRecord', 'WriteToTFRecord']
3947

4048
_LOGGER = logging.getLogger(__name__)
@@ -67,6 +75,11 @@ def _default_crc32c_fn(value):
6775
pass
6876

6977
if not _default_crc32c_fn.fn:
78+
if crcmod is None:
79+
raise RuntimeError(
80+
'Could not find python-snappy, google-crc32c, or crcmod. To allow '
81+
'execution to succeed, make sure that one of these packages is '
82+
'installed or pip install apache-beam[tfrecord]')
7083
_LOGGER.warning(
7184
'Couldn\'t find python-snappy or google-crc32c so the '
7285
'implementation of _TFRecordUtil._masked_crc32c is not as fast '

sdks/python/apache_beam/io/tfrecordio_test.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333
import zlib
3434
from datetime import datetime
3535

36-
import crcmod
3736
import pytz
3837

3938
import apache_beam as beam
@@ -61,6 +60,11 @@
6160
tf = None # pylint: disable=invalid-name
6261
logging.warning('Tensorflow is not installed, so skipping some tests.')
6362

63+
try:
64+
import crcmod
65+
except ImportError:
66+
crcmod = None
67+
6468
# Created by running following code in python:
6569
# >>> import tensorflow as tf
6670
# >>> import base64
@@ -121,6 +125,7 @@ def test_masked_crc32c(self):
121125
0xe4999b0,
122126
_TFRecordUtil._masked_crc32c(b'\x03\x00\x00\x00\x00\x00\x00\x00'))
123127

128+
@unittest.skipIf(crcmod is None, 'crcmod not installed.')
124129
def test_masked_crc32c_crcmod(self):
125130
crc32c_fn = crcmod.predefined.mkPredefinedCrcFun('crc-32c')
126131
self.assertEqual(

sdks/python/container/common.gradle

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ def generatePythonRequirements = tasks.register("generatePythonRequirements") {
4242
"${files(configurations.sdkSourceTarball.files).singleFile} " +
4343
"base_image_requirements.txt " +
4444
"container " +
45-
"[gcp,dataframe,test] " +
45+
"[gcp,dataframe,test,tfrecord] " +
4646
"${pipExtraOptions}"
4747
}
4848
// Generate versions for ML dependencies
@@ -53,7 +53,7 @@ def generatePythonRequirements = tasks.register("generatePythonRequirements") {
5353
"${files(configurations.sdkSourceTarball.files).singleFile} " +
5454
"base_image_requirements.txt " +
5555
"container/ml " +
56-
"[gcp,dataframe,test,ml_cpu] " +
56+
"[gcp,dataframe,test,ml_cpu,tfrecord] " +
5757
"${pipExtraOptions}"
5858
}
5959
// TODO(https://github.com/apache/beam/issues/36637)
@@ -73,7 +73,7 @@ def generatePythonRequirements = tasks.register("generatePythonRequirements") {
7373
"${files(configurations.sdkSourceTarball.files).singleFile} " +
7474
"gpu_image_requirements.txt " +
7575
"container/ml " +
76-
"[gcp,dataframe,test,tensorflow,torch,transformers,vllm] " +
76+
"[gcp,dataframe,test,tensorflow,tfrecord,torch,transformers,vllm] " +
7777
"${pipExtraOptions}"
7878
}
7979
}

sdks/python/setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -373,7 +373,6 @@ def get_portability_package_data():
373373
},
374374
ext_modules=extensions,
375375
install_requires=[
376-
'crcmod>=1.7,<2.0',
377376
'cryptography>=39.0.0,<48.0.0',
378377
'fastavro>=0.23.6,<2',
379378
'fasteners>=0.3,<1.0',
@@ -596,6 +595,7 @@ def get_portability_package_data():
596595
,
597596
'dill'
598597
],
598+
'tfrecord': ['crcmod>=1.7,<2.0'],
599599
'onnx': [
600600
'onnxruntime==1.13.1',
601601
'torch==1.13.1',

sdks/python/tox.ini

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ pip_pre = True
3333
# allow apps that support color to use it.
3434
passenv=TERM,CLOUDSDK_CONFIG,DOCKER_*,TESTCONTAINERS_*,TC_*,ALLOYDB_PASSWORD
3535
# Set [] options for pip installation of apache-beam tarball.
36-
extras = test,dataframe,yaml
36+
extras = test,dataframe,tfrecord,yaml
3737
# Don't warn that these commands aren't installed.
3838
allowlist_externals =
3939
false

0 commit comments

Comments
 (0)