1.

What are some different command line options available for using Python interpreter?

Answer»

Python interpreter is invoked from command terminal of operating system (Windows or Linux). Two most common WAYS are starting interactive console and running script

For interactive console $ python Python 3.6.6 |Anaconda custom (64-bit)| (DEFAULT, Oct  9 2018, 12:34:16)  [GCC 7.3.0] on linux Type "help", "copyright", "credits" or "license" for more information. >>>

For scripting mode

$ python hello.py

In addition to these common ways, Python command line can have different options. They are listed below:

-c cmd : program passed in as string. Interpreter executes the Python code in string which can be one or more statements separated by newlines.

$ python -c "print ('hello')" Hello

-m mod : run library module as a script. the named module and execute its contents as the __main__ module. The following command creates a new virtual environment

$ python -m venv myenv

-i: inspect interactively after running script or code with -c option. The interactive prompt APPEARS after the output. This can be useful to inspect global variables or a stack trace when a script raises an exception.

$ python -i -c "print ('hello')" hello $ python -i temp.py Hello world >>>

Other options are:

  • B: don't write .pyc files on import; also PYTHONDONTWRITEBYTECODE=x
  • d: debug output from PARSER; also PYTHONDEBUG=x
  • E: ignore PYTHON* environment variables (such as PYTHONPATH)
  • h: print this help message and exit (also --help)
  • q: don't print version and copyright messages on interactive startup
  • v: verbose (trace import statements) can be supplied multiple times to increase verbosity
  • V: print the Python version number and exit (also --version)
  • x: skip first line of source, allowing use of non-Unix forms of #!cmd shebang
  • file: program read from script file


Discussion

No Comment Found