Installing PyTorch with Anaconda and CUDA on Windows
PyTorch represents data as tensors—multi-dimensional arrays of a single data type—wrapped in a class that bundles operations and processing methods. This section covers setting up a working PyTorch environment using Anaconda and CUDA.
Anaconda Setup
Download Anaconda from https://www.anaconda.com/download/ and run the installer. Choose an installation path containing only English characters. Once installed, launch Anaconda Navigator from the Start menu (look for the green icon).
Environment management: virtualenv vs conda
virtualenvcreates isolated environments but inherits the system Python version; packages are typically installed via pip or conda.condacombines environment and package management: it can create environments with any Python version, handle dependencies across languages, and resolve package conflicts effectively.
The default evnironment base (root) confirms a successful installation. You can manage environments using the Anaconda Prompt.
Creating a Virtual Environment
Open any Anaconda command prompt.
List existing environments:
conda env list
Create a new environment with a specific Python version:
conda create -n torch_env python=3.11
Confirm with y. Avoid creating an empty environment (e.g., conda create -n torch_env) as it will lack a Python interpreter.
Configuring Mirror Channels (Optional)
Default channles point to overseas servers, which can be slow. Check current channels:
conda config --show
conda config --get
Available mirrors:
- Tsinghua:
https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main - BFSU:
https://mirrors.bfsu.edu.cn/anaconda/pkgs/main - Alibaba:
http://mirrors.aliyun.com/anaconda/pkgs/main
Add a channel:
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
It is safer to specify the channel during package installation rather than permanently modifying the configuration.
Installing PyTorch with CUDA
Activate the target environment:
conda activate torch_env
Check your NVIDIA driver and CUDA version:
nvidia-smi
Note the reported CUDA Version.
pip Method (Recommended)
pip3 install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu118
Replace cu118 with your CUDA version if different.
conda Method (Alternative)
Using a mirror for cudatoolkit:
conda install cudatoolkit=11.8 -c https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
conda install pytorch torchvision torchaudio -c https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/pytorch/win-64/
This approach has more potential dependency conflicts.
Verifying the Installation
Check installed packages:
conda activate torch_env
pip list | findstr torch
Launch Python and test GPU availability:
import torch
print(torch.cuda.is_available())
True indicates a successful GPU-enabled PyTorch installation.
PyCharm Integration
-
Configure Python Interpreter
In PyCharm, go to File → Settings → Project → Python Interpreter. Add a new Conda environment or select an existing one. The environment path is typicallyC:\Users\<username>\.conda\envs\torch_env. The Conda executable is at<Anaconda_root>\Scripts\conda.exe. -
Terminal Setup
By default, the PyCharm terminal may activate thebaseenvironment. To change to your virtual environment:
Locate the Anaconda Prompt shortcut, open its properties, and copy the target string starting fromcmd.exe. For example:cmd.exe "/K" E:\Anaconda3\Scripts\activate.bat E:\Anaconda3Adjust the activate path to your environment:
cmd.exe "/K" E:\Anaconda3\Scripts\activate.bat C:\Users\<username>\.conda\envs\torch_envIn PyCharm: File → Settings → Tools → Terminal, paste the adjusted command into "Shell path".
To prevent auto-activation of
baseglobally:conda config --set auto_activate_base falseThen you can manually enter an environment:
conda activate torch_env
Running .ipynb Files in PyCharm
-
Install Jupyter in your environment
conda activate torch_env conda install notebook -
Set up an IPython kernel
python -m ipykernel install --user --name torch_env_kernel --display-name "Python [conda env:torch_env]"List kernels:
jupyter kernelspec listRemove unnecessary kernels:
jupyter kernelspec remove python3 -
Set a Jupyter password (avoid token each time)
jupyter notebook password -
Launch the notebook server
jupyter notebookThe console prints a URL like
http://localhost:8888/; you can use the token or the password set earlier. -
Connect PyCharm Create or open a
.ipynbfile, click the Jupyter server configuration, and enter the server URL (e.g.,http://localhost:8888/).
If a cell remains stuck on In[*], update tornado and ipykernel:
pip install tornado --upgrade
pip install ipykernel --upgrade
Troubleshooting: "Not a valid Win32 application"
An error like OSError: [WinError 193] %1 is not a valid Win32 application when importing torch often stems from DLL conflicts or corrupted installations. A clean environment often resolves it:
conda create -n fresh_torch python=3.9
conda activate fresh_torch
pip install numpy
pip3 install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu118
Verify:
import torch
print(torch.cuda.is_available())
Removing a Virtual Environment
To delete an environment completely:
conda deactivate
conda env remove --name torch_env