Skip to content

When working with Python for AI software, you'll often encounter pip and venv. pip is a package installer for Python, while venv allows you to create isolated environments to avoid software conflicts. This tutorial will teach you how to use them and guide you through installing PyTorch (a popular AI tool) with support for different CUDA versions (11.8, 12.4, and 12.6). Don't worry, we'll go step by step, so even beginners can follow along!


Step 1: Understanding Common pip Commands and Error Handling

Common pip Commands

  1. Install a package

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

    • Enter:
      pip list
      This will list all the packages you've installed, along with their versions.
  3. Update pip itself

    • If your pip version is outdated, enter:
      pip install --upgrade pip
      Keeping it up-to-date helps prevent issues.
  4. Uninstall a package

    • If you no longer need a package, like requests, enter:
      pip uninstall requests
      Enter y when prompted to confirm the removal.

Common Errors and Solutions

  1. Package version conflicts

    • For example, you might see an error saying "Package X requires version xxx, but version yyy is already installed."
    • Solution: First, uninstall the conflicting package (pip uninstall package_name), then install the specific version you need:
      pip install package_name==version_number
      For example, pip install numpy==1.21.0. You can ask the software developers for the correct version number.
  2. Network connection errors

    • You might see errors like "Connection timed out" or "Download failed."
    • Solution: Try again a few times, or switch to a different network. If the problem persists, the next section will show you how to use a mirror source to speed up downloads.
  3. Package not found error

    • For example, after entering pip install xxx, you might see an error saying "No package found."
    • Solution: Double-check the spelling, or search online for the correct name (e.g., Google "Python xxx module"). It's also possible that pip isn't installed correctly. Try entering:
      python -m ensurepip
      python -m pip install --upgrade pip
      This will fix pip, then try again.

Step 2: Setting Up pip Mirror Sources and Restoring Defaults

Why use a mirror source?

By default, pip downloads packages 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 mirror:
      pip install numpy -i https://pypi.tuna.tsinghua.edu.cn/simple
      This will download much faster.
  2. Permanently set 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 new folder named pip. Inside it, create a new text file and name it 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 the file. pip will now use this mirror source by default.

Restoring the default source

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

Step 3: Using venv to Create and Manage Virtual Environments

venv is a built-in Python tool that lets you create isolated environments, which are ideal for running AI software for different projects.

  1. Create a virtual environment

    • Navigate to a folder (e.g., a new folder named "AIProject"): Go into the folder, delete content in the address bar, input cmd and enter to open the terminal.
    • In the terminal, enter the command to create the environment:
      python -m venv myenv
      myenv is the name of the environment, and it will create a "myenv" subfolder inside the current folder.
  2. Activate the virtual environment

    • Enter:
      myenv\Scripts\activate
      Press Enter, and if you see (myenv), you've successfully activated the environment.
  3. Deactivate the virtual environment

    • Enter:
      deactivate
      The (myenv) will disappear when 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 in AI software that supports GPU acceleration (requiring an NVIDIA graphics card and CUDA). Here's how to install PyTorch with different CUDA versions on Windows.

  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 (see Tutorial 2 for how to check), 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 the installation

    • Enter:
      python -c "import torch; print(torch.__version__); print(torch.cuda.is_available())"
    • If the output is something like 2.6.0 (version number) and True, the installation was successful and your GPU is available.
    • If it's False, check if CUDA is installed correctly (refer to Tutorial 2).
  4. 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 will work but be slower.

Finally: Start Your AI Journey!

Now you know how to use pip to install packages, 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 out in the virtual environment!