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
Install a Module
- For example, install
requests(a networking tool):Press Enter, and it will download and install.pip install requests
- For example, install
Check Installed Modules
- Enter:This lists all installed modules and their versions.
pip list
- Enter:
Update pip Itself
- If pip is outdated, enter:Keeping it updated helps reduce issues.
pip install --upgrade pip
- If pip is outdated, enter:
Uninstall a Module
- To remove a module, like
requests, enter:Confirm by typingpip uninstall requestsywhen prompted.
- To remove a module, like
Common Errors and Solutions
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:For example,pip install module_name==version_numberpip install numpy==1.21.0. Check with the software author for the correct version.
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.
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:Fix it and try again.
python -m ensurepip python -m pip install --upgrade pip
- For example, "Module not found" after entering
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
Temporarily Use a Mirror Source
- Add
-iand the mirror URL to the command, e.g., using Tsinghua University's source:Downloads will be much faster.pip install numpy -i https://pypi.tuna.tsinghua.edu.cn/simple
- Add
Permanently Set a Mirror Source
- Open File Explorer, type in the address bar:Press Enter to go to
%APPDATA%C:\Users\YourUsername\AppData\Roaming. - Create a new folder named
pip, then inside it, create a text file namedpip.ini(make sure to change the extension). - Open
pip.iniwith Notepad and enter:[global] index-url = https://pypi.tuna.tsinghua.edu.cn/simple - Save and close. From now on, pip will use this source.
- Open File Explorer, type in the address bar:
Restore Default Source
- To switch back to the official source, open
pip.iniand change the content to:[global] index-url = https://pypi.org/simple - Or simply delete the
pip.inifile.
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.
Create a Virtual Environment
- Go to a folder (e.g., a new "AIProject" folder):
In that folder, clear the address bar, typecmd, and press Enter to open the terminal. - In the terminal, enter the command to create the environment:
python -m venv myenvmyenvis the environment name, and it will create a "myenv" subfolder.
- Go to a folder (e.g., a new "AIProject" folder):
Activate the Virtual Environment
- Enter:Press Enter, and if you see
myenv\Scripts\activate(myenv), it's successful.
- Enter:
Deactivate the Virtual Environment
- Enter:The
deactivate(myenv)prompt will disappear.
- Enter:
Delete the Virtual Environment
- When not needed, simply delete the
myenvfolder (right-click > Delete).
- When not needed, simply delete the
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.
Ensure You're in the Virtual Environment
- Enter
myenv\Scripts\activateand confirm you see(myenv).
- Enter
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
- CUDA 11.8:
torchis the main module,torchvisionhandles images, andtorchaudiohandles audio.
- Based on your CUDA version (check with methods from Tutorial 2), enter the corresponding command:
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) andTrue, the installation is successful and GPU is available. - If it's
False, check if CUDA is installed properly (refer to Tutorial 2).
- Enter:
No CUDA? Use CPU Version
- If you don't have an NVIDIA GPU or CUDA, enter:This installs the CPU version, which works but is slower.
pip install torch torchvision torchaudio
- If you don't have an NVIDIA GPU or CUDA, enter:
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!
