Skip to content

How to Use uvx for Temporary Python Shells and Exploratory Coding

Problem

I want to test how a Flask API behaves. I could create a new virtual environment, install Flask, test my hypothesis, then figure out what to do with that environment. Or I could run it in a disposable shell that vanishes when I’m done.

Here’s the second approach - it’s faster, cleaner, and exactly what uvx was designed for.

The Short Answer

Use uvx --from python for throwaway shells, and uvx --with <packages> ipython for interactive sessions with dependencies.

Terminal window
# Run a throwaway Python shell with Flask
uvx --from flask python
# Run IPython with additional packages
uvx --with pandas --with requests ipython
# Run a Flask app interactively
uvx --from flask python -c "from flask import Flask; app = Flask(__name__)"

What is uvx for Shells?

uvx (alias for uv tool run) can execute Python with any package as the main entry point. It creates a temporary, isolated environment that cleans up after itself.

The process works like this:

  1. Resolves the specified package
  2. Creates an ephemeral environment
  3. Runs the Python interpreter or tool
  4. Cleans up when done - no persistent installation

This is useful for:

  • Quick API testing
  • Framework exploration
  • One-off script execution with dependencies
  • Interactive debugging sessions

uvx —from for Package-as-Main

The --from flag lets you run a package’s default executable. This is powerful for shells:

Terminal window
# Run Flask's Python interpreter
uvx --from flask python
# Now you're in a shell with Flask available
>>> from flask import Flask, request
>>> app = Flask(__name__)
>>> @app.route('/test')
... def test(): return 'OK'
...

Armin Ronacher (mitsuhiko), the creator of Flask, shared this workflow on Reddit:

“One thing I sometimes do is to just run a python shell. Eg: if I want to figure out how an API behaves I can just run it in a throwaway virtualenv: $ uvx —from flask python”

This is the quickest way to explore any Python package interactively.

Version Pinning with —from

You can pin specific versions:

Terminal window
# Run specific Flask version
uvx --from 'flask<3.0' python
# Run with version constraints
uvx --from 'flask>2.0,<3.0' python

uvx —with for Adding Dependencies

The --with flag adds additional packages to the ephemeral environment:

Terminal window
# IPython with data science packages
uvx --with pandas --with requests ipython
# Multiple packages at once
uvx --with pandas --with requests --with numpy ipython
# With version constraints
uvx --with 'requests>2.28,<3.0' ipython

From a Reddit discussion:

“uvx —with pandas —with requests ipython” for ad-hoc iPython sessions

Another developer added:

“Adhoc iPython sessions with some libs is something I tend to do A LOT”

Combining —from and —with

You can combine both flags for the best of both worlds:

Terminal window
# Flask as main, plus additional packages
uvx --from flask --with sqlalchemy --with pydantic python
# Great for testing ORM models with Flask routes
uvx --from flask --with 'sqlalchemy>2.0' python

Practical Use Cases

Use Case 1: API Exploration

Terminal window
# Quick HTTP testing
uvx --from flask python
>>> import requests
>>> response = requests.get('https://api.example.com/data')
>>> print(response.json())

Use Case 2: Framework Testing

Terminal window
# Test Django setup
uvx --from django python -c "import django; print(django.VERSION)"
# Test FastAPI
uvx --from fastapi python
>>> from fastapi import FastAPI
>>> app = FastAPI()

Use Case 3: Package Exploration

Terminal window
# Explore any package without installing
uvx --from pandas python
>>> import pandas as pd
>>> df = pd.DataFrame({'a': [1,2,3]})
>>> df.describe()

Use Case 4: Data Science Scratchpad

Terminal window
# Ad-hoc analysis environment
uvx --with pandas --with numpy --with matplotlib ipython

uvx vs Traditional Approaches

ApproachSetup TimeCleanupBest For
uvxSecondsAutomaticOne-off tasks
pipxSecondsManualPersistent tools
venv~30 secondsManualLong-lived projects
uv venv~5 secondsManualProject work

The key advantage of uvx: zero commitment. Create, use, forget. No cleanup needed.

Advanced Patterns

Running Scripts with Dependencies

Terminal window
# Run a script with temporary dependencies
uvx --with rich --with httpx python script.py
# Equivalent to creating a temp venv with those packages

Using uv run Instead

For project-based work:

Terminal window
# For project-based work
uv run --with pandas --with requests script.py
# --no-project ignores project dependencies
uv run --no-project --with pandas script.py

Specific Python Version

Terminal window
# Run with specific Python version
uvx --python 3.11 --from flask python
# PyPy for specific needs
uvx --python pypy3.9 --from numpy python

Why This Works

The old workflow was: create venv, install stuff, do work, delete venv. That’s four steps with manual cleanup.

uvx handles all of this automatically:

  1. --from specifies the “main” package - the one that provides the context
  2. --with adds supporting packages without making them the focus
  3. The environment is truly temporary - it vanishes when the session ends
  4. No accumulated junk in your system

This is why the Flask creator uses it for API exploration. It’s not about laziness - it’s about removing friction from the thought-to-execution pipeline.

Summary

In this post, I showed how to use uvx for temporary Python shells and exploratory coding. The key commands are:

  • uvx --from <package> python - Run Python with a package available
  • uvx --with <pkg> ipython - Interactive session with dependencies
  • uvx --from <pkg1> --with <pkg2> python - Combine approaches

The key point is: no more “let me create a quick venv” - just run and go. When the creator of Flask uses this workflow for API testing, you know it’s production-grade thinking.

Final Words + More Resources

My intention with this article was to help others share my knowledge and experience. If you want to contact me, you can contact by email: Email me

Here are also the most important links from this article along with some further resources that will help you in this scope:

Oh, and if you found these resources useful, don’t forget to support me by starring the repo on GitHub!

Comments