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:
| Problem | What happens without a virtual environment |
|---|---|
| Version conflicts | Project A needs torch 2.4, project B needs torch 2.9. Only one can win. |
| Broken system tools | On Linux and macOS the system Python is used by the OS itself; overwriting its packages can break it. |
| Irreproducible results | You 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:
Windows
py --versionIf 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-projectEverything 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.
Windows
py -3.12 -m venv .venvActivate 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.
Windows
PowerShell:
.venv\Scripts\Activate.ps1Command Prompt (cmd.exe):
.venv\Scripts\activate.batIf PowerShell refuses with “running scripts is disabled on this system”, allow local scripts once for your user:
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUserVerify 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 pipInstall 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.
Windows
CPU only:
pip install torchWith 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/cu128Install SVETlANNa
pip install svetlannaThis 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.txtRecreating it elsewhere:
python -m venv .venv
source .venv/bin/activate # .venv\Scripts\Activate.ps1 on Windows
pip install -r requirements.txtEveryday use
cd my-optics-project
source .venv/bin/activate # .venv\Scripts\Activate.ps1 on Windows
# ... work ...
deactivate # leave the environmentTo delete an environment, just remove the folder — nothing is registered anywhere else:
rm -rf .venv # rmdir /s .venv on WindowsAdd .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 labThen 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
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 svetlannaTroubleshooting
| Symptom | Cause 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 system | PowerShell policy: Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser. |
| Packages install but imports fail | The environment is not active, or the IDE points at another interpreter. Check sys.executable. |
torch.cuda.is_available() is False on a GPU machine | The CPU-only wheel was installed. Reinstall from the matching download.pytorch.org/whl/cuXXX index. |
pip cannot resolve svetlanna | Python is older than 3.11. Check with python --version. |
What next?
- Installation — GPU notes and dependency details
- Quickstart — a complete optical system in five minutes