deepgram / deepgram-python-sdk

Official Python SDK for Deepgram's automated speech recognition APIs.
https://developers.deepgram.com
MIT License
175 stars 47 forks source link

v3.3.0 needs pyaudio but doesn't specify it as a dependency #418

Closed Tenzer closed 1 week ago

Tenzer commented 1 week ago

What is the current behavior?

Installing v3.3.0 of this library and trying to import deepgram gives a stack trace because deepgram-sdk depends on pyaudio, but it isn't installed as a dependency.

Steps to reproduce

  1. Create an empty virtual environment: python3 -m venv venv
  2. Install deepgram-sdk v3.3.0 in the virtual environment: venv/bin/pip install deepgram-sdk==3.3.0
  3. Try to import the library: venv/bin/python -c 'import deepgram'

It gives a stack trace like this:

Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/your/path/venv/lib/python3.12/site-packages/deepgram/__init__.py", line 129, in <module>
    from .audio import Microphone
  File "/your/path/venv/lib/python3.12/site-packages/deepgram/audio/__init__.py", line 5, in <module>
    from .microphone import Microphone
  File "/your/path/venv/lib/python3.12/site-packages/deepgram/audio/microphone/__init__.py", line 5, in <module>
    from .microphone import Microphone
  File "/your/path/venv/lib/python3.12/site-packages/deepgram/audio/microphone/microphone.py", line 15, in <module>
    class Microphone:  # pylint: disable=too-many-instance-attributes
  File "/your/path/venv/lib/python3.12/site-packages/deepgram/audio/microphone/microphone.py", line 23, in Microphone
    import pyaudio  # pylint: disable=import-outside-toplevel
    ^^^^^^^^^^^^^^
ModuleNotFoundError: No module named 'pyaudio'

Expected behavior

It should be possible to install the library and import it without having to install extra dependencies manually.

Please tell us about your environment

This happens regardless of operating system. I have only tested on Python 3.12.

Other information

dvonthenen commented 1 week ago

That's actually a 100% intentional.

Please see: https://github.com/deepgram/deepgram-python-sdk/issues/302

In case that issue isn't clear, on certain platforms, the pip install deepgram-sdk will fail if portaudio isn't installed. That would force people with a hard requirement to install portaudio just to use the SDK when they might not be interested in any of the examples that use it.

All the examples that use the Microphone have documentation in the README.md to install (like brew install portaudio or whatever it is for your platform) before running.

Tenzer commented 1 week ago

This wasn't a problem in version 3.2.7 which is why I am reporting it.

The problem seems to stem from the import which is done at the class-level here: https://github.com/deepgram/deepgram-python-sdk/blob/344cb461b3cc444a37ea1979c14bf60d394bbd72/deepgram/audio/microphone/microphone.py#L23

It attempts to import pyaudio as soon as the Microphone class is parsed by Python, which is done as soon as import deepgram is done.

Since that import is only used for type checking, it can instead be put in a conditional, so this isn't a problem. I'll make a PR in a moment to demonstrate.

dvonthenen commented 1 week ago

ooooh you are reportng that it is giving you the error. now I see what you are saying. If you want to do the PR to remove the line, that would be awesome. Otherwise, I can.

Tenzer commented 1 week ago

Yep, exactly. See #419.

dvonthenen commented 1 week ago

I think TYPE_CHECKING was introduced a little later in v3. I will need to see if this works for the supported versions of of the SDK.

dvonthenen commented 1 week ago

@Tenzer would you happen to know if this works for 3.10? and I have been working on a side project on getting the SDK to work on 3.9 (because some have been complaining about this) but I haven't been able to devote a ton of time to it.

Tenzer commented 1 week ago

As mentioned here, typing.TYPING_CHECKING was added in Python 3.5.2 according to the documentation, so I don't see why it shouldn't work.

dvonthenen commented 1 week ago

@Tenzer thanks again for the PR. just posted a new release with this fix: https://github.com/deepgram/deepgram-python-sdk/releases/tag/v3.3.1