Creating Python Virtual Environment With CUDA

Mohamad MahmoodMohamad Mahmood
3 min read

[1] Install packages


POWERSHELL INSTALL COMMAND

python -m venv c:/ZLAB201/myenv201

C:/ZLAB201/myenv201/Scripts/Activate.ps1

pip install torch==2.1.0+cu121 --index-url https://download.pytorch.org/whl/cu121

pip install numpy==1.26 pandas==2.2.3

pip install ipywidgets

[2] check via python notebook:

check installed packages:

import sys
import os
import numpy

def test_environment():
    print("Testing Virtual Environment: myenv")
    print("Python Executable:", sys.executable)
    print("Python Version:", sys.version)

    # Checking if numpy is installed
    try:
        import numpy as np
        print("NumPy Version:", np.__version__)
    except ImportError:
        print("NumPy is not installed. Installing it now...")
        os.system("pip install numpy")
        import numpy as np
        print("NumPy Version after installation:", np.__version__)

if __name__ == "__main__":
    test_environment()

Output:

Testing Virtual Environment: myenv
Python Executable: c:\ZML1\myenv1\Scripts\python.exe
Python Version: 3.11.9 (tags/v3.11.9:de54cf5, Apr  2 2024, 10:12:12) [MSC v.1938 64 bit (AMD64)]
NumPy Version: 1.26.0

check CUDA:

import torch
print("Torch Version:", torch.__version__)
print("CUDA Available:", torch.cuda.is_available())
print("CUDA Version:", torch.version.cuda)
print("GPU:", torch.cuda.get_device_name(0) if torch.cuda.is_available() else "None")

Output:

Torch Version: 2.1.0+cu121
CUDA Available: True
CUDA Version: 12.1
GPU: NVIDIA GeForce RTX 3050 Ti Laptop GPU

some compatible versions:

Package                   Version
------------------------- --------------
anyio                     4.8.0
argon2-cffi               23.1.0
argon2-cffi-bindings      21.2.0
arrow                     1.3.0
asttokens                 3.0.0
async-lru                 2.0.4
attrs                     25.3.0
babel                     2.17.0
beautifulsoup4            4.13.3
bleach                    6.2.0
certifi                   2025.1.31
cffi                      1.17.1
charset-normalizer        3.4.1
click                     8.1.8
colorama                  0.4.6
comm                      0.2.2
cramjam                   2.9.1
debugpy                   1.8.13
decorator                 5.2.1
defusedxml                0.7.1
emoji                     2.14.1
executing                 2.2.0
fastjsonschema            2.21.1
fastparquet               2024.11.0
fasttext-wheel            0.9.2
filelock                  3.13.1
fqdn                      1.5.1
fsspec                    2024.6.1
gdown                     5.2.0
h11                       0.14.0
httpcore                  1.0.7
httpx                     0.28.1
huggingface-hub           0.29.3
idna                      3.10
ipykernel                 6.29.5
ipython                   9.0.2
ipython_pygments_lexers   1.1.1
ipywidgets                8.1.5
isoduration               20.11.0
jedi                      0.19.2
Jinja2                    3.1.4
joblib                    1.4.2
json5                     0.10.0
jsonpointer               3.0.0
jsonschema                4.23.0
jsonschema-specifications 2024.10.1
jupyter                   1.1.1
jupyter_client            8.6.3
jupyter-console           6.6.3
jupyter_core              5.7.2
jupyter-events            0.12.0
jupyter-lsp               2.2.5
jupyter_server            2.15.0
jupyter_server_terminals  0.5.3
jupyterlab                4.3.5
jupyterlab_pygments       0.3.0
jupyterlab_server         2.27.3
jupyterlab_widgets        3.0.13
MarkupSafe                2.1.5
matplotlib-inline         0.1.7
mistune                   3.1.2
mpmath                    1.3.0
nbclient                  0.10.2
nbconvert                 7.16.6
nbformat                  5.10.4
nest-asyncio              1.6.0
networkx                  3.3
nltk                      3.9.1
notebook                  7.3.2
notebook_shim             0.2.4
numpy                     1.26.0
overrides                 7.7.0
packaging                 24.2
pandas                    2.2.3
pandocfilters             1.5.1
parso                     0.8.4
pillow                    11.1.0
pip                       25.0.1
platformdirs              4.3.6
prometheus_client         0.21.1
prompt_toolkit            3.0.50
psutil                    7.0.0
pure_eval                 0.2.3
pybind11                  2.13.6
pycparser                 2.22
pycryptodomex             3.21.0
Pygments                  2.19.1
PySocks                   1.7.1
python-dateutil           2.9.0.post0
python-json-logger        3.3.0
pytz                      2025.1
pywin32                   309
pywinpty                  2.0.15
PyYAML                    6.0.2
pyzipper                  0.3.6
pyzmq                     26.2.1
referencing               0.36.2
regex                     2024.11.6
requests                  2.32.3
rfc3339-validator         0.1.4
rfc3986-validator         0.1.1
rpds-py                   0.23.1
safetensors               0.5.3
scikit-learn              1.2.2
scipy                     1.15.2
Send2Trash                1.8.3
sentence-transformers     3.4.1
sentencepiece             0.2.0
setuptools                76.0.0
six                       1.17.0
sniffio                   1.3.1
soupsieve                 2.6
stack-data                0.6.3
sympy                     1.13.1
terminado                 0.18.1
threadpoolctl             3.6.0
tinycss2                  1.4.0
tokenizers                0.21.1
torch                     2.1.0+cu121
tornado                   6.4.2
tqdm                      4.67.1
traitlets                 5.14.3
transformers              4.49.0
types-python-dateutil     2.9.0.20241206
typing_extensions         4.12.2
tzdata                    2025.1
uri-template              1.3.0
urllib3                   2.3.0
wcwidth                   0.2.13
webcolors                 24.11.1
webencodings              0.5.1
websocket-client          1.8.0
wheel                     0.45.1
widgetsnbextension        4.0.13

