Skip to content

When running AI software with Python, you might frequently 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 commonly used AI tool) with support for different CUDA versions (11.8, 12.4, 12.6). Don't worry, I'll guide you step by step, so even beginners can learn!


Step 1: Understanding Common pip Commands and Error Handling

Common pip Commands

  1. Install a module

    • For example, installing requests (a network tool):
      pip install requests
      Press Enter to download and install it.
  2. Check installed modules

    • Enter:
      pip list
      This will list all the modules you've installed and their versions.
  3. Update pip itself

    • If your pip version is old, enter:
      pip install --upgrade pip
      Keeping it up-to-date can reduce problems.
  4. Uninstall a module

    • If you no longer want a module, such as requests, enter:
      pip uninstall requests
      Enter y as prompted to confirm the deletion.

Common Errors and Solutions

  1. Module version conflicts

    • For example, a message saying "Module X requires version xxx, but version yyy is already installed."
    • Solution: First uninstall the conflicting module (pip uninstall module_name), then install the specified version:
      pip install module_name==version_number
      For example, pip install numpy==1.21.0. You can ask the software author for the version number.
  2. Network connection errors

    • Messages like "Connection timed out" or "Download failed."
    • Solution: Try again a few times, or switch to a different network. If it still doesn't work, the next section will teach you how to use a mirror source for faster downloads.
  3. Module not found error

    • For example, entering pip install xxx and getting a message saying "No module found."
    • Solution: Check your spelling or search online for the correct name (e.g., Google "Python xxx module"). It's also possible that pip is not installed correctly. Enter:
      python -m ensurepip
      python -m pip install --upgrade pip
      Repair and try again.

Step 2: Setting up pip Mirror Sources and Restoring Defaults

Why Use a Mirror Source?

By default, pip downloads modules from overseas servers, which can be slow. Mirror sources are like domestic "proxy servers" that can speed up downloads.

Specifying a Mirror Source

  1. Temporarily Use a Mirror Source

    • Add -i and the mirror address after the command, for example, using the Tsinghua University source:
      pip install numpy -i https://pypi.tuna.tsinghua.edu.cn/simple
      Downloads will be much faster.
  2. Permanently Setting a Mirror Source

    • Open File Explorer and enter the following in the address bar:
      %APPDATA%
      Press Enter to go to C:\Users\YourUsername\AppData\Roaming.
    • Create a folder named pip, go into it, and create a new text file named pip.ini (be 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, and pip will use this source from now on.

Restoring the Default Source

  • If you want to use the official source again, open pip.ini and change the content to:
    [global]
    index-url = https://pypi.org/simple
  • Or simply delete the pip.ini file.

Step 3: Using venv to Create and Manage Virtual Environments

venv is a tool built into Python that can create isolated environments, suitable for running AI software with different project requirements.

  1. Create a Virtual Environment

    • Go to a folder (e.g., a newly created "AIProject"): Enter the folder, delete the content in the address bar, enter 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 a "myenv" subfolder will be created in the folder.
  2. Activate the Virtual Environment

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

    • Enter:
      deactivate
      (myenv) disappearing means you've exited.
  4. Delete the Virtual Environment

    • When you no longer need it, simply delete the myenv folder (right-click > Delete).

Step 4: Installing PyTorch with pip (Supporting CUDA 11.8, 12.4, 12.6)

PyTorch is a commonly used module for AI software that supports GPU acceleration (requires an NVIDIA graphics card and CUDA). We will install different CUDA versions of PyTorch using Windows as an example.

  1. Make Sure You're in the Virtual Environment

    • Enter myenv\Scripts\activate and confirm that (myenv) is present.
  2. Install PyTorch

    • Based on your CUDA version (Tutorial 2 has a method for checking), 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 processes images, and torchaudio processes audio.
  1. Verify the Installation

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

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

Finally: Start Your AI Journey!

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