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.1.tar.gz (15.1 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.1-py3-none-any.whl (5.4 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: pybites_pysource-1.3.1.tar.gz
  • Upload date:
  • Size: 15.1 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.1.tar.gz
Algorithm Hash digest
SHA256 8e9129dbe16041e4ec7315361c8c4abce0e22c2535d480d701b2a8f2a297c20b
MD5 c12fd36a1d7bafb41aa6aeecdfe239f9
BLAKE2b-256 910f1cc48d79d34308b2a02b8a7800fc443aeb8a20507847e9ac9717634721e5

See more details on using hashes here.

File details

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

File metadata

  • Download URL: pybites_pysource-1.3.1-py3-none-any.whl
  • Upload date:
  • Size: 5.4 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.1-py3-none-any.whl
Algorithm Hash digest
SHA256 ed03c297364c35bf8e0fb51ab165ece351733816a5173463fb72817039b2bef6
MD5 e9b67359c43c2bb6f5bcccf0d666ae03
BLAKE2b-256 1a592e7c6670212686085629745c16922fd0aa07839937d695716c818e320fb3

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