[3] Install transformers

pip install transformers==4.51.3

Run a test code:

from transformers import MobileBertTokenizer, MobileBertModel
import torch

# Use GPU if available
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")

# Load tokenizer and model
tokenizer = MobileBertTokenizer.from_pretrained("google/mobilebert-uncased")
model = MobileBertModel.from_pretrained("google/mobilebert-uncased").to(device)

# Tokenize input
inputs = tokenizer("Hello, MobileBERT!", return_tensors="pt").to(device)

# Run inference
with torch.no_grad():
    outputs = model(**inputs)

# Output shape of last hidden state
print("Last hidden states shape:", outputs.last_hidden_state.shape)

Output:

Last hidden states shape: torch.Size([1, 7, 512])

[4] Duplicate project containing venv scripts

  1. In the original project e.g venv200 folder, get the installed package list, store in a subfolder e.g. setup:

    1.    mkdir -p ./setup && pip freeze > ./setup/requirements.txt
      
  2. Copy only the project files , not the venv.

  3. In the new project folder create a new venv folder, e.g. venv300

    1.  (Assuming that you are already in the venv project root folder, 
        and you have already created requirements.txt file in setup folder)
      
         python -m venv venv300
         ./venv300/Scripts/Activate.ps1
         pip install -r ./setup/requirements.txt
      
  4. Note: If your requirements.txt contains CUDA-related packages like the one below, the pip installation will fail. Remove this line from the requirements.txt file before proceeding.

    1. torch==2.1.0+cu121
  5. Run the CUDA-related package installation separately in the terminal:

    1. pip install torch==2.1.0+cu121 --index-url https://download.pytorch.org/whl/cu121
  6. .

0
Subscribe to my newsletter

Read articles from Mohamad Mahmood directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Mohamad Mahmood
Mohamad Mahmood

Mohamad's interest is in Programming (Mobile, Web, Database and Machine Learning). He studies at the Center For Artificial Intelligence Technology (CAIT), Universiti Kebangsaan Malaysia (UKM).