Running chemistry calculations with uv

2025-10-28

I’m a bit biased, but I really love using uv. I most often use it for testing out Python snippets on different versions of Python with commands like these:

uvx python@3.12
uvx python@3.10 -m ast <<<'with (open("foo.txt")): ...'

So when my good friend Zach started a new postdoc where he needed to use pyscf, I was immediately intrigued. The other open-source quantum chemistry packages I’ve used like psi4 and mopac are solidly in the conda ecosystem. In contrast, pyscf is available on PyPI, where it can be easily installed by package managers like pip and uv.

I plan to play with this more in future posts, but for now I just want to record this great “one-liner” I’ve been using to test out pyscf1:

$ uvx --with pyscf python - <<EOF
from pyscf import gto, scf

mol_h2o = gto.M(atom = 'O 0 0 0; H 0 1 0; H 0 0 1', basis = 'ccpvdz')
rhf_h2o = scf.RHF(mol_h2o)
e_h2o = rhf_h2o.kernel()

print(e_h2o)
EOF
Installed 5 packages in 29ms
converged SCF energy = -76.0167894720742
-76.01678947207424

In case you’re not familiar with uv (or uvx, which is short for uv tool run), you can see a brief snippet of its output here showing that it installed pyscf and its dependencies automatically before invoking the command. The rest of the output is from pyscf.

And in case you’re not familiar with quantum chemistry calculations, this snippet constructs a water molecule with a really terrible geometry (oxygen at the origin, one hydrogen along the y axis and one 90° away on the z axis), and then calculates the energy of the structure using one of the cheapest quantum chemical methods, Hartree-Fock (HF). The HF method is also known as the self-consistent field (SCF) method, hence the mix of HF and SCF in this snippet, and the SCF in pyscf. We also select the somewhat small cc-pVDZ basis set.

That’s all it takes to get started with quantum chemistry calculations with uv!


  1. You can tell I also like using shell heredocs and here strings for this kind of thing.↩︎