Python Development Tools

PythonPythonBeginner
Practice Now

Introduction

In this lab, you will explore various tools for Python development. You will begin by using the standard Python interactive mode to execute code line by line and experiment with basic operations.

Next, you will enhance your interactive experience by utilizing IPython, which provides advanced features like tab completion and syntax highlighting. Finally, you will learn how to write and run Python scripts using the Vim text editor and the IDLE integrated development environment, gaining practical experience with different development workflows.

This is a Guided Lab, which provides step-by-step instructions to help you learn and practice. Follow the instructions carefully to complete each step and gain hands-on experience. Historical data shows that this is a intermediate level lab with a 74% completion rate. It has received a 97% positive review rate from learners.

Use Python Interactive Mode

In this step, we will explore the interactive mode of Python, which allows you to execute Python code line by line and see the results immediately. This is particularly useful for testing small snippets of code or experimenting with Python features.

First, open the terminal in the WebIDE. The WebIDE loads with a terminal open by default. If you don't see the terminal, you can open it by clicking on the "Terminal" menu at the top and selecting "New Terminal".

WebIDE terminal

The WebIDE is a VS Code-like integrated development environment that provides a comprehensive coding experience with features like syntax highlighting, code completion, and integrated terminal. The red box in the image highlights the terminal area where you'll execute commands.

For more detailed information about the WebIDE interface, please refer to the LabEx VM WebIDE documentation.

Once the terminal is open, you will be in the default directory /home/labex/project. To enter the Python interactive mode, type the command python and press Enter.

python

You should see output similar to this, indicating that you are now in the Python interactive shell. The >>> is the Python prompt.

Python 3.10.12 (main, Nov 20 2023, 15:14:05) [GCC 11.4.0]
Type "help", "copyright", "credits" or "license" for more information.
>>>

Now, let's try some simple calculations in the interactive mode. Type the following expressions one by one and press Enter after each line:

1 + 1

You will see the result 2 printed immediately.

5 ** 2

This calculates 5 raised to the power of 2 (5 squared). The output will be 25.

5 ** 100

This calculates 5 raised to the power of 100. Python can handle very large numbers, and you will see the full result printed.

>>> 1 + 1
2
>>> 5 ** 2
25
>>> 5 ** 100
788860905221011805411728563735037463272540590005
>>>

As you can see, the interactive mode acts like a powerful calculator.

Python interactive shell output

To exit the Python interactive mode, you can type exit() or quit() and press Enter, or press Ctrl + D.

exit()

You will return to the regular terminal prompt.

Enhance Interaction with IPython

In this step, we will explore IPython, an enhanced interactive Python shell that offers features like tab completion, syntax highlighting, and magic commands, making interactive development more efficient.

Tip: Before starting with IPython, you can clear the terminal screen to start fresh by typing clear and pressing Enter, or open a new terminal if you prefer a clean environment.

IPython is already installed in the LabEx environment. To start an IPython session, open the terminal and type ipython, then press Enter.

ipython

You will see the IPython prompt, which looks similar to the standard Python prompt but with some additional information.

Python 3.10.12 (main, Nov 20 2023, 15:14:05) [GCC 11.4.0]
Type 'copyright', 'credits' or 'license' for more information
IPython 8.12.2 -- An enhanced Interactive Python. Type '?' for help.

In [1]:

One of the key features of IPython is tab completion. Let's try it. Type the first few letters of a Python keyword or function, like pri, and then press the Tab key. IPython will suggest completions.

pri<Tab>

You should see print as a suggestion. Press Tab again to cycle through other possible completions if any exist.

Now, let's try getting help on a function. In standard Python, you would use help(print). In IPython, you can use the ? or ?? magic commands. Type print? and press Enter.

print?

This will display the help documentation for the print function directly in the terminal.

In [2]: print?
Signature: print(*args, sep=' ', end='\n', file=None, flush=False)
Docstring:
Prints the values to a stream, or to sys.stdout by default.
...

Press q to exit the help viewer.

Now try print??. In some cases, ?? provides more detailed information, including the source code of the object if available.

print??

For built-in functions like print, ? and ?? might show similar information. However, for user-defined functions or modules, ?? can be very helpful for understanding their implementation.

IPython help output for print function

To exit IPython, type exit() or quit() and press Enter, or press Ctrl + D.

exit()

Write and Run Python Scripts with Vim

In this step, we will learn how to write and run Python code as a script file using the Vim text editor in the terminal. This is a common way to work with Python for larger programs or tasks that need to be run repeatedly.

First, make sure you are in the ~/project directory in your terminal. You can confirm this by typing pwd and pressing Enter.

pwd

The output should be /home/labex/project.

Now, we will create a new Python file named hello.py using the Vim editor. Type the following command and press Enter:

vim hello.py

Since the file hello.py does not exist, Vim will create it and open it for editing. You will see a blank screen with information about Vim at the bottom.

To start typing code, you need to enter Vim's insert mode. Press the i key. You will see -- INSERT -- at the bottom of the screen, indicating that you are now in insert mode.

Now, type the following Python code into the editor:

print("Hello World")
print("This is my first Python script.")

