Skip to Content
DocsGetting startedVirtual environment

Setting up a virtual environment

This chapter walks you through creating an isolated Python environment and installing PyTorch and SVETlANNa into it, on Windows, macOS and Linux.

What is a virtual environment, and why bother?

A virtual environment is a self-contained folder holding its own copy of the Python interpreter and its own site-packages directory. Anything you install while the environment is active goes into that folder and nowhere else.

Without one, every pip install writes into a single system-wide location, which causes three recurring problems:

ProblemWhat happens without a virtual environment
Version conflictsProject A needs torch 2.4, project B needs torch 2.9. Only one can win.
Broken system toolsOn Linux and macOS the system Python is used by the OS itself; overwriting its packages can break it.
Irreproducible resultsYou cannot tell which package versions a result was produced with, so a colleague cannot reproduce it.

With a virtual environment each project gets its own dependency set, you never need administrator rights, and deleting the project means deleting one folder.

Rule of thumb: one project — one virtual environment. Create it once, activate it every time you work on the project.

Requirements

  • Python 3.11 or newer — SVETlANNa declares requires-python >= 3.11.
  • Roughly 3 GB of free disk space (PyTorch wheels are large).
  • An NVIDIA GPU is optional; everything below also works on the CPU.

Check the version you have:

py --version

If py is not recognised, install Python from python.org  and tick “Add python.exe to PATH” in the installer.

Step by step

Create a project folder

mkdir my-optics-project cd my-optics-project

Everything below happens inside this folder.

Create the environment

The venv module is part of the standard library, so no extra tooling is needed. The .venv argument is simply the folder the environment is created in — .venv is the conventional name and is recognised by VS Code and PyCharm.

py -3.12 -m venv .venv

Activate it

Activation puts the environment’s python and pip first on your PATH. The shell prompt gains a (.venv) prefix — that is how you know it worked.

PowerShell:

.venv\Scripts\Activate.ps1

Command Prompt (cmd.exe):

.venv\Scripts\activate.bat

If PowerShell refuses with “running scripts is disabled on this system”, allow local scripts once for your user:

Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser

Verify that the right interpreter is in use:

python -c "import sys; print(sys.executable)"

The path printed must point inside your .venv folder.

Update pip

Old pip versions cannot read modern wheel metadata, which produces confusing errors later:

python -m pip install --upgrade pip

Install PyTorch

Install PyTorch first, SVETlANNa second. SVETlANNa does not list torch as a dependency — it deliberately leaves the choice of CPU/CUDA build to you — so pip install svetlanna on its own leaves you without PyTorch.

CPU only:

pip install torch

With CUDA (NVIDIA GPU). Pick the index URL matching your driver on the PyTorch install selector ; cu128 is a current choice:

pip install torch --index-url https://download.pytorch.org/whl/cu128

Install SVETlANNa

pip install svetlanna

This also pulls in matplotlib, numpy, jinja2 and anywidget. To get the optional pandas integration used by some logging helpers:

pip install "svetlanna[pandas]"

To work against the development version instead:

git clone https://github.com/CompPhysLab/SVETlANNa.git cd SVETlANNa pip install -e .

Check that everything works

python -c "import torch, svetlanna; print(torch.__version__); print('svetlanna ok')"

A fuller check, including the accelerator:

import torch from svetlanna import SimulationParameters, Wavefront from svetlanna.units import ureg print("PyTorch:", torch.__version__) print("CUDA available:", torch.cuda.is_available()) print("MPS available:", torch.backends.mps.is_available()) # Apple Silicon params = SimulationParameters.from_ranges( x_range=(-1*ureg.mm, 1*ureg.mm), x_points=128, y_range=(-1*ureg.mm, 1*ureg.mm), y_points=128, wavelength=632.8*ureg.nm, ) wf = Wavefront.plane_wave(params) print("Field shape:", wf.shape)

Expected output:

PyTorch: 2.14.0 CUDA available: False MPS available: True Field shape: torch.Size([128, 128])

Record the versions

So that the environment can be recreated later — by a colleague, or by you on another machine:

pip freeze > requirements.txt

Recreating it elsewhere:

python -m venv .venv source .venv/bin/activate # .venv\Scripts\Activate.ps1 on Windows pip install -r requirements.txt

Everyday use

cd my-optics-project source .venv/bin/activate # .venv\Scripts\Activate.ps1 on Windows # ... work ... deactivate # leave the environment

To delete an environment, just remove the folder — nothing is registered anywhere else:

rm -rf .venv # rmdir /s .venv on Windows

Add .venv/ to your .gitignore. The environment is a build artefact; requirements.txt is what belongs in version control.

Jupyter

To use the environment as a Jupyter kernel:

pip install jupyterlab ipykernel python -m ipykernel install --user --name my-optics-project jupyter lab

Then pick my-optics-project from the kernel list. If import svetlanna fails inside a notebook, you are almost certainly running a different kernel — check with:

import sys; print(sys.executable)

Alternative tools

venv requires nothing beyond Python itself, which is why it is used above. Two popular alternatives do the same job:

uv  is a fast Rust-based installer that is a drop-in replacement for venv + pip:

uv venv --python 3.12 .venv source .venv/bin/activate uv pip install torch svetlanna

Troubleshooting

SymptomCause and fix
ModuleNotFoundError: No module named 'torch'SVETlANNa was installed without PyTorch. Run pip install torch.
ModuleNotFoundError: No module named 'venv'Debian/Ubuntu ships it separately: sudo apt install python3-venv.
running scripts is disabled on this systemPowerShell policy: Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser.
Packages install but imports failThe environment is not active, or the IDE points at another interpreter. Check sys.executable.
torch.cuda.is_available() is False on a GPU machineThe CPU-only wheel was installed. Reinstall from the matching download.pytorch.org/whl/cuXXX index.
pip cannot resolve svetlannaPython is older than 3.11. Check with python --version.

What next?