Python interactive shell and interpreting of .py files
In this tutorial we will learn how to work with Python interactive shell in Windows and Linux and how to run files with Python source code.
Interactive shell allows to run Python code in dialogue mode, i.e. you input the command and immediately see the result.
Interactive mode is started by python.exe file in Windows or by python (python3) command in Linux. It will start interpreter from Python directory. You need just input python in command line.
My path to interpreter - C:\python34\python.exe, you will have another. At start of interactive shell you can see Python version installed on your computer. On screenshot you can see v3.4.2, I'm currently using v3.5.2 and the latest Python release - 5.6.0 (january 2017).
Starting Python interpreter with graphical user interface (GUI) - IDLE
IDLE is very simple IDE for Python. On one hand in the IDLE word there are three letters coincide with IDE, on the other hand IDLE is named after Eric Idle - member of Monty Python (it's for them Python is named).
- On Linux you'll need to install IDLE manually - it's not the part of standard package. For Windows users: you can run IDLE differently
- Run it from the Start menu. IDLE will be listed in the file list for Python.
- Go to the directory where Python is installed. Then, go to lib\idlelib. Here you can run idle.bat, idle.py or idle.pyw - they all work.
- Through command line. Go to the directory where Python is installed, then run one of next commands (exe extension is not necessary):
python.exe lib\idlelib\idle.py\npython.exe lib\idlelib\idle.pyw
IDLE has py or pyw extensions. These are standard extensions (pyw - for windows, but you can use py on windows too). You can open idle.py or idle.pyw in any text editor - there you will see python source code.
On the next screenshot there is running Python interpreter in Idle:
There are many more powerful IDE's for Python. As for me, I'm using Sublime text editor most of the time.
Python interpreter commands
Let's try to write something in Python. Start interactive shell and input next commands:
When you input command and press Enter, interactive shell executes the command and outputs the result.
To exit from interpreter you can input quit() or Ctrl+Z:
Running .py files
As I mentioned earlier, files with Python source code usually have .py extension. You can run them with the command python (or python.exe) with argument - name of the file, which you want to run.
Let's create file hello.py in any directory. Open it in text editor and paste next string:
Now in command line you need to go to the directory there you've created hello.py file. And here you need to input:
Your code will be executed and in console you will see greetings.
On this screenshot you see the word "Привет" (prievet), it's "hello" in Russian.
We've learned how to run Python interpreter and files with Python source code. You can work with Python interpreter in any operating system (Windows, Linux, XOS). All you need - just install Python interpreter and preferable text editor or IDE. In next tutorial we will learn Python basics.