To create a **virtual environment (venv)** using **Python 3.11** or older

#1
by louiecerv - opened

To create a virtual environment (venv) using Python 3.11 while your system has Python 3.13 installed, follow these steps:


๐Ÿ› ๏ธ Step-by-Step Instructions

1๏ธโƒฃ Check Installed Python Versions

First, confirm that Python 3.11 is installed on your system.

Run this command:

python3 --version

or

python --version

If it returns Python 3.13, you need to check if Python 3.11 is available.

๐Ÿ‘‰ Windows:

List available Python versions:

py --list

If Python 3.11 appears, use py -3.11 instead of python.

๐Ÿ‘‰ macOS/Linux:

List installed Python versions:

ls /usr/bin/python*

If you see /usr/bin/python3.11, you can use python3.11 instead of python.


2๏ธโƒฃ Create a Virtual Environment Using Python 3.11

Run one of the following commands depending on your OS:

๐Ÿ‘‰ Windows (PowerShell / CMD)

py -3.11 -m venv myenv

or (if py -3.11 does not work)

C:\Path\To\Python311\python.exe -m venv myenv

๐Ÿ‘‰ macOS/Linux

python3.11 -m venv myenv

or (if python3.11 is not recognized)

/usr/bin/python3.11 -m venv myenv

This will create a virtual environment named myenv.


3๏ธโƒฃ Activate the Virtual Environment

Once the virtual environment is created, activate it:

๐Ÿ‘‰ Windows (PowerShell)

myenv\Scripts\Activate

๐Ÿ‘‰ Windows (CMD)

myenv\Scripts\activate.bat

๐Ÿ‘‰ macOS/Linux

source myenv/bin/activate

After activation, your terminal should show something like:

(myenv) user@computer:~$

4๏ธโƒฃ Install PyTorch for Python 3.11

Now that you have the virtual environment, install PyTorch:

๐Ÿ‘‰ For CPU (No CUDA)

pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cpu

๐Ÿ‘‰ For GPU (CUDA 11.8)

pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu118

5๏ธโƒฃ Verify the Installation

Run Python inside your virtual environment:

python

Then, test if PyTorch is installed correctly:

import torch
print(torch.__version__)
print(torch.cuda.is_available())  # Should return True if GPU is available

6๏ธโƒฃ Use the Virtual Environment

Now, whenever you need to run your project, activate the environment first:

source myenv/bin/activate  # macOS/Linux
myenv\Scripts\Activate      # Windows (PowerShell)

To deactivate the environment, simply run:

deactivate

โœ… Summary

1๏ธโƒฃ Ensure Python 3.11 is installed.
2๏ธโƒฃ Create a virtual environment:

  • Windows: py -3.11 -m venv myenv
  • macOS/Linux: python3.11 -m venv myenv
    3๏ธโƒฃ Activate the environment.
    4๏ธโƒฃ Install PyTorch using pip.
    5๏ธโƒฃ Verify the installation.
    6๏ธโƒฃ Use source myenv/bin/activate (or myenv\Scripts\Activate on Windows) to activate it when needed.

Now your environment is set up with Python 3.11 and PyTorch! ๐Ÿš€

Sign up or log in to comment