Skip to content

When running AI software with Python, you'll often use pip and venv. pip is a tool for installing Python packages, while venv helps you create isolated environments to avoid software conflicts. This tutorial will teach you how to use them and guide you through installing PyTorch, a common AI tool, with support for different CUDA versions (11.8, 12.4, 12.6). Don't worry, I'll guide you step by step—even if you're starting from scratch, you can do it!


Step 1: Learn Common pip Commands and Error Handling

Common pip Commands

  1. Install a Module

    • For example, install requests (a networking tool):
      pip install requests
      Press Enter, and it will download and install.
  2. Check Installed Modules

    • Enter:
      pip list
      This lists all installed modules and their versions.
  3. Update pip Itself

    • If pip is outdated, enter:
      pip install --upgrade pip
      Keeping it updated helps reduce issues.
  4. Uninstall a Module

    • To remove a module, like requests, enter:
      pip uninstall requests
      Confirm by typing y when prompted.

Common Errors and Solutions

  1. Module Version Conflicts

    • For example, "Module X requires version xxx, but version yyy is installed."
    • Solution: First uninstall the conflicting module (pip uninstall module_name), then install the specific version:
      pip install module_name==version_number
      For example, pip install numpy==1.21.0. Check with the software author for the correct version.
  2. Network Connection Errors

    • Messages like "Connection timed out" or "Download failed."
    • Solution: Try again or switch networks. If it persists, the next section shows how to use mirror sources for faster downloads.
  3. Module Not Found Error

    • For example, "Module not found" after entering pip install xxx.
    • Solution: Check the spelling or search online for the correct name (e.g., Google "Python xxx module"). If pip isn't working, enter:
      python -m ensurepip
      python -m pip install --upgrade pip
      Fix it and try again.

Step 2: Set Up pip Mirror Sources and Restore Default

Why Use Mirror Sources?

By default, pip downloads modules from overseas servers, which can be slow. Mirror sources act as local "proxy servers" to speed up downloads.

Specify Mirror Sources

  1. Temporarily Use a Mirror Source

    • Add -i and the mirror URL to the command, e.g., using Tsinghua University's source:
      pip install numpy -i https://pypi.tuna.tsinghua.edu.cn/simple
      Downloads will be much faster.
  2. Permanently Set a Mirror Source

    • Open File Explorer, type in the address bar:
      %APPDATA%
      Press Enter to go to C:\Users\YourUsername\AppData\Roaming.
    • Create a new folder named pip, then inside it, create a text file named pip.ini (make sure to change the extension).
    • Open pip.ini with Notepad and enter:
      [global]
      index-url = https://pypi.tuna.tsinghua.edu.cn/simple
    • Save and close. From now on, pip will use this source.

Restore Default Source

  • To switch back to the official source, open pip.ini and change the content to:
    [global]
    index-url = https://pypi.org/simple
  • Or simply delete the pip.ini file.

Step 3: Use venv to Create and Manage Virtual Environments

venv is a built-in Python tool that creates isolated environments, ideal for running AI software for different projects.

  1. Create a Virtual Environment

    • Go to a folder (e.g., a new "AIProject" folder):
      In that folder, clear the address bar, type cmd, and press Enter to open the terminal.
    • In the terminal, enter the command to create the environment:
      python -m venv myenv
      myenv is the environment name, and it will create a "myenv" subfolder.
  2. Activate the Virtual Environment

    • Enter:
      myenv\Scripts\activate
      Press Enter, and if you see (myenv), it's successful.
  3. Deactivate the Virtual Environment

    • Enter:
      deactivate
      The (myenv) prompt will disappear.
  4. Delete the Virtual Environment

    • When not needed, simply delete the myenv folder (right-click > Delete).

Step 4: Install PyTorch with pip (Supports CUDA 11.8, 12.4, 12.6)

PyTorch is a common module for AI software, supporting GPU acceleration (requires an NVIDIA GPU and CUDA). Here's how to install PyTorch for different CUDA versions on Windows.

  1. Ensure You're in the Virtual Environment

    • Enter myenv\Scripts\activate and confirm you see (myenv).
  2. Install PyTorch

    • Based on your CUDA version (check with methods from Tutorial 2), enter the corresponding command:
      • CUDA 11.8:
        pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu118
      • CUDA 12.4:
        pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu124
      • CUDA 12.6:
        pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu126
    • torch is the main module, torchvision handles images, and torchaudio handles audio.
  3. Verify Installation

    • Enter:
      python -c "import torch; print(torch.__version__); print(torch.cuda.is_available())"
    • If it outputs something like 2.6.0 (version number) and True, the installation is successful and GPU is available.
    • If it's False, check if CUDA is installed properly (refer to Tutorial 2).
  4. No CUDA? Use CPU Version

    • If you don't have an NVIDIA GPU or CUDA, enter:
      pip install torch torchvision torchaudio
      This installs the CPU version, which works but is slower.

Final Step: Start Your AI Journey!

Now you know how to use pip to install modules, set up mirror sources, manage environments with venv, and install PyTorch. If your AI software has a run command (e.g., python run.py), try it in the virtual environment!