04 Oct Setting Up the Python Environment for AI Guardrails
Before writing more guardrails, let us prepare your computer. In this chapter, you will install Python, create a clean workspace for the tutorial, install the libraries we need, and run a small test script to confirm everything works.
The examples in the first part of this tutorial use plain Python and need no paid account or API key.
What You Need
- A computer with Windows, macOS, or Linux
- Python 3.14 (the current version). Any recent version, such as 3.10 or newer, also works for the examples.
- A code editor, such as Visual Studio Code
- An internet connection to download libraries
Step 1: Install Python
Download Python from the official website, python.org, and run the installer. On Windows, tick the option “Add Python to PATH” on the first screen of the installer. This lets you run Python from the command line.
After installing, open a terminal (Command Prompt or PowerShell on Windows, Terminal on macOS and Linux) and check the version:
python --version
You should see something like Python 3.14.x. On some macOS and Linux systems, the command is python3 instead of python.
python3 --version
Step 2: Create a Project Folder
Create a new folder for this tutorial and move into it.
mkdir ai-guardrails cd ai-guardrails
Step 3: Create a Virtual Environment
A virtual environment is a private box for your project’s libraries. It keeps this project separate from other Python projects on your computer, so libraries do not clash with each other.
Create the virtual environment with this command:
python -m venv venv
This creates a folder named venv inside your project. Now activate it.
On Windows:
venv\Scripts\activate
On macOS and Linux:
source venv/bin/activate
When it is active, you will see (venv) at the start of your terminal line. Whenever you come back to work on the tutorial, activate the environment again.
Step 4: Install the Libraries
Python uses a tool called pip to install libraries. For now, we need only one library, Pydantic, which helps us validate data. We will use it in Chapter 8.
pip install pydantic
Later chapters will ask you to install other libraries, and we will explain each one when it is needed.
Saving Your Library List
You can save the list of installed libraries in a file. This makes it easy to set up the same environment on another computer.
pip freeze > requirements.txt
To install everything from that file later, use:
pip install -r requirements.txt
Step 5: Choose a Code Editor
You can write Python in any text editor, but a code editor makes life easier. Visual Studio Code is a free and popular choice. Install the Python extension to get features such as syntax coloring and error hints.
Step 6: Run a Test Script
Create a file named test_setup.py inside your project folder and add the following code:
import re
import pydantic
# A tiny guardrail: does the text contain a digit?
def contains_digits(text):
return re.search(r"\d", text) is not None
print("Setup is working!")
print("Pydantic version:", pydantic.VERSION)
print(contains_digits("Order 123"))
print(contains_digits("Hello"))
Run it from the terminal:
python test_setup.py
Output
Setup is working! Pydantic version: 2.x.x True False
Your Pydantic version number may be different, and that is fine. What matters is that the script runs without errors.
Understanding the Code
- import re loads Python’s built-in tool for pattern matching.
- import pydantic loads the library we just installed. If this line fails, the installation did not work.
- contains_digits uses the pattern \d, which means “any digit”. It returns True if a digit is found and False otherwise.
- The print lines show a success message, the Pydantic version, and the results for two sample texts.
Common Problems and Fixes
- ‘python’ is not recognized: Python was not added to PATH. Reinstall it and tick the “Add Python to PATH” option, or try python3 instead.
- ModuleNotFoundError: No module named ‘pydantic’: The library is not installed in the active environment. Make sure (venv) is showing, then run pip install pydantic again.
- Activation is blocked on Windows PowerShell: Run the activation command in Command Prompt instead, or allow scripts for your user in PowerShell settings.
- pip is not recognized: Try python -m pip install pydantic.
- A library fails to install on the newest Python: Some libraries take time to support a brand new Python release. If this happens in a later chapter, create the virtual environment with an earlier version, such as Python 3.12 or 3.13, and try again.
A Note About API Keys
The early chapters use a fake AI function, so you can learn the ideas without any cost. In later chapters, we will show how guardrails connect to a real AI model. At that point, you will need an API key from an AI provider. Always keep API keys private and never write them directly into code that you share.
Key Takeaways
- Install the current Python (3.14) and confirm it with python –version.
- Use a virtual environment to keep each project’s libraries separate.
- Install libraries with pip, and save them with pip freeze.
- Run a small test script to confirm your setup before moving on.
What is Next?
In the next chapter, you will build your first real guardrail using keyword lists and regular expressions, and learn how to make them more accurate.
If you liked the tutorial, spread the word and share the link and our website, Studyopedia, with others.
For Videos, Join Our YouTube Channel:Â Join Now
Read More:
- Generative AI Tutorial
- AI Ethics
- Machine Learning Tutorial
- Deep Learning Tutorial
- Ollama Tutorial
- Retrieval Augmented Generation (RAG) Tutorial
- ChatGPT Tutorial
- Microsoft Copilot Tutorial
No Comments