Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

Installing PyTorch with Anaconda and CUDA on Windows

Tech 1

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

  • virtualenv creates isolated environments but inherits the system Python version; packages are typically installed via pip or conda.
  • conda combines 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

  1. 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 typically C:\Users\<username>\.conda\envs\torch_env. The Conda executable is at <Anaconda_root>\Scripts\conda.exe.

  2. Terminal Setup
    By default, the PyCharm terminal may activate the base environment. To change to your virtual environment:
    Locate the Anaconda Prompt shortcut, open its properties, and copy the target string starting from cmd.exe. For example:

    cmd.exe "/K" E:\Anaconda3\Scripts\activate.bat E:\Anaconda3
    

    Adjust the activate path to your environment:

    cmd.exe "/K" E:\Anaconda3\Scripts\activate.bat C:\Users\<username>\.conda\envs\torch_env
    

    In PyCharm: File → Settings → Tools → Terminal, paste the adjusted command into "Shell path".

    To prevent auto-activation of base globally:

    conda config --set auto_activate_base false
    

    Then you can manually enter an environment:

    conda activate torch_env
    

Running .ipynb Files in PyCharm

  1. Install Jupyter in your environment

    conda activate torch_env
    conda install notebook
    
  2. 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 list
    

    Remove unnecessary kernels:

    jupyter kernelspec remove python3
    
  3. Set a Jupyter password (avoid token each time)

    jupyter notebook password
    
  4. Launch the notebook server

    jupyter notebook
    

    The console prints a URL like http://localhost:8888/; you can use the token or the password set earlier.

  5. Connect PyCharm Create or open a .ipynb file, 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

Related Articles

Understanding Strong and Weak References in Java

Strong References Strong reference are the most prevalent type of object referencing in Java. When an object has a strong reference pointing to it, the garbage collector will not reclaim its memory. F...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

SBUS Signal Analysis and Communication Implementation Using STM32 with Fus Remote Controller

Overview In a recent project, I utilized the SBUS protocol with the Fus remote controller to control a vehicle's basic operations, including movement, lights, and mode switching. This article is aimed...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.