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
Install a package
- For example, to install
requests
(a networking tool):Press Enter to download and install it.pip install requests
- For example, to install
Check installed packages
- Enter:This will list all the packages you've installed, along with their versions.
pip list
- Enter:
Update pip itself
- If your pip version is outdated, enter:Keeping it up-to-date helps prevent issues.
pip install --upgrade pip
- If your pip version is outdated, enter:
Uninstall a package
- If you no longer need a package, like
requests
, enter:Enterpip uninstall requests
y
when prompted to confirm the removal.
- If you no longer need a package, like
Common Errors and Solutions
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:For example,pip install package_name==version_number
pip install numpy==1.21.0
. You can ask the software developers for the correct version number.
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.
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:This will fix pip, then try again.
python -m ensurepip python -m pip install --upgrade pip
- For example, after entering
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
Temporarily use a mirror source
- Add
-i
and the mirror address after the command. For example, using the Tsinghua University mirror:This will download much faster.pip install numpy -i https://pypi.tuna.tsinghua.edu.cn/simple
- Add
Permanently set a mirror source
- Open File Explorer and enter the following in the address bar:Press Enter to go to
%APPDATA%
C:\Users\YourUsername\AppData\Roaming
. - Create a new folder named
pip
. Inside it, create a new text file and name itpip.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.
- Open File Explorer and enter the following in the address bar:
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.
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.
- Navigate to a folder (e.g., a new folder named "AIProject"): Go into the folder, delete content in the address bar, input
Activate the virtual environment
- Enter:Press Enter, and if you see
myenv\Scripts\activate
(myenv)
, you've successfully activated the environment.
- Enter:
Deactivate the virtual environment
- Enter:The
deactivate
(myenv)
will disappear when you've exited.
- Enter:
Delete the virtual environment
- When you no longer need it, simply delete the
myenv
folder (Right click > Delete).
- When you no longer need it, simply delete the
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.
Make sure you're in the virtual environment
- Enter
myenv\Scripts\activate
and confirm that(myenv)
is present.
- Enter
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
- CUDA 11.8:
torch
is the main module,torchvision
handles images, andtorchaudio
handles audio.
- Based on your CUDA version (see Tutorial 2 for how to check), enter the corresponding command:
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) andTrue
, the installation was successful and your GPU is available. - If it's
False
, check if CUDA is installed correctly (refer to Tutorial 2).
- Enter:
No CUDA installed? Use the CPU version
- If you don't have an NVIDIA graphics card or CUDA, enter:This installs the CPU version, which will work but be slower.
pip install torch torchvision torchaudio
- If you don't have an NVIDIA graphics card or CUDA, enter:
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!