How to Launch an interactive Python shell

Total Views : 192
Zoom In Zoom Out Read Later Print

Various websites provide online access to Python shells. Let's work with the default one with python included.

By executing (running) the python command in your terminal, you are presented with an interactive Python shell.

This is also known as the Python Interpreter or a REPL (for 'Read Evaluate Print Loop').

$ python
Python 2.7.12 (default, Jun 28 2016, 08:46:01)
[GCC 6.1.1 20160602] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> print 'Hello, World'
Hello, World
>>>

If you want to run Python 3 from your terminal, execute the command python3.

$ python3
Python 3.6.0 (default, Jan 13 2017, 00:00:00)
[GCC 6.1.1 20160602] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> print('Hello, World')
Hello, World
>>>

Alternatively, start the interactive prompt and load the file with python -i <file.py>. In the command line, run:

$ python -i hello.py
"Hello World"
>>>

There are multiple ways to close the Python shell:

>>> exit()

or

>>> quit()

Alternatively, CTRL + D will close the shell and put you back on your terminal's command line.

If you want to cancel a command you're in the middle of typing and get back to a clean command prompt, while

staying inside the Interpreter shell, use CTRL + C.

See More

Latest Photos