Chapter 18 · Hands-on: set up your AI workbench
Set up Python and a virtual environment
A virtual environment is a private box of Python packages for one project, so projects never clash.
Chapter 18 · Hands-on: set up your AI workbench
A virtual environment is a private box of Python packages for one project, so projects never clash.
Project A and project B share everything.
Python packages are add-on libraries. If every project shared one shelf of them, a project that needs a newer version would break an older one. A virtual environment gives each project its own box.
Python is the language most AI tools are written in. Open your terminal and go into your ai-workbench folder (cd ai-workbench, as in the Terminal basics lesson).
Ask Python to report its version. On macOS and Linux the name is usually python3; on Windows it is python.
python --versionCheck: it prints a Python 3 version. If you get ‘not found’ or ‘not recognized’, go to the next step. If it printed a version, skip to step 3.
One popular option is the free installer from the official Python website, python.org. Download the installer for Windows or macOS and run it. On Windows, tick the box that adds Python to PATH on the first screen, so the terminal can find it.
On Linux, use your system’s package manager. On Debian or Ubuntu that is:
sudo apt install python3 python3-venv python3-pipCheck: close the terminal, open a new one, and repeat step 1. A version now prints.
Create a folder for this and later lessons, and step into it.
mkdir my-ai-project
cd my-ai-projectCheck: pwd shows a path ending in my-ai-project.
-m venv asks Python to run its built-in venv tool. .venv is the name of the new hidden folder that holds your private box. It takes a few seconds.
python -m venv .venvCheck: no error appears, and a new folder named .venv exists (ls -a or dir -Force lists it).
Activating tells this terminal window to use the box. You do this once in every new terminal window.
.venv\Scripts\Activate.ps1If PowerShell says running scripts is disabled, run this once in the same window, then try again: Set-ExecutionPolicy -Scope Process -ExecutionPolicy RemoteSigned
Check: your prompt now starts with (.venv).
pip is Python’s package installer. We use python -m pip so pip always belongs to the active environment. requests is a popular library for talking to web addresses; you will need it soon.
python -m pip install requests
python -c "import requests; print('requests works')"Check: the last line prints: requests works
When you are finished, deactivate switches back to normal. Your packages stay saved in .venv for next time.
deactivateCheck: the (.venv) at the start of the prompt disappears.
Paste what your terminal printed. The checker looks for well-known messages and suggests a fix. The text stays in this page and is not sent anywhere.
Why use a virtual environment?
How do you know the environment is active?
You see ‘No module named requests’ right after installing it. What should you check first?