Lecture 103 · Practical AIBefore you start · one time
Set up Python on your laptop
Do these steps one time. At the end, your laptop has Python, VS Code and a virtual environment with the libraries for this course.
- ~30 min
- Windows · macOS · Linux
- 8 steps
Install Python 3.12 or newer
Go to python.org/downloads. Download the newest Python 3 installer for Windows. Run it.
On the first screen, tick “Add python.exe to PATH”. Then click Install Now. If you forget this box, the
pythoncommand does not work.Go to python.org/downloads. Download the newest Python 3 installer for macOS. Open the
.pkgfile and follow the steps.Ubuntu 24.04 and newer already have Python 3.12. Install the venv and pip parts:
sudo apt update sudo apt install python3 python3-venv python3-pipOther Linux: use your package manager, or download from python.org.
Check Python
Open a new terminal and type:
Terminal = PowerShell. Press the Windows key, type
powershell, press Enter.python --versionYou must see
Python 3.12or a higher number. If you see an error, trypy --version. Ifpyworks, typepywhere this guide sayspython, until step 6.Terminal: press Cmd + Space, type
Terminal, press Enter.python3 --versionYou must see
Python 3.12or a higher number. On macOS and Linux the command ispython3. After step 6, plainpythonalso works.Install VS Code and the Python extension
Download VS Code from code.visualstudio.com and install it.
Open VS Code. Click the Extensions icon on the left (four squares). Search Python. Install the extension from Microsoft.
Make a course folder and open it
Make one folder for this course, for example
ai-ecein your Documents folder. You put the code of each lecture in this folder.In VS Code, click File → Open Folder. Select
ai-ece. If VS Code asks “Do you trust the authors?”, click Yes.Click Terminal → New Terminal. The terminal opens at the bottom, in the
ai-ecefolder. Type all the next commands in this terminal.Terminal basics
You need only these commands. They work in PowerShell, macOS and Linux.
To do this Type this See the folder you are in pwdList the files in this folder lsGo into a folder cd 0-basicsGo up one folder cd ..Run a Python file python 01_first_call.pyStop a running program Ctrl + C Get the last command again ↑ Finish a file or folder name Type the first letters, then Tab Make a virtual environment (venv)
A venv is a private box for the Python libraries of this course. Make it one time, in the
ai-ecefolder:python -m venv .venvTurn it on (PowerShell, the VS Code default on Windows):
.venv\Scripts\Activate.ps1If you see “running scripts is disabled on this system”, read Activate.ps1 is blocked. In Command Prompt (cmd), use
.venv\Scripts\activate.bat.python3 -m venv .venvTurn it on:
source .venv/bin/activateNow the prompt starts with
(.venv). Each time you open a new terminal, turn the venv on again.Install the libraries
The course code uses two libraries:
requests(to call an AI model) andflask(to make a small website). The venv must be on.pip install requests flaskA later lecture can need one more library. Its page tells you the command.
Test it
python -c "import requests, flask; print('Python is ready')"You must see
Python is ready. If you see an error, read Common errors.Tell VS Code to use the venv: press Ctrl + Shift + P (Cmd + Shift + P), type Python: Select Interpreter, and select the one with
.venv.
Each time you start work
- Open the
ai-ecefolder in VS Code. - Click Terminal → New Terminal.
- Turn the venv on:
.venv\Scripts\Activate.ps1source .venv/bin/activate
Common errors
‘python’ is not recognized / command not found: python
Try py --version. If it works, use py in place of python to make the venv. If it does not work, run the Python installer again, click Modify or reinstall, and tick “Add python.exe to PATH”. Close and open the terminal again.
If the Microsoft Store opens when you type python: open Settings → Apps → Advanced app settings → App execution aliases. Turn off the two “python” aliases.
Use python3, not python, until the venv is on. After source .venv/bin/activate, python works.
python --version shows 3.11 or lower
Install the newest Python from python.org (step 1). Close and open the terminal. Check again. Or use py -3.12 to make the venv: py -3.12 -m venv .venv.
Activate.ps1 cannot be loaded because running scripts is disabled
Windows PowerShell blocks scripts by default. Run this one time, then turn the venv on again:
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUserType Y and press Enter if it asks. Or use Command Prompt and .venv\Scripts\activate.bat.
pip: command not found / ‘pip’ is not recognized
Turn the venv on first (step 6). Or use python -m pip install requests flask (or python3 -m pip ...).
On Ubuntu, install pip and venv: sudo apt install python3-pip python3-venv.
ModuleNotFoundError: No module named ‘requests’ (or ‘flask’)
The venv is not on in this terminal. Look for (.venv) at the start of the prompt. Turn the venv on (step 6) and run the program again. If you never installed the libraries, do step 7.
Next: the OpenRouter key and your first AI call are in Exercise 103.1, Part A. How to run the lecture code and the web apps is on the code page.