272 words
1 minute
Trying out Python packages with ipython and uvx
Anubhav Gain
2024-04-09

Trying out Python packages with ipython and uvx#

I figured out a really simple pattern for experimenting with new Python packages today:

Terminal window
uvx --with llm --with sqlite-utils ipython

This command will work if you have uv installed and nothing else. It uses uvx to install and run the excellent ipython Python REPL in a new, dedicated virtual environment with two additional Python libraries - llm and sqlite-utils. You don’t need to install ANY of those packages first - uvx will fetch and cache them the first time you run this, and reuse the cached versions on future invocations.

Screenshot of the command running - I can then import llm and sqlite_utils and start using them.

You can also set which Python version will be used with the --python 3.13 option:

Terminal window
uvx --python 3.13 --with llm --with sqlite-utils ipython

I turned this into a shell script (with help from Claude):

#!/bin/sh
# itry - A portable script for launching ipython with uvx packages
# Show help if requested
[ "$1" = "--help" ] && {
echo "Usage: itry [packages...]"
echo "Example: itry llm sqlite-utils datasette"
exit 0
}
# Initialize empty string for packages
PACKAGES=""
# Process all arguments, adding --with before each
for arg in "$@"; do
PACKAGES="$PACKAGES --with $arg"
done
# Remove leading space if present
PACKAGES="${PACKAGES# }"
# Execute uvx command with Python 3.13
exec uvx $PACKAGES --python 3.13 ipython

This is saved as ~/.local/bin/itry - then chmod 755 ~/.local/bin/itry - and now I can jump straight into an interactive ipython REPL with any Python packages I like using this command:

Terminal window
itry cowsay

Then:

import cowsay
cowsay.cow("hello")
_____
| hello |
=====
\
\
^__^
(oo)\_______
(__)\ )\/\
||----w |
|| ||
Trying out Python packages with ipython and uvx
https://mranv.pages.dev/posts/trying-out-python-packages-with-ipython-and-uvx/
Author
Anubhav Gain
Published at
2024-04-09
License
CC BY-NC-SA 4.0