Python for AI and Machine Learning / Start Here

Set up your Python project

Lesson 2 of 30. Open to read. No account needed.

Why the Course Uses Its Own Environment

Python on its own does the language; most machine learning work also needs libraries, packages of code that other people have written and you install. This course uses six:

LibraryVersionUsed for
NumPy2.5.4Arrays of numbers
pandas3.0.6Tables
scikit-learn1.9.1Machine learning models
Matplotlib3.11.2Charts
joblib1.6.0Saving fitted models
pytest9.1.1Running tests

Libraries change between releases: a default value moves, a message is reworded, a result shifts in the third decimal place. The lessons were checked against these exact versions, so you will install exactly them.

Installing them into your computer's main Python could break other programs that need different versions. Instead, you will create a virtual environment: a folder containing its own Python command and its own set of installed libraries. While the environment is active in a terminal, the python command in that terminal uses the environment's libraries; other terminals and programs are unaffected.

The practice folder. Your computer's own Python has only system libraries. Inside it, ~/pyml-practice holds the course environment .venv, with its own python and the six pinned libraries, kept for the whole course; requirements.txt with the six exact versions; and one exercise folder, project-check, removed afterwards. Before activation, python3 runs the computer's Python; after activation, python runs the environment's Python.

Read the diagram from the outside in. The grey box is your computer's own Python. Inside your home folder, the practice folder holds three kinds of thing: the environment .venv, which you keep for the whole course; the file requirements.txt, which lists the exact versions so that the environment can be rebuilt at any time; and one folder per exercise, which you remove when the exercise is finished.

Terminal Commands You Will Use

The exercises use four terminal commands. On macOS and Linux they work in the standard shell; Windows PowerShell accepts the same four names:

  • pwd prints the folder you are in.
  • ls lists the files and folders in it.
  • cd <folder> moves into a folder; cd ~ returns to your home folder.
  • mkdir <folder> creates a folder.

The exercise starts with a short check of these commands. If they are new to you, run that check slowly and compare each result with the description.

Which Python You Need

The course supports Python 3.12 to 3.15. Python 3.12 is the oldest version the pinned libraries accept: NumPy 2.5.4 and SciPy 1.18.1, which scikit-learn installs alongside itself, both require it. Python 3.15.0, released on 9 October 2026, is the newest. For every version in that range, the libraries publish ready-built packages for Windows, macOS and Linux, so installation needs no compiler. A Python newer than 3.15 may fail to install them until such packages exist.

Check what you have. On macOS and Linux:

python3 --version

In Windows PowerShell:

python --version

Illustrative output:

Python 3.15.0

If the command is not found, or the version is below 3.12, install Python first:

  • Windows: install the Python Install Manager from python.org/downloads or from the Microsoft Store, then open a new PowerShell window. It provides the python command.
  • macOS: use the installer from python.org/downloads, then open a new Terminal window. The libraries' ready-built packages need macOS 12 or later on a Mac with Apple silicon, or macOS 10.15 or later on an Intel Mac.
  • Linux: most distributions include Python. If yours is older than 3.12, install a newer version with your distribution's package manager.

Exercise: Create the Course Environment

You will create the practice folder, the course environment inside it, a check script and your first test.

