AI for ECE

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.

Your computer:
  1. 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 python command does not work.

    Go to python.org/downloads. Download the newest Python 3 installer for macOS. Open the .pkg file 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-pip

    Other Linux: use your package manager, or download from python.org.

  2. Check Python

    Open a new terminal and type:

    Terminal = PowerShell. Press the Windows key, type powershell, press Enter.

    python --version

    You must see Python 3.12 or a higher number. If you see an error, try py --version. If py works, type py where this guide says python, until step 6.

    Terminal: press Cmd + Space, type Terminal, press Enter.

    python3 --version

    You must see Python 3.12 or a higher number. On macOS and Linux the command is python3. After step 6, plain python also works.

  3. 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.

  4. Make a course folder and open it

    Make one folder for this course, for example ai-ece in 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-ece folder. Type all the next commands in this terminal.

  5. Terminal basics

    You need only these commands. They work in PowerShell, macOS and Linux.

    To do thisType this
    See the folder you are inpwd
    List the files in this folderls
    Go into a foldercd 0-basics
    Go up one foldercd ..
    Run a Python filepython 01_first_call.py
    Stop a running programCtrl + C
    Get the last command again↑
    Finish a file or folder nameType the first letters, then Tab
  6. 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-ece folder:

    python -m venv .venv

    Turn it on (PowerShell, the VS Code default on Windows):

    .venv\Scripts\Activate.ps1

    If 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 .venv

    Turn it on:

    source .venv/bin/activate

    Now the prompt starts with (.venv). Each time you open a new terminal, turn the venv on again.

  7. Install the libraries

    The course code uses two libraries: requests (to call an AI model) and flask (to make a small website). The venv must be on.

    pip install requests flask

    A later lecture can need one more library. Its page tells you the command.

  8. 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

  1. Open the ai-ece folder in VS Code.
  2. Click Terminal → New Terminal.
  3. 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 CurrentUser

Type 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.