Skip to content

Installation

PtyLab.py requires Python 3.11 or newer and is distributed on PyPI. Install it in a virtual environment, for example with conda or uv venv.

If you are contributing to the library instead, see the contributing guide.

From PyPI

Tip

The commands below use uv, which installs faster. With plain pip, drop the uv prefix.

uv pip install -U ptylab
uv pip install -U "ptylab[gpu]"

Optional extras

Extra Installs Needed for
gpu CuPy (CUDA 12) GPU reconstruction
torch PyTorch differentiable ptychography (GradientEngine)
gui pyqtgraph, PySide6 the interactive Qt viewer

Verify GPU detection

After installing with the gpu extra, run:

uv run ptylab check gpu

It lists the GPUs that CuPy and PyTorch (if installed) find, with their CUDA versions, and fails if a library cannot use the GPU.

PtyLab uses the GPU when CuPy finds one; set params.gpuSwitch = False to run on the CPU. For large datasets, call engine.convert2single() before reconstruct() to use single precision, which halves memory use.

Differentiable ptychography

GradientEngine reconstructs with PyTorch gradients, so custom models, losses and constraints can be used. Install it with the torch extra:

uv pip install -U "ptylab[torch]"
uv pip install -U "ptylab[gpu,torch]" --torch-backend=cu126 

If there are installation issues on your system, see the PyTorch installation guide.

Interactive viewer (Qt)

With the gui extra, show3Dslider opens a Qt viewer outside a notebook; otherwise it falls back to a matplotlib slider. On a remote machine the window needs a display, such as a remote desktop or ssh -X.

Latest unreleased version

To install the latest unreleased changes on main directly from GitHub:

uv pip install -U git+https://github.com/PtyLab/PtyLab.py.git

Extras work the same way, for example with GPU support:

uv pip install -U "ptylab[gpu] @ git+https://github.com/PtyLab/PtyLab.py.git"