I am wanting to build a tkinter based python program that uses azure speech sdk into a .dmg for the mac os. I am using Mac OS M3 Pro Sonoma.
In spite of confirming that the azure speech is installed in the environment and it is packaged properly using poetry when running the py2app build it fails, saying no module found 'azure'.
Can someone provide some insight as to how to fix py2app to look in correct directory for azure?
We can see that poetry finds the module:
poetry show azure-cognitiveservices-speech
name : azure-cognitiveservices-speech version : 1.41.1 description : Microsoft Cognitive Services Speech SDK for Python
My setup.py is:
import sys
import os
import site
from typing import List, Dict, Any
APP_NAME: str = 'app'
VERSION: str = '1.0.0'
MIN_PYTHON_VERSION: str = '>=3.13'
REQUIRED_PACKAGES: List[str] = [
'azure-cognitiveservices-speech>=1.41.1',
'python-dotenv>=0.19.0',
]
SETUP_REQUIRES: List[str] = [
'py2app',
'setuptools>=44.0.0',
'azure-cognitiveservices-speech>=1.41.1',
]
# Add Azure SDK import at module level with error handling
try:
import azure.cognitiveservices.speech as speech_sdk
SPEECH_SDK_PATH = os.path.dirname(speech_sdk.__file__)
AZURE_ROOT = os.path.dirname(os.path.dirname(os.path.dirname(SPEECH_SDK_PATH)))
except ImportError:
SPEECH_SDK_PATH = None
print("Warning: Azure Speech SDK not found. Build may be incomplete.")
def log_environment_info() -> None:
"""Log environment information for debugging purposes.
Raises:
ImportError: If azure speech SDK cannot be imported
"""
print("Python interpreter being used:", sys.executable)
print("Python path:", sys.path)
print("Site packages:", site.getsitepackages())
try:
# List all files in the azure package directory
azure_dir = os.path.dirname(os.path.dirname(os.path.dirname(speech_sdk.__file__)))
print("Contents of azure package directory:")
for root, dirs, files in os.walk(azure_dir):
print(f"Directory: {root}")
for file in files:
print(f" - {file}")
except ImportError as e:
print("Import Error:", str(e))
raise
# Call this before setup()
log_environment_info()
APP = ['app.py']
DATA_FILES = [
'requirements.txt',
'settings.ini',
'.venv',
'languages.json',
]
# Get the site-packages directory and package paths
SITE_PACKAGES = site.getsitepackages()[0]
AZURE_PATH = os.path.join(SITE_PACKAGES, 'azure')
AZURE_SPEECH_PATH = os.path.join(SITE_PACKAGES, 'azure', 'cognitiveservices', 'speech')
OPTIONS: Dict[str, Any] = {
'argv_emulation': True,
'includes': [
'azure',
'azure.cognitiveservices',
'azure.cognitiveservices.speech',
],
'packages': [
'azure',
'azure.cognitiveservices',
'azure.cognitiveservices.speech',
],
'site_packages': True,
'resources': [
AZURE_ROOT, # Include the entire azure package
SPEECH_SDK_PATH, # Include the speech SDK specifically
],
'frameworks': [],
'plist': {
'CFBundleName': 'App',
'CFBundleShortVersionString': '1.0.0',
'CFBundleVersion': '1.0.0',
'CFBundleIdentifier': 'com.yourdomain.domain',
'NSMicrophoneUsageDescription': 'test app',
'LSMinimumSystemVersion': '10.10',
}
}
setup(
name=APP_NAME,
version=VERSION,
packages=find_packages(),
app=APP,
data_files=DATA_FILES,
options={'py2app': OPTIONS},
setup_requires=SETUP_REQUIRES,
install_requires=REQUIRED_PACKAGES,
python_requires=MIN_PYTHON_VERSION,
description='Kasana Application',
long_description=open('README.md').read(),
long_description_content_type='text/markdown',
author='Your Name',
author_email='your.email@example.com',
url='https://github.com/yourusername/app',
classifiers=[
'Development Status :: 5 - Production/Stable',
'Intended Audience :: Developers',
'Programming Language :: Python :: 3',
'Programming Language :: Python :: 3.13',
],
)
When py2app fails this is the part of the stack trace that seems to be the start of the failure. I've added the PACKAGE DIRECTORY to clearly delineate.
1120 total
42 filtered
150 orphaned
1078 remaining
PACKAGE DIRECTORY /Users/sethhammock/Code/Repos/kasana-production/azure
PACKAGE DIRECTORY /Users/sethhammock/Code/Repos/kasana-production/.venv/lib/python3.11/site-packages/azure/azure
PACKAGE DIRECTORY /Users/sethhammock/Code/Repos/kasana-production/azure
PACKAGE DIRECTORY /Users/sethhammock/.pyenv/versions/3.11.6/lib/python311.zip/azure
PACKAGE DIRECTORY /Users/sethhammock/.pyenv/versions/3.11.6/lib/python3.11/azure
PACKAGE DIRECTORY /Users/sethhammock/.pyenv/versions/3.11.6/lib/python3.11/lib-dynload/azure
PACKAGE DIRECTORY /Users/sethhammock/Code/Repos/kasana-production/.venv/lib/python3.11/site-packages/azure
PACKAGE DIRECTORY /Users/sethhammock/Code/Repos/kasana-production/.venv/lib/python3.11/site-packages/setuptools/_vendor/azure
Traceback (most recent call last):
File "/Users/sethhammock/Code/Repos/kasana-production/setup.py", line 135, in <module>
setup(
File "/Users/sethhammock/Code/Repos/kasana-production/.venv/lib/python3.11/site-packages/setuptools/__init__.py", line 117, in setup
return distutils.core.setup(**attrs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/sethhammock/Code/Repos/kasana-production/.venv/lib/python3.11/site-packages/setuptools/_distutils/core.py", line 183, in setup
return run_commands(dist)
^^^^^^^^^^^^^^^^^^
File "/Users/sethhammock/Code/Repos/kasana-production/.venv/lib/python3.11/site-packages/setuptools/_distutils/core.py", line 199, in run_commands
dist.run_commands()
File "/Users/sethhammock/Code/Repos/kasana-production/.venv/lib/python3.11/site-packages/setuptools/_distutils/dist.py", line 954, in run_commands
self.run_command(cmd)
File "/Users/sethhammock/Code/Repos/kasana-production/.venv/lib/python3.11/site-packages/setuptools/dist.py", line 999, in run_command
super().run_command(command)
File "/Users/sethhammock/Code/Repos/kasana-production/.venv/lib/python3.11/site-packages/setuptools/_distutils/dist.py", line 973, in run_command
cmd_obj.run()
File "/Users/sethhammock/Code/Repos/kasana-production/.venv/lib/python3.11/site-packages/py2app/build_app.py", line 987, in run
self._run()
File "/Users/sethhammock/Code/Repos/kasana-production/.venv/lib/python3.11/site-packages/py2app/build_app.py", line 1217, in _run
self.run_normal()
File "/Users/sethhammock/Code/Repos/kasana-production/.venv/lib/python3.11/site-packages/py2app/build_app.py", line 1329, in run_normal
pkgdirs = self.collect_packagedirs()
^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/sethhammock/Code/Repos/kasana-production/.venv/lib/python3.11/site-packages/py2app/build_app.py", line 1293, in collect_packagedirs
[
File "/Users/sethhammock/Code/Repos/kasana-production/.venv/lib/python3.11/site-packages/py2app/build_app.py", line 1294, in <listcomp>
os.path.join(os.path.realpath(self.get_bootstrap(pkg)), "")
^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/sethhammock/Code/Repos/kasana-production/.venv/lib/python3.11/site-packages/py2app/build_app.py", line 2205, in get_bootstrap
bootstrap = imp_find_module(bootstrap)[1]
^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/sethhammock/Code/Repos/kasana-production/.venv/lib/python3.11/site-packages/modulegraph/util.py", line 34, in imp_find_module
result = imp.find_module(name, path)
^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/sethhammock/.pyenv/versions/3.11.6/lib/python3.11/imp.py", line 298, in find_module
raise ImportError(_ERR_MSG.format(name), name=name)
ImportError: No module named 'azure'```
Key Adjustments: Explicitly set sys.path to include your virtual environment's site-packages directory. Log environment details to help with debugging. Adjust options to ensure all required packages are included.
Try running the build again after making these changes. If the issue persists, it may help to manually verify that the azure packages are correctly installed and accessible in your environment
import sys
import os
import site
from typing import List, Dict, Any
from setuptools import setup, find_packages
APP_NAME = 'app'
VERSION = '1.0.0'
MIN_PYTHON_VERSION = '>=3.13'
REQUIRED_PACKAGES = [
'azure-cognitiveservices-speech>=1.41.1',
'python-dotenv>=0.19.0',
]
SETUP_REQUIRES = [
'py2app',
'setuptools>=44.0.0',
'azure-cognitiveservices-speech>=1.41.1',
]
# Add Azure SDK import at module level with error handling
try:
import azure.cognitiveservices.speech as speech_sdk
SPEECH_SDK_PATH = os.path.dirname(speech_sdk.__file__)
AZURE_ROOT = os.path.dirname(os.path.dirname(os.path.dirname(SPEECH_SDK_PATH)))
except ImportError:
SPEECH_SDK_PATH = None
print("Warning: Azure Speech SDK not found. Build may be incomplete.")
def log_environment_info():
"""Log environment information for debugging purposes."""
print("Python interpreter being used:", sys.executable)
print("Python path:", sys.path)
print("Site packages:", site.getsitepackages())
try:
azure_dir = os.path.dirname(os.path.dirname(os.path.dirname(speech_sdk.__file__)))
print("Contents of azure package directory:")
for root, dirs, files in os.walk(azure_dir):
print(f"Directory: {root}")
for file in files:
print(f" - {file}")
except ImportError as e:
print("Import Error:", str(e))
raise
# Call this before setup()
log_environment_info()
APP = ['app.py']
DATA_FILES = [
'requirements.txt',
'settings.ini',
'.venv',
'languages.json',
]
# Get the site-packages directory and package paths
SITE_PACKAGES = site.getsitepackages()[0]
AZURE_PATH = os.path.join(SITE_PACKAGES, 'azure')
AZURE_SPEECH_PATH = os.path.join(SITE_PACKAGES, 'azure', 'cognitiveservices', 'speech')
OPTIONS = {
'argv_emulation': True,
'includes': [
'azure',
'azure.cognitiveservices',
'azure.cognitiveservices.speech',
],
'packages': [
'azure',
'azure.cognitiveservices',
'azure.cognitiveservices.speech',
],
'site_packages': True,
'resources': [
AZURE_ROOT, # Include the entire azure package
SPEECH_SDK_PATH, # Include the speech SDK specifically
],
'frameworks': [],
'plist': {
'CFBundleName': 'App',
'CFBundleShortVersionString': '1.0.0',
'CFBundleVersion': '1.0.0',
'CFBundleIdentifier': 'com.yourdomain.domain',
'NSMicrophoneUsageDescription': 'test app',
'LSMinimumSystemVersion': '10.10',
}
}
setup(
name=APP_NAME,
version=VERSION,
packages=find_packages(),
app=APP,
data_files=DATA_FILES,
options={'py2app': OPTIONS},
setup_requires=SETUP_REQUIRES,
install_requires=REQUIRED_PACKAGES,
python_requires=MIN_PYTHON_VERSION,
description='Kasana Application',
long_description=open('README.md').read(),
long_description_content_type='text/markdown',
author='Your Name',
author_email='your.email@example.com',
url='https://github.com/yourusername/app',
classifiers=[
'Development Status :: 5 - Production/Stable',
'Intended Audience :: Developers',
'Programming Language :: Python :: 3',
'Programming Language :: Python :: 3.13',
],
)