Starting state: Python 3.12 to 3.15 installed; no pyml-practice folder yet. Commands that differ by platform are shown twice: once for macOS and Linux, once for Windows PowerShell. Run only the version for your computer.

  1. Check the terminal commands. macOS and Linux:

    pwd
    mkdir -p ~/pyml-practice/terminal-check
    cd ~/pyml-practice/terminal-check
    pwd
    ls
    cd ~/pyml-practice
    rm -r terminal-check
    ls
    

    Windows PowerShell:

    pwd
    mkdir $HOME\pyml-practice\terminal-check
    cd $HOME\pyml-practice\terminal-check
    pwd
    ls
    cd $HOME\pyml-practice
    Remove-Item terminal-check
    ls
    

    The second pwd prints a path ending in pyml-practice/terminal-check (pyml-practice\terminal-check on Windows), and ls there prints nothing because the folder is empty. The last ls no longer shows terminal-check. The -p option on macOS and Linux creates any missing parent folders, here pyml-practice itself.

  2. Create and activate the environment. macOS and Linux:

    cd ~/pyml-practice
    python3 -m venv .venv
    source .venv/bin/activate
    python --version
    

    Windows PowerShell:

    cd $HOME\pyml-practice
    python -m venv .venv
    .venv\Scripts\Activate.ps1
    python --version
    

    python3 -m venv .venv runs Python's built-in venv module, which creates the environment in a folder named .venv. The leading dot is a common convention; on macOS and Linux it also hides the folder from a plain ls. Activation changes which python this terminal uses, and your prompt now starts with (.venv). From here on, the command is python on every platform, because the active environment provides it.

  3. Save the list of pinned libraries. In your editor, create a file named requirements.txt in your pyml-practice folder. Save this as requirements.txt:

    numpy==2.5.4
    pandas==3.0.6
    scikit-learn==1.9.1
    matplotlib==3.11.2
    joblib==1.6.0
    pytest==9.1.1
    

    Each line names a library and, after ==, the exact version to install.

  4. Install the libraries into the active environment:

    python -m pip install -r requirements.txt
    

    pip is Python's package installer. Running it as python -m pip makes sure it is the environment's own copy, and -r requirements.txt tells it to read the list you saved. It also installs the libraries these six depend on, such as SciPy. The finished environment takes about 460 MB. This can take a few minutes; the last line starts with Successfully installed.

  5. Create the exercise folder and the check script. macOS and Linux:

    mkdir -p ~/pyml-practice/project-check
    cd ~/pyml-practice/project-check
    

    Windows PowerShell:

    mkdir $HOME\pyml-practice\project-check
    cd $HOME\pyml-practice\project-check
    

    Save this as check_setup.py in that folder:

    """Check the course environment: Python version, location and pinned libraries."""
    import sys
    
    import joblib
    import matplotlib
    import numpy
    import pandas
    import pytest
    import sklearn
    
    print("Python:", sys.version.split()[0])
    print("Environment:", sys.prefix)
    print("Python supported:", (3, 12) <= sys.version_info[:2] <= (3, 15))
    
    expected = {
        "NumPy": (numpy.__version__, "2.5.4"),
        "pandas": (pandas.__version__, "3.0.6"),
        "scikit-learn": (sklearn.__version__, "1.9.1"),
        "Matplotlib": (matplotlib.__version__, "3.11.2"),
        "joblib": (joblib.__version__, "1.6.0"),
        "pytest": (pytest.__version__, "9.1.1"),
    }
    for name, (found, wanted) in expected.items():
        result = "OK" if found == wanted else f"expected {wanted}"
        print(f"{name}: {found} {result}")
    

    Run it:

    python check_setup.py
    

    Illustrative output on Linux:

    Python: 3.15.0
    Environment: /home/you/pyml-practice/.venv
    Python supported: True
    NumPy: 2.5.4 OK
    pandas: 3.0.6 OK
    scikit-learn: 1.9.1 OK
    Matplotlib: 3.11.2 OK
    joblib: 1.6.0 OK
    pytest: 9.1.1 OK
    
  6. Save your first test. Save this as test_environment.py in the same folder:

    """A first test: the course environment has the pinned libraries."""
    import joblib
    import matplotlib
    import numpy
    import pandas
    import pytest
    import sklearn
    
    
    def test_pinned_versions():
        assert numpy.__version__ == "2.5.4"
        assert pandas.__version__ == "3.0.6"
        assert sklearn.__version__ == "1.9.1"
        assert matplotlib.__version__ == "3.11.2"
        assert joblib.__version__ == "1.6.0"
        assert pytest.__version__ == "9.1.1"
    

    Run the tests:

    python -m pytest
    

    Illustrative output:

    ============================= test session starts ==============================
    platform linux -- Python 3.15.0, pytest-9.1.1, pluggy-1.6.0
    rootdir: /home/you/pyml-practice/project-check
    collected 1 item
    
    test_environment.py .                                                    [100%]
    
    ============================== 1 passed in 0.95s ===============================
    

How the Check Script and the Test Work

The six import lines load the libraries. If the environment is not active, the first missing one stops the script with an error, which is itself a useful check. scikit-learn is imported as sklearn: the name you install and the name you import differ.

sys.version holds the full Python version text, and .split()[0] keeps only its first word, the version number. sys.prefix is the folder of the Python that is running; with the environment active, it is the .venv folder. sys.version_info[:2] gives the major and minor version as a pair such as (3, 15), and Python compares pairs item by item, so the (3, 12) <= ... <= (3, 15) test is True only inside the supported range.

Each library stores its version in __version__. The expected dictionary pairs each found version with the wanted one, and the loop prints OK or the version the course expects. You will learn dictionaries, loops and f-strings in the next lessons; for now, read the script as "for each library, compare what is installed with what the course wants".

The test file does the same comparison in a different form. pytest looks in the current folder for files whose names start with test_, runs every function in them whose name starts with test_, and reports each one. Inside a test, assert followed by a condition does nothing when the condition is true and makes the test fail when it is false. In the output, the dot after test_environment.py stands for one passed test, and the last line counts passes and failures. The platform line and the time are illustrative. You will write many tests later in the course; here, one passing test shows that python -m pytest works in your environment.

Success: Python supported is True, all six library lines end in OK, the Environment line ends in pyml-practice/.venv (pyml-practice\.venv on Windows), and pytest reports 1 passed.

If something differs:

