Run script in python idle

Getting Started with Python IDLE

Getting Started with Python IDLE

Python is a popular programming and scripting language used by a lot of people. There are a lot of tools and software in order to develop python scripts and applications.

What Is Python IDLE?

IDLE is the short form of the Integrated Development and Learning Environment . IDLE is a simple IDE (Integrated Development Environment) which do not provides full features of IDE. But IDLE provides basic features in order to start learning and developing the Python programming language. Python IDLE is created and developed by the Python Programming Language community. So Python IDLE has been first provided with Python version 1.5 which is a long time ago. As a not core tool it is provided optionally which is not installed default with Python but can be installed easily simple steps.

As a simple and basic IDE Python IDLE provides less features than other Python IDE’s. Following features are provided by Python IDLE to make the Python development and test easier.

  • Cross-platform where operating systems like Windows, Linux, Ubuntu, MacOS supported
  • Created and coded with Python where GUI uses the tkinter library.
  • Provides interactive Python shell same as Python command line shell.
  • Code is colorized according to the Python syntax
  • Multiple windows can be opened and used at the same time
  • Smart indent provided to automatically indent Python code blocks
  • Provides auto completion for keywords, variables, modules etc.
  • Useful search and replace features in a single or multiple windows and files
  • Debugging the script to detect errors, exceptions easily by using breakpoints, stepping
Читайте также:  Чтение файла до конца php

Install Python IDLE For Ubuntu, Debian, Mint

IDLE is provided as a package in deb based distributions like Ubuntu, Debian, Mint etc. In order to install and run IDLE the Python should be installed but during the installation of the IDLE if the Python is not installed it will be installed automatically. Python has two major version called as Python2 and Python3. Two IDLE installation package is provides for these version.

IDLE for Python3 can be installed with the package named idle-python3.8 like below. But keep in mind that if the Python version changes to the 3.9 the package name may be changing too.

$ sudo apt install idle-python3.8

IDLE for Python2 can be installed with the package named idle-python2.7 like below.

$ sudo apt install idle-python2.7

Python IDLE can be started from the application menu or command line. Application menu can provide different categories but for XFCE desktop the Development Tools contains the IDLE.

We can also start IDLE from the Linux bash or command line by using the idle-python3.8 command below. But this will start Python 3 IDLE.

If you have installed the Python2 IDLE you should use the idle-python2.7 command from the Linux bash or command line like below.

You can see that then we start IDLE a Python shell is provided on the main screen. Also, some information like Python Interpreter, Build Date, Build Tool, and Help information about the IDLE is provided automatically by default.

Install Python IDLE For Windows

IDLE for windows is provided with the Python installation executable. In order to install Python IDLE on windows during the installation of Python the tcl/tk and IDLE feature should be selected like below. Other features are optional for the installation of the Python IDLE.

You can access the IDLE from the start menu or program files by typing its name like below. Just typing “idle” lists the Python IDLE like below.

Install Python IDLE For MacOS

Python IDLE can be installed for MacOS similar to the Windows operating systems. Python installation will provide the IDLE as features that can be installed easily. The following link provides Python3.8 MacOS 64-bit installer in pkg format. These setup files work with OS X 10.9 and later versions. If you need to install older versions take look at older Python versions like Python3.7, Python3.6, etc.

In order to print the IDLE version and related information the Help-> About IDLE menu can be used. About IDLE will provide information for the IDLE installation, Python interpreter version, email for support, Tk version, copyright information tec.

Run Python Script with IDLE Interactive Shell

The IDLE main screen provides the python shell which is like an MS-DOS or Linux Bash shell. We can run simple or complex Python scripts from this shell but if you want to develop applications prefer the Python files which will be described below. We will run the following Python print() function in the IDLE interactive shell.

We can see that the script will output to the current interactive shell. This is very useful to test simple scripts or code.

Create a Python Script File

We can create a new script file from the File->New File menu from the menu bar. Alternatively we can use the CTRL+N keyboard shortcut to create a new script file directly.

We will see the following screen which is like a simple text editor with more menu items. We can put the script or code we want to create into the file like below.

In order to create a new file, we will save from the File->Save menu or use the CTRL+S keyboard shortcut like below. We will be asked the filename we want to save where the file extension will be *.py by default.

Run Python Script File

We can run a Python file by using the Run->Run Module steps from the IDLE menu. Alternatively, you can use the F5 keyboard shortcut. Running a python script file will create a new interactive shell and execute the Python script file content.

When the script is executed you will see a screen like this where the executed script full path and name printed to the interactive shell and the script outputs will be printed to the screen.

Open Python Script File

You can open a Python Script file with IDLE by using the File->Open steps from the menu. Alternatively, you can use the CTRL+O keyboard shortcut to open the file menu.

We can navigate to the Python file we want to open in the IDLE and selecting the file. This open file menu will only show the Python files which are configured right bottom corner. Python files may have an extension like .py .py2 or .py3 .

After clicking to the Open the selected python file will be opened in the current editor.

Источник

Writing, Saving and Running Python Programs with IDLE

Entering commands at the prompt is just the beginning. Let’s use IDLE to save and run files. With this skill you’ll be able to write and build complex and powerful Python programs. IDLE has two modes: interactive and script. We wrote our first program, “Hello, World!” in interactive mode. Interactive mode immediately returns the results of commands you enter into the shell. In script mode, you will write a script and then run it.

Let’s create a module. We’ll cover modules in depth later. For now, all you need to know is that a module is a file containing Python code ending in the suffix:

If it’s not already running, start IDLE. Using IDLE is very similar to using a text editor. Under File, select New Window or press Ctrl + N. That will bring up a new window titled, “Untitled”.

IDLE

Enter this command in the Untitled window:

print(“What's brown and sounds like a bell?”)

We need to save our file before we can run it. So under File, select Save. At the prompt, name it something clever like:

And save it somewhere you can find it later.

Now the fun part. Under Run, select Run Module.

Running a Python program in IDLE

Now go back to the Shell. What’s this RESTART business? The Shell is restarted every time you run a module in IDLE. That’s to clear out any old settings that might interfere with your program. Below the RESTART line you should see:

Running a Python program in IDLE

It’s not much of a joke without a punchline. Let’s modify our module. Change the following:

input(“What's brown and sound like a bell?”) print(“Dung!”)

Let’s run that again, this time with the keyboard shortcut: F5. Try that.

In the Shell you’ll see our joke printed, but where’s the punchline? Notice that there isn’t a prompt? That’s because the Shell is waiting for your input. Press any key.

Running a Python program in IDLE

I love that joke. It’s so good and so bad. Know what I mean? Know what I mean? Nudge nudge, say no more.

Источник

Оцените статью