Skip to main content

Read Python source code from the command line

Project description

PyBites Pysource

A command line tool to read Python source code.

Update 5th of Nov 2021: we did not know it at the time, but this actually can be accomplished by Python's inspect module! So you might want to use that over this tool, for more info see this Twitter thread.

Installation

Run it without installing anything using uv:

uvx --from pybites-pysource pysource -m datetime.date

To make that an alias, add to your shell config:

alias pysource='uvx --from pybites-pysource pysource'

Or install it from PyPI:

uv tool install pybites-pysource

This tool requires Python 3.12+.

Usage

Pass any importable object as module(.submodule).name with -m.

A function:

$ pysource -m re.match
def match(pattern, string, flags=0):
    """Try to apply the pattern at the start of the string, returning
    a Match object, or None if no match was found."""
    return _compile(pattern, flags).match(string)

A class:

$ pysource -m argparse.ArgumentParser
class ArgumentParser(_AttributeHolder, _ActionsContainer):
    """Object for parsing command line strings into Python objects.
    ...

A whole module (omit the trailing name):

$ pysource -m string
"""A collection of string constants.
...

A C-implemented stdlib objectpysource falls back to the pure-Python source (datetime, decimal, io):

$ pysource -m decimal.Decimal
class Decimal(object):
    """Floating-point class for decimal arithmetic."""
    ...

No source available — when an object is implemented in C with no Python equivalent, you get a clear message (and a non-zero exit code) instead of a traceback:

$ pysource -m builtins.len
no Python source available for 'len' (likely implemented in C / a builtin)

Paging — add -p (or --pager) to page long output, e.g. pysource -m argparse.ArgumentParser -p.

Check out our blog post for a demo.

Pipe into your editor

Since pysource writes to stdout, you can pipe it straight into Vim (the - reads from stdin):

$ pysource -m re.match | vim -

Add -c 'set ft=python' for syntax highlighting: pysource -m re.match | vim - -c 'set ft=python'.

Vim integration

If you use jedi-vim, you already have source navigation built in — just hit ,s on an object to jump to its definition, no extra mapping needed.

Development

This project uses uv. To set up and run the tests:

$ uv sync
$ uv run pytest

Linting, formatting, and type checking run via prek:

$ uv run prek run --all-files

Enjoy!

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pybites_pysource-1.3.0.tar.gz (14.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pybites_pysource-1.3.0-py3-none-any.whl (5.3 kB view details)

Uploaded Python 3

File details

Details for the file pybites_pysource-1.3.0.tar.gz.

File metadata

  • Download URL: pybites_pysource-1.3.0.tar.gz
  • Upload date:
  • Size: 14.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.19 {"installer":{"name":"uv","version":"0.11.19","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for pybites_pysource-1.3.0.tar.gz
Algorithm Hash digest
SHA256 838d56e97a92c9a39e20394afbb548d539780a142cef93791a2f3e234c546e8d
MD5 8c6d7caca9e0efbc6bd84934fb01fd58
BLAKE2b-256 7908f1c1ab97af417f90df81be6760ea60151f9a6c91e87d2ac318c22221b4e3

See more details on using hashes here.

File details

Details for the file pybites_pysource-1.3.0-py3-none-any.whl.

File metadata

  • Download URL: pybites_pysource-1.3.0-py3-none-any.whl
  • Upload date:
  • Size: 5.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.19 {"installer":{"name":"uv","version":"0.11.19","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for pybites_pysource-1.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 ad8bc6940c8844bf506c7b36dd7ac5a8b150c0ddeb6f8fe2d4eec953ec1c79c1
MD5 4a4e78daa796d189168ec24efafd25ae
BLAKE2b-256 ea391b95268a63cf28904ec34126304d14ae0d37a90dd14c2f778d9ca83f8261

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page