You will need Python installed to run these samples. Ideally you will need a developer text editor like Visual Studio Code to create some files and view the code. To get this set up, follow the instructions in the Set up your Python beginner development environment with Visual Studio Code module on Microsoft Learn.
The code needs to know your Speech to Text service region and key. These values can be added to a file called .env
and loaded by the code as environment variables at run time.
-
Open the folder for the sample you want to run in Visual Studio Code
-
Select the New File button in the explorer and create a new file called
.env
-
Add the following to this file:
KEY=<key> REGION=<region>
Set
<key>
to the key you copied when setting up the Speech to Text resource.Set
<region>
to the location you used when setting up the Speech to Text resource.
Python comes in various versions, and Python apps can use external code in packages installed via a tool called pip
. This can lead to problems if different apps need different package versions, or different Python versions. To make it easier to avoid issues with package or Python versions, it is best practice to use virtual environments, self-contained folder trees that contain a Python installation for a particular version of Python, plus a number of additional packages.
-
When the new Visual Studio Code window is opened, the terminal should be opened by default. If not, open a new terminal by selecting Terminal -> New Terminal.
-
Create a new virtual environment called
.venv
using Python 3 by running the following command in the terminalpython3 -m venv .venv
-
A dialog will pop up asking if you want to activate this virtual environment. Select Yes.
-
The existing terminal will not have the virtual environment activated. Close it by selecting the trash can button
-
Create a new terminal by selecting Terminal -> New Terminal. The terminal will load the virtual environment
This code relies on a number of Python packages:
Package | Description |
---|---|
azure-cognitiveservices-speech | The Python SDK for the Azure Speech cognitive service |
python-dotenv | A package that can load environment variables from .env files |
These packages can be restored from the terminal.
-
From the terminal, restore the packages with the following command:
pip install -r requirements.txt
The packages will be installed into the virtual environment.