Skip to main content

Command line interface

gopy [options] [-c cmd | -m mod | file | -] [arg ...]

The argument parser lives in getopt/getopt.go and follows CPython's Modules/main.c and Modules/getpath.c. gopy parses the full CPython short-option set; not all of them have effect yet. This page is honest about which is which.

Wired flags

These flags work today.

OptionEffect
-c cmdRun cmd as a single statement; remaining args become sys.argv.
fileExecute file as __main__. sys.argv[0] is the filename.
-Read the program from stdin (also the default when stdin is not a TTY).
-V, --versionPrint version and exit.
-h, --help, -?Print usage and exit.
(no args)Enter the interactive REPL.

Parsed but not yet wired

These flags are accepted (you will not get a parse error) but have no effect yet. They are listed here so you can recognise gaps when you compare against CPython.

OptionWhat it should do (per CPython)
-m modImport mod and run it as __main__.
-iDrop into the REPL after the script.
-qSuppress the REPL banner.
-uForce unbuffered stdout/stderr.
-vVerbose import tracing.
-O, -OOOptimise; strip assertions / docstrings.
-SSkip implicit import site.
-EIgnore PYTHON* environment variables.
-IIsolated mode (-E -s -P combined).
-sDon't add the user site directory to sys.path.
-PDon't prepend script directory to sys.path.
-b, -bbWarn / error on bytes/str mixing.
-BDon't write .pyc files.
-dDebug mode (CPython prints parser debug; gopy treats this as a no-op).
-RAlready-default hash randomisation (kept for compatibility).
-tRemoved in CPython 3; accepted for back-compat.
-W argAdd a warning filter.
-X optSet an implementation option in sys._xoptions.
-xSkip the first source line (shebang in scripts started by other interpreters).

The wiring lands one section at a time as the underlying subsystems mature. See Status for the current state.

Environment variables

gopy reads two environment variables at startup today:

VariableEffect
PYTHONPATHPrepended to sys.path. Colon-separated on POSIX, semicolon on Windows.
GOPY_STDLIBOverride the directory gopy walks for bundled .py resources.

The full CPython environment surface (PYTHONHOME, PYTHONSTARTUP, PYTHONHASHSEED, PYTHONOPTIMIZE, PYTHONUNBUFFERED, PYTHONUTF8, ...) is not yet honoured; the plumbing arrives with initconfig.c port.

Exit codes

CodeMeaning
0The interpreter terminated cleanly.
1An unhandled exception escaped the top frame.
2A bad command-line argument or a missing file.
nsys.exit(n) exited with code n.
130The interpreter was interrupted by SIGINT.

These follow Modules/main.c. Implementation is in cmd/gopy/main.go.

Examples

Run a script with arguments:

gopy script.py --flag value
# inside the script: sys.argv == ["script.py", "--flag", "value"]

Evaluate an expression:

gopy -c 'import sys; print(sys.version_info)'

Pipe a script:

cat <<'PY' | gopy
import json
print(json.dumps({"hello": "gopy"}, indent=2))
PY

Run a one-liner under timing:

time gopy -c 'sum(i*i for i in range(1_000_000))'

Compatibility checklist

When porting a CPython invocation, the failure modes you may hit today are:

  • -m some.module is silently ignored; pass a path instead.
  • -O does not strip asserts. Don't rely on __debug__ == False.
  • PYTHONPATH works; PYTHONHASHSEED and the rest do not.
  • python -X dev does nothing (no sys._xoptions plumbing yet).

For anything that does not fit, file an issue on the tracker with the invocation and the expected behaviour.

See also

  • REPL for the interactive prompt.
  • Embedding when you want to skip the CLI.
  • Debugging for the introspection tools gopy ships.