2.7 Setup Dev Environment¶
In this step, you will configure your Python development environment in Visual Studio Code. At the end of this step, you should have:
- Created a Python virtual environment
- Installed the required Python libraries from
requirements.txt
- Create and populated a
.env
file in the Woodgrove API project. - Connected to your database using pgAdmin
Create a Python virtual environment¶
Virtual environments in Python are essential for maintaining a clean and organized development space, allowing individual projects to have their own set of dependencies, isolated from others. This prevents conflicts between different projects and ensures consistency in your development workflow. By using virtual environments, you can manage package versions easily, avoid dependency clashes, and keep your projects running smoothly. It's a best practice that keeps your coding environment stable and dependable, making your development process more efficient and less prone to issues.
-
Return to Visual Studio Code, where you have the PostgreSQL Solution Accelerator: Build your own AI Copilot project open.
-
In Visual Studio Code, open a new terminal window and change directories to the
src/api
folder of the repo, and create a virtual environment named.venv
by running the following command at the terminal prompt:1 2
cd src/api python -m venv .venv
The above command will create a
.venv
folder under theapi
folder, which will provide a dedicated Python environment for theapi
project that can be used throughout this lab. -
Activate the virtual environment.
Select the appropriate command for your OS and shell from the table.
Platform Shell Command to activate virtual environment POSIX bash/zsh source .venv/bin/activate
fish source .venv/bin/activate.fish
csh/tcsh source .venv/bin/activate.csh
pwsh .venv/bin/Activate.ps1
Windows cmd.exe .venv\Scripts\activate.bat
PowerShell .venv\Scripts\Activate.ps1
macOS bash/zsh source .venv/bin/activate
-
Execute the command at the terminal prompt to activate your virtual environment.
Install required Python libraries¶
The requirements.txt
file in the src\api
folder contains the set of Python libraries needed to run the Python components of the solution accelerator.
Review required libraries
Open the src\api\requirements.txt
file in the repo to review the required libraries and the versions that are being used.
-
From the integrated terminal window in VS Code, run the following command to install the required libraries in your virtual environment:
1
pip install -r requirements.txt
Create .env
file¶
Configuration values, such as connection string and endpoints, that allow your application to interact with Azure services are hosted in an Azure App Configuration service. To enable your application to retrieve these values, you must provide it with the endpoint of that service. You will use a .env
file to host the endpoint as an environment variable, which will allow you to run the Woodgrove API locally. The .env
file will be created within the src\api\app
folder of the project.
-
In VS Code, navigate to the
src\api\app
folder in the Explorer panel. -
Right-click the
app
folder and select New file... from the context menu. -
Enter
.env
as the name of the new file within the VS Code Explorer panel. -
In the
.env
file, add the following as the first line, replacing the{YOUR_APP_CONFIG_ENDPOINT}
with the endpoint for the App Configuration resource in your deployed resource group.1
AZURE_APP_CONFIG_ENDPOINT={YOUR_APP_CONFIG_ENDPOINT}
Retrieve the endpoint for your App Configuration resource
To get the endpoint for your App Configuration resource:
-
Navigate to your App Configuration resource in the Azure portal.
-
Select Access settings from the resource navigation menu, under Settings.
-
Copy the Endpoint value and paste it into the
.env
file.
-
-
Save the
.env
file.
Connect to your database from pgAdmin¶
You will use pgAdmin from your machine to configure various features in the database and execute queries to test those features. The azd up
deployment script added your Microsoft Entra ID user as the owner of the database, so you will authenticate with Entra ID to. Please follow the steps below to connect to your Azure Database for PostgreSQL - Flexible Server using pgAdmin:
-
Navigate to your Azure Database for PostgreSQL - Flexible Server resource in the Azure portal.
-
On the Azure Database for PostgreSQL - Flexible Server page, copy the Server name value from the Essentials panel on the Overview page by selecting the Copy to clipboard button to the right of the value.
-
On your development computer, open pgAdmin.
-
In the pgAdmin Object Explorer, right-click on Servers and in the context menu select Register >, then Server....
-
In tab of Register - Server dialog, follow these steps:
-
On the General tab, enter "PostgreSQLSolutionAccelerator" into the Name field and clear the Connect now option.
-
Select the Connection tab and provide your Azure Database for PostgreSQL flexible server instance details for Hostname/address and Username.
-
Paste the Server name value of your Azure Database for PostgreSQL flexible server into the Host name/address field.
-
The Username value is your Microsoft Entra ID or email.
-
-
Select Save.
-
Right-click the newly added PostgreSQLSolutionAccelerator server in the pgAdmin Object Explorer, and select Connect Server in the context menu.
-
In the Connect to Server dialog, you will need to provide an access token.
To Retrieve Your Microsoft Entra ID Access Token
-
In VS Code, open a new integrated terminal.
-
At the integrated terminal prompt, execute the following command to generate and output an access token:
Bash 1 2
$token = az account get-access-token --resource-type oss-rdbms --output json | ConvertFrom-Json $token.accessToken
-
Copy the output value.
The token is a Base64 string. It encodes all the information about the authenticated user and is targeted to the Azure Database for PostgreSQL service.
-
-
Return to pgAdmin and the Connect to Server dialog and paste the access token into the password field.
Do not save password!
Ensure the Save Password box in the Connect to Server dialog is unchecked. Checking this box can cause your login to fail.
-
Select OK.
Access token expiration
If your access token expires during the course of the workshop, you will need to come back and repeat the above steps to reauthenticate.
-
Leave pgAdmin open as you will be using it throughout the remainder of the workshop.