What you seeLikely causeNext step
python3 or python is not foundPython is not installed, or the terminal was opened before installationInstall Python as described above, then open a new terminal
A version below 3.12An older Python is installedInstall a supported version; recreate the environment with it
PowerShell refuses to run Activate.ps1 because running scripts is disabledWindows does not allow local scripts for your accountSee the note after this table
python3 -m venv says that ensurepip is not availableDebian, Ubuntu and their derivatives package venv support separatelyInstall the venv package for your Python version (for Python 3.12, python3.12-venv), then repeat step 2
pip prints errors about building a package or a missing compilerYour Python version has no ready-built package for a pinned libraryCheck python --version; use Python 3.12 to 3.15
ModuleNotFoundError: No module named 'joblib', or No module named pytestThe environment is not active in this terminalActivate it as in step 2, then rerun
A library line ends with expected ..., or the test failsAnother version is installed in the environmentRun step 4 again in the active environment

Activating on Windows: Python's documentation notes that you may first need to allow local scripts for your user account with this PowerShell command:

Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser

It changes a Windows security setting for your account only; read Microsoft's "About Execution Policies" page, linked from the Python documentation, before running it. If you prefer not to change it, skip activation and type .venv\Scripts\python instead of python in every command, including ..\.venv\Scripts\python check_setup.py from an exercise folder. The environment works without activation; activation only saves typing.

Reset: an environment is disposable because requirements.txt describes it. If it ever breaks, remove .venv and build it again from that file. First deactivate the environment and check you are in the practice folder. macOS and Linux:

deactivate
cd ~/pyml-practice
ls -a
rm -rf .venv
python3 -m venv .venv
source .venv/bin/activate
python -m pip install -r requirements.txt

Windows PowerShell:

deactivate
cd $HOME\pyml-practice
ls -Force
Remove-Item -Recurse .venv
python -m venv .venv
.venv\Scripts\Activate.ps1
python -m pip install -r requirements.txt

Check that ls -a (ls -Force on Windows) lists .venv and requirements.txt before you remove anything. Afterwards, rerun the check script; it prints the same six OK lines.

  1. Clean up the exercise folder, keeping the environment. macOS and Linux:

    cd ~/pyml-practice
    ls
    rm -rf project-check
    ls
    

    Windows PowerShell:

    cd $HOME\pyml-practice
    ls
    Remove-Item -Recurse project-check
    ls
    

    Check that the first ls shows project-check before you remove it. Do not remove .venv or requirements.txt: every later lesson uses them.

Independent task: before step 7, open a new terminal window, go to the exercise folder and run the tests without activating the environment (python3 -m pytest on macOS and Linux, python -m pytest on Windows). Predict what happens first, then run it. Then activate the environment in that terminal, run the tests again and explain the difference.

Check Your Understanding

Three learners report problems:

  1. "My terminal says command not found: python3."
  2. "It says Python 3.11.9."
  3. "It worked yesterday, but today it says No module named pytest."

For each, name the cause and the fix.

Answer and Reasoning

Independent task

Without activation, the terminal runs your computer's own Python, which does not have the course's libraries. On most computers it stops at once with a line ending in No module named pytest, because pytest is not installed there. If your computer's Python happens to have pytest, the test runs but fails on the version checks, or one of the imports fails. After activating the environment in that terminal, pytest reports 1 passed again. Activation applies to one terminal at a time, so every new terminal needs it.

Understanding check

  1. Python is not installed, or the terminal was opened before it was installed. Install a supported Python and open a new terminal.
  2. Python 3.11 is below the course minimum, because NumPy 2.5.4 needs 3.12 or later. Install Python 3.12 to 3.15 and recreate the environment with it.
  3. The environment is not active in today's terminal. Activation lasts only for the terminal where you ran it. Activate it again; there is nothing to reinstall.

References

25 more lessons in the full course

Course access opens every lesson and adds revision notes, the Quick Reference, saved progress, bookmarks and your own notes.

INR 199 plus 18% GST. 90 days of online access. No auto-renewal.

Get the full course

This website is operated by VECTAMETRIC EDTECH (OPC) PRIVATE LIMITED, operating as Vectametric.

Corporate Identity Number (CIN): U85499KA2026OPC229822. GSTIN: 29AAMCV8281D1ZS. Registered office: RS NO/38/2A/1 BASTAND, ROAD MAHANTESH NAGAR, Ramdurg, Ramdurg, Belgaum- 591123, Karnataka.

Queries and grievances: Prajval Honnungar, Grievance Officer and Data Protection Contact.

Telephone: +91 94800 09310. Support: support@vectametric.com. Grievances: support@vectametric.com.

Copyright 2026 VECTAMETRIC EDTECH (OPC) PRIVATE LIMITED. All rights reserved.

Vectametric home · Roadmaps · Vecta IT Exams · Vecta Government Exams · Vecta Banking & Finance Exams · Vecta Entrance Exams · About us · Contact · Privacy Policy · Terms and Conditions · No-Refund Policy