After typing the code, you need to exit insert mode to save the file. Press the Esc key. The -- INSERT -- indicator at the bottom will disappear.

To save the file and exit Vim, type :wq and press Enter. The colon : enters command mode, w writes the file, and q quits Vim.

:wq

You will return to the terminal prompt. To verify that the file was created, you can list the files in the current directory using the ls command:

ls

You should see hello.py listed in the output.

hello.py

Now, let's run the Python script we just created. Use the python command followed by the script's filename:

python hello.py
Python script output in terminal

The Python interpreter will execute the code in hello.py, and you will see the output in the terminal:

Hello World
This is my first Python script.

You have successfully written and run your first Python script using Vim!

Develop and Run Python Code with IDLE

In this step, we will use IDLE, Python's Integrated Development and Learning Environment. IDLE provides a more feature-rich environment for writing, running, and debugging Python code compared to the basic interactive shell or a simple text editor.

Note: Since IDLE is a GUI application, you need to switch to the Desktop interface. Click on the "Desktop" tab in the top-left corner of the WebIDE to access the graphical desktop environment.

Desktop interface

For more detailed information about the Desktop interface, please refer to the LabEx VM Desktop documentation.

Now, open a terminal in the Desktop environment. You can do this by clicking on the terminal icon from the left panel (Xfce Terminal) to open a new terminal window.

Open terminal in Desktop

First, install IDLE in the terminal:

sudo apt-get update
sudo apt-get install idle3 -y

Support: How to Copy and Paste in Terminal

To open IDLE, you can type idle in the terminal and press Enter.

idle

This will open the IDLE Shell window. This window is similar to the interactive mode we used in the first step, allowing you to execute Python commands line by line. You will see the >>> prompt.

Python 3.10.12 (main, Nov 20 2023, 15:14:05) [GCC 11.4.0]
Type "help", "copyright", "credits" or "license" for more information.
>>>

You can try some simple commands here, just like in the regular Python interactive mode:

print("Hello from IDLE Shell")
IDLE shell window example

Now, let's use IDLE to write and run a Python script. To create a new script file, go to the menu bar in the IDLE Shell window, click on File, and then select New File.

A new, empty editor window will open. This is where you will write your Python code. Type the following code into this new window:

print("I love Python")
print("IDLE is useful")
print(123 * 456)

To run this script, you first need to save it. Go to the menu bar in the editor window, click on Run, and then select Run Module.

IDLE will prompt you to save the file before running. Click OK.

A "Save As" dialog box will appear. Navigate to the /home/labex/project directory. You can do this by double-clicking on folders in the file browser. Once you are in /home/labex/project, enter idle_script.py in the "File name:" field and click the Save button.

IDLE save as dialog box

After saving, IDLE will automatically run the script, and the output will appear in the IDLE Shell window.

>>> = RESTART: /home/labex/project/idle_script.py
I love Python
IDLE is useful
56088
>>>

You have successfully written and run a Python script using IDLE. The IDLE editor provides features like syntax highlighting and automatic indentation, which can be very helpful when writing code.

Edit and Run Python Scripts with WebIDE

In this step, we will explore using the WebIDE to edit and run Python scripts. The WebIDE, similar to VS Code, provides a modern, integrated development environment with a powerful editor and an integrated terminal. This is the most common and recommended method for working on projects in LabEx.

Subsequent labs will primarily use this method for teaching. However, it is still valuable to understand other methods. For instance, when you only have access to a terminal, using an editor like Vim is very efficient. For running short code snippets, the Python shell or IPython are excellent choices.

First, let's create a new file in the WebIDE. In the file explorer on the left side of the screen, right-click on an empty area and select "New File".

Script output in WebIDE terminal

Name the new file hello_webide.py and press Enter. The file will be created and opened in the editor.

Now, type the following Python code into the hello_webide.py editor window:

print("Hello from WebIDE")
print("This is a script run from the WebIDE.")

The WebIDE provides syntax highlighting, which makes the code easier to read.

To run the script, use the integrated terminal at the bottom of the WebIDE. If it's not open, you can open it from the "Terminal" menu. Type the following command and press Enter:

python hello_webide.py

You will see the output of your script printed directly in the terminal.

Hello from WebIDE
This is a script run from the WebIDE.

You have successfully created, edited, and run a Python script using the WebIDE. This workflow, combining a powerful editor with an integrated terminal, is highly efficient for development and will be used throughout the upcoming labs.

Summary

In this lab, you explored several tools for Python development. You started with the basic Python interactive mode for running simple code snippets. Then you moved on to IPython for an enhanced interactive experience with features like tab completion. You learned how to write and run scripts from the terminal using the Vim text editor, and how to use a graphical IDE with IDLE.

Finally, you learned how to use the WebIDE, which combines a powerful code editor with an integrated terminal. This is the recommended approach and will be used in subsequent labs. Understanding all these tools provides you with a versatile skill set for Python development in different environments. For terminal-only situations, Vim is a great choice, while the Python shell or IPython are perfect for quick tests and exploration.

Morty Proxy This is a proxified and sanitized view of the page, visit original site.