How to Make Python Packages Import Faster the First Time with uv --compile-bytecode
The first time you import a large package in a new Python environment, it's slower than every time after. Part of that time goes to compiling the package's source files, and uv can do that work while it installs instead, with --compile-bytecode. This guide measures what that saves on SymPy, with uv 0.13.10 and Python 3.15.2.
Syntax
uv sync --compile-bytecode
| Part | What it means |
|---|---|
uv sync | Installs the project's packages into its .venv, as uv.lock lists them. uv add and uv pip install take the same option |
--compile-bytecode | After installing, compiles the .py files in the environment to bytecode. Setting the UV_COMPILE_BYTECODE environment variable does the same |
To make it the project's default, put it in pyproject.toml:
[tool.uv]
compile-bytecode = true
Python doesn't run a .py file as it is. The Python glossary explains: "Python source code is compiled into bytecode, the internal representation of a Python program in the CPython interpreter. The bytecode is also cached in .pyc files so that executing the same file is faster the second time". uv's settings reference says when that happens after a uv install: "By default, uv does not compile Python (.py) files to bytecode (__pycache__/*.pyc); instead, compilation is performed lazily the first time a module is imported."
The Example
A uv project, bytecode-demo, on Python 3.15.2, with SymPy, a library for symbolic maths with many modules. A one-line timer measures how long import sympy takes, with and without compiled bytecode.
Prerequisites
- uv. This guide uses uv 0.13.10 with Python 3.15.2.
Step 1: Create the Project and Add SymPy
uv init --python 3.15 bytecode-demo
cd bytecode-demo
uv add sympy
Initialized project `bytecode-demo` at `/Users/you/source/bytecode-demo`
Using CPython 3.15.2 interpreter at: /Library/Frameworks/Python.framework/Versions/3.15/bin/python3.15
Creating virtual environment at: .venv
Resolved 3 packages in 6ms
Built bytecode-demo @ file:///Users/you/source/bytecode-demo
Prepared 1 package in 4ms
Installed 3 packages in 18ms
+ bytecode-demo==0.1.0 (from file:///Users/you/source/bytecode-demo)
+ mpmath==1.3.0
+ sympy==1.14.0

uv installed SymPy 1.14.0, mpmath, which SymPy needs, and the project itself. Nothing in the output mentions bytecode: none was compiled. The commands work unchanged in PowerShell.
Step 2: Time the First Import
uv run python -c "import time; start = time.perf_counter(); import sympy; print(f'import sympy took {time.perf_counter() - start:.2f} s')"
import sympy took 1.28 s

python -c runs the code in quotes, and the semicolons separate its statements. time.perf_counter() reads a clock before and after import sympy, and the difference is printed with two decimal places. The first import took 1.28 seconds. The command works unchanged in PowerShell.
Step 3: Time It Again
Run the same command a second time:
uv run python -c "import time; start = time.perf_counter(); import sympy; print(f'import sympy took {time.perf_counter() - start:.2f} s')"
import sympy took 0.14 s

0.14 seconds, about nine times faster. The first import compiled SymPy's files and saved the bytecode, and this one loaded what was saved. Every new environment pays that first cost again.
Step 4: Compile the Bytecode While Installing
Delete the environment, so the next install starts from nothing, and sync it again with --compile-bytecode:
- macOS / Linux
- Windows (PowerShell)
rm -rf .venv
uv sync --compile-bytecode
Remove-Item -Recurse -Force .venv
uv sync --compile-bytecode
Using CPython 3.15.2 interpreter at: /Library/Frameworks/Python.framework/Versions/3.15/bin/python3.15
Creating virtual environment at: .venv
Resolved 3 packages in 4ms
Installed 3 packages in 15ms
Bytecode compiled 1620 files in 726ms
+ bytecode-demo==0.1.0 (from file:///Users/you/source/bytecode-demo)
+ mpmath==1.3.0
+ sympy==1.14.0

Bytecode compiled 1620 files in 726ms: uv compiled every .py file in the new environment in under a second.
Step 5: Time the First Import Again
uv run python -c "import time; start = time.perf_counter(); import sympy; print(f'import sympy took {time.perf_counter() - start:.2f} s')"
import sympy took 0.15 s

The first import in this new environment took 0.15 seconds instead of 1.28: as fast as a second import, because the bytecode was already there.
Step 6: Make It the Project's Default
Add a [tool.uv] table to the end of pyproject.toml:
[tool.uv]
compile-bytecode = true
The complete file:
[project]
name = "bytecode-demo"
version = "0.1.0"
description = "Add your description here"
readme = "README.md"
authors = [
{ name = "Your Name", email = "you@example.com" }
]
requires-python = ">=3.15"
dependencies = [
"sympy>=1.14.0",
]
[project.scripts]
bytecode-demo = "bytecode_demo:main"
[build-system]
requires = ["uv_build>=0.13.10,<0.14.0"]
build-backend = "uv_build"
[tool.uv]
compile-bytecode = true

Now a plain uv sync compiles too:
- macOS / Linux
- Windows (PowerShell)
rm -rf .venv
uv sync
Remove-Item -Recurse -Force .venv
uv sync
Using CPython 3.15.2 interpreter at: /Library/Frameworks/Python.framework/Versions/3.15/bin/python3.15
Creating virtual environment at: .venv
Resolved 3 packages in 4ms
Built bytecode-demo @ file:///Users/you/source/bytecode-demo
Prepared 1 package in 4ms
Installed 3 packages in 15ms
Bytecode compiled 1620 files in 716ms
+ bytecode-demo==0.1.0 (from file:///Users/you/source/bytecode-demo)
+ mpmath==1.3.0
+ sympy==1.14.0

Bytecode compiled 1620 files again, with no flag on the command.
Step 7: See What It Does on Every Run
With the setting on, the compile line appears on uv run too:
uv run python -c "print('hello')"
Bytecode compiled 1620 files in 101ms
hello

uv run checks the environment before every command, and with the setting on that check covers the bytecode too: 101 milliseconds here, and 112 on the next run. The settings reference explains why it goes through every package: "When enabled, uv will process the entire site-packages directory (including packages that are not being modified by the current operation) for consistency."
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
| Expecting it to fix every slow first import | TensorFlow's first import still took 34 seconds after --compile-bytecode. Its slowest single step, 13.9 seconds, was loading one of its compiled libraries, which bytecode doesn't touch | Time the import with and without it, as in Steps 2 and 5, before you rely on it |
| Timing a second import and calling it the result | The second import is fast without the option too: 0.14 seconds | Delete .venv and time the first import in a new environment |
FAQ
Where does uv put the bytecode?
Where Python itself does: in a __pycache__ folder beside each module's .py file. For SymPy on macOS that's .venv/lib/python3.15/site-packages/sympy/__pycache__/, with files such as __init__.cpython-315.pyc.
Does it make the second import faster too?
No. The second import took 0.14 seconds without it, and the first import took 0.15 with it: once the bytecode exists, it doesn't matter who compiled it.
Conclusion
SymPy's first import took 1.28 seconds in a new environment, and 0.14 after that. uv sync --compile-bytecode compiled 1,620 files in under a second, and the first import dropped to 0.15. compile-bytecode = true under [tool.uv] made it the project's default, at the cost of a short check on every uv run.
Additional Resources
- compile-bytecode — uv's settings reference.
- Compiling bytecode — uv's Docker guide, where start time matters most.
- bytecode — the Python glossary.
