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
Install a module
- For example, installing
requests
(a network tool):Press Enter to download and install it.pip install requests
- For example, installing
Check installed modules
- Enter:This will list all the modules you've installed and their versions.
pip list
- Enter:
Update pip itself
- If your pip version is old, enter:Keeping it up-to-date can reduce problems.
pip install --upgrade pip
- If your pip version is old, enter:
Uninstall a module
- If you no longer want a module, such as
requests
, enter:Enterpip uninstall requests
y
as prompted to confirm the deletion.
- If you no longer want a module, such as
Common Errors and Solutions
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:For example,pip install module_name==version_number
pip install numpy==1.21.0
. You can ask the software author for the version number.
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.
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:Repair and try again.
python -m ensurepip python -m pip install --upgrade pip
- For example, entering
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
Temporarily Use a Mirror Source
- Add
-i
and the mirror address after the command, for example, using the Tsinghua University source:Downloads will be much faster.pip install numpy -i https://pypi.tuna.tsinghua.edu.cn/simple
- Add
Permanently Setting 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 folder named
pip
, go into it, and create a new text file namedpip.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.
- Open File Explorer and enter the following in the address bar:
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.
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.
- Go to a folder (e.g., a newly created "AIProject"): Enter the folder, delete the content in the address bar, enter
Activate the Virtual Environment
- Enter:Press Enter, and you'll see
myenv\Scripts\activate
(myenv)
if successful.
- Enter:
Deactivate the Virtual Environment
- Enter:
deactivate
(myenv)
disappearing means 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 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.
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 (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
- CUDA 12.4:
torch
is the main module,torchvision
processes images, andtorchaudio
processes audio.
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) andTrue
, 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).
- 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 can run but is 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 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!