Development environments for Python scripting
In this section, we will review PyCharm and Python IDLE as development environments for Python scripting.
Setting up a development environment
In order to rapidly develop and debug Python applications, it is absolutely necessary to use an Integrated Development Environment (IDE). If you want to try different options, we recommend you check out the list that is on the official site of Python, where you can see the tools according to your operating systems and needs:
https://wiki.python.org/moin/IntegratedDevelopmentEnvironments
Between all the environments, the following two are what we will look at:
- PyCharm: http://www.jetbrains.com/pycharm
- Python IDLE: https://docs.python.org/3/library/idle.html
PyCharm
PyCharm is an IDE developed by Jetbrains, based on the company’s IntelliJ IDEA, the same company’s IDE, but focused on Java, and is the Android Studio base.
PyCharm is multi-platform and we can find binaries for operating systems running Windows, Linux, and macOS X. There are two versions of PyCharm – community and technical, with variations in functionality relating to web framework integration and support for databases. In the following URL, we can see a comparison between both editions:
http://www.jetbrains.com/pycharm
The main advantages of this development environment are as follows:
- Autocomplete, syntax highlighter, analysis tool, and refactoring
- Integration with web frameworks such as Django and Flask
- An advanced debugger
- Connection with version-control systems, such as Git, CVS, and SVN
In the following screenshot, we can see how to configure virtualenv
in PyCharm:
In the preceding screenshot, we are setting the configuration related to establishing a new environment for the project using virtualenv.
Debugging with PyCharm
In this example, we are debugging a Python script that accepts two input parameters. An interesting topic is the possibility of adding a breakpoint to our script.
In the following screenshot, we are setting a breakpoint in the view_parameters
method:
With the View Breakpoint
option, we can see the breakpoint established in the script:
In the following screenshot, we can visualize the values of the parameters that contain the values we are debugging:
In this way, we can know the state of each of the variables at runtime, as well as modify their values to change the logic of our script.
Debugging with Python IDLE
Python IDLE is the default IDE that comes installed by default when you install Python in your operating system. When executing Python IDLE, it offers the possibility to debug your script and see errors and exceptions in the Python shell console:
In the preceding screenshot, we can see the output in the Python shell and the exception is related to File not found.