B
Text Editors and IDEs

Programmers spend a lot of time writing, reading, and editing code, and using a text editor or an IDE (integrated development environment) to make this work as efficient as possible is essential. A good editor will do simple tasks, like highlighting your code’s structure so you can catch common bugs as you’re working. But it won’t do so much that it distracts you from your thinking. Editors also have useful features like automatic indenting, markers to show appropriate line length, and keyboard shortcuts for common operations.

An IDE is a text editor with a number of other tools included, like interactive debuggers and code introspection. An IDE examines your code as you enter it and tries to learn about the project you’re building. For example, when you start typing the name of a function, an IDE might show you all the arguments that function accepts. This behavior can be very helpful when everything works and you understand what you’re seeing. But it can also be overwhelming as a beginner and difficult to troubleshoot when you aren’t sure why your code isn’t working in the IDE.

These days, the lines have blurred between text editors and IDEs. Most popular editors have some features that used to be exclusive to IDEs. Likewise, most IDEs can be configured to run in a lighter mode that’s less distracting as you work, but lets you use the more advanced features when you need them.

If you already have an editor or IDE installed that you like, and if it’s already configured to work with a recent version of Python that’s installed on your system, then I encourage you to stick with what you already know. Exploring different editors can be fun, but it’s also a way to avoid the work of learning a new language.

If you don’t already have an editor or IDE installed, I recommend VS Code for a number of reasons:

In this appendix, you’ll learn how to start configuring VS Code so that it works well for you. You’ll also learn some shortcuts that let you work more efficiently. Being a fast typist is not as important as many people think in programming, but understanding your editor and knowing how to use it efficiently is quite helpful.

With all that said, VS Code doesn’t work for everyone. If it doesn’t work well on your system for some reason, or if it’s distracting you as you work, there are a number of other editors that you might find more appealing. This appendix includes a brief description of some of the other editors and IDEs you should consider.

Working Efficiently with VS Code

In Chapter 1, you installed VS Code and added the Python extension as well. This section will show you some further configurations you can make, plus shortcuts for working efficiently with your code.

Configuring VS Code

There are a few ways to change the default configuration settings for VS Code. Some changes can be made through the interface, and some will require changes in configuration files. These changes will sometimes take effect for everything you do in VS Code, while others will affect only the files within the folder that contains the configuration file.

For example, if you have a configuration file in your python_work folder, those settings will only affect the files in that folder (and its subfolders). This is a good feature, because it means you can have project-specific settings that override your global settings.

Using Tabs and Spaces

If you use a mix of tabs and spaces in your code, it can cause problems in your programs that are difficult to diagnose. When working in a .py file with the Python extension installed, VS Code is configured to insert four spaces whenever you press the TAB key. If you’re writing only your own code and you have the Python extension installed, you’ll likely never have an issue with tabs and spaces.

However, your installation of VS Code may not be configured correctly. Also, at some point, you may end up working on a file that has only tabs or a mix of tabs and spaces. If you suspect any issue with tabs and spaces, look at the status bar at the bottom of the VS Code window and click either Spaces or Tab Size. A drop-down menu will appear that lets you switch between using tabs and using spaces. You can also change the default indentation level and convert all indentation in the file to either tabs or spaces.

If you’re looking at some code and you’re not sure whether the indentation consists of tabs or spaces, highlight several lines of code. This will make the invisible whitespace characters visible. Each space will show up as a dot, and each tab will show up as an arrow.

Changing the Color Theme

VS Code uses a dark theme by default. If you want to change this, click File (Code in the menu bar on macOS), then click Preferences and choose Color Theme. A drop-down list will appear, and it will let you choose a theme that works well for you.

Setting the Line Length Indicator

Most editors allow you to set up a visual cue, usually a vertical line, to show where your lines should end. In the Python community, the convention is to limit lines to 79 characters or less.

To set this feature, click Code and then Preferences, and then choose Settings. In the dialog that appears, enter rulers. You’ll see a setting for Editor: Rulers; click the link labeled Edit in settings.json. In the file that appears, add the following to the editor.rulers setting:

settings.json

    "editor.rulers": [
    80,
    ]

This will add a vertical line in the editing window at the 80-character position. You can have more than one vertical line; for example, if you want an additional line at 120 characters, the value for your setting would be [80, 120]. If you don’t see the vertical lines, make sure you saved the settings file; you may also need to quit and reopen VS Code for the changes to take effect on some systems.

Simplifying the Output

By default, VS Code shows the output of your programs in an embedded terminal window. This output includes the commands that are being used to run the file. For many situations, this is ideal, but it might be more distracting than you want when you’re first learning Python.

To simplify the output, close all the tabs that are open in VS Code and then quit VS Code. Launch VS Code again and open the folder that contains the Python files you’re working on; this could just be the python_work folder where hello_world.py is saved.

Click the Run/Debug icon (which looks like a triangle with a small bug), and then click Create a launch.json File. Select the Python options in the prompts that appear. In the launch.json file that opens, make the following change:

launch.json

{
    --snip--
    "configurations": [
        {
            --snip--
            "console": "internalConsole",
            "justMyCode": true
        }
    ]
}

Here, we’re changing the console setting from integratedTerminal to internalConsole. After saving the settings file, open a .py file such as hello_world.py, and run it by pressing CTRL-F5. In the output pane of VS Code, click Debug Console if it’s not already selected. You should see only your program’s output, and the output should be refreshed every time you run a program.

Exploring Further Customizations

You can customize VS Code in many ways to help you work more efficiently. To start exploring the customizations available, click Code and then Preferences, and then choose Settings. You’ll see a list titled Commonly Used; click any of the subheadings to see some common ways you can modify your installation of VS Code. Take some time to see if there are any that make VS Code work better for you, but don’t get so lost in configuring your editor that you put off learning how to use Python!

VS Code Shortcuts

All editors and IDEs offer efficient ways to do common tasks that everyone needs to do when writing and maintaining code. For example, you can easily indent a single line of code or an entire block of code; you can just as easily move a block of lines up or down in a file.

There are too many shortcuts to describe fully here. This section will share just a few that you’ll likely find helpful as you’re writing your first Python files. If you end up using a different editor than VS Code, make sure you learn how to do these same tasks efficiently in the editor you’ve chosen.

Indenting and Unindenting Code Blocks

To indent an entire block of code, highlight it and press CTRL-], or ⌘-] on macOS. To unindent a block of code, highlight it and press CTRL-[, or ⌘-[ on macOS.

Commenting Out Blocks of Code

To temporarily disable a block of code, you can highlight the block and comment it so Python will ignore it. Highlight the section of code you want to ignore and press CTRL-/, or -/ on macOS. The selected lines will be commented out with a hash mark (#) indented at the same level as the line of code, to indicate these are not regular comments. When you want to uncomment the block of code, highlight the block and reissue the same command.

Moving Lines Up or Down

As your programs grow more complex, you may find that you want to move a block of code up or down within a file. To do so, highlight the code you want to move and press ALT-up arrow, or Option-up arrow on macOS. The same key combination with the down arrow will move the block down in the file.

If you’re moving a single line up or down, you can click anywhere in that line; you don’t need to highlight the whole line to move it.

Hiding the File Explorer

The integrated file explorer in VS Code is really convenient. However, it can be distracting when you’re writing code and can take up valuable space on a smaller screen. The command CTRL-B, or ⌘-B on macOS, toggles the visibility of the file explorer pane.

Finding Additional Shortcuts

Working efficiently in an editing environment takes practice, but it also takes intention. When you’re learning to work with code, try to notice the things you do repeatedly. Any action you take in your editor likely has a shortcut; if you’re clicking menu items to carry out editing tasks, look for the shortcuts for those actions. If you’re switching between your keyboard and mouse frequently, look for the navigation shortcuts that keep you from reaching for your mouse so often.

You can see all the keyboard shortcuts in VS Code by clicking Code and then Preferences, and then choosing Keyboard Shortcuts. You can use the search bar to find a particular shortcut, or you can scroll through the list to find shortcuts that might help you work more efficiently.

Remember, it’s better to focus on the code that you’re working on, and avoid spending too much time on the tools you’re using.

Other Text Editors and IDEs

You’ll hear about and see people using a number of other text editors. Most of them can be configured to help you in the same way you’ve customized VS Code. Here’s a small selection of text editors you might hear about.

IDLE

IDLE is a text editor that’s included with Python. It’s a little less intuitive to work with than other, more modern editors. However, you’ll see references to it in other tutorials aimed at beginners, so you might want to give it a try.

Geany

Geany is a simple text editor that displays all of your output in a separate terminal window, which helps you become comfortable using terminals. Geany has a very minimalist interface, but it’s powerful enough that a significant number of experienced programmers still use it.

If you find VS Code too distracting and full of too many features, consider using Geany instead.

Sublime Text

Sublime Text is another minimalist editor that you should consider using if you find VS Code too busy. Sublime Text has a really clean interface and is known for working well even on very large files. It’s an editor that will get out of your way and let you focus on the code you’re writing.

Sublime Text has an unlimited free trial, but it’s not free or open source. If you decide you like it and can afford to purchase a full license, you should do so. The purchase is a one-time fee; it’s not a software subscription.

Emacs and Vim

Emacs and Vim are two popular editors favored by many experienced programmers, because they’re designed so you can use them without your hands ever having to leave the keyboard. This makes writing, reading, and modifying code very efficient, once you learn how the editor works. It also means both editors have a fairly steep learning curve. Vim is included on most Linux and macOS machines, and both Emacs and Vim can be run entirely inside a terminal. For this reason, they’re often used to write code on servers through remote terminal sessions.

Programmers will often recommend that you give them a try, but many proficient programmers forget how much new programmers are already trying to learn. It’s good to be aware of these editors, but you should hold off on using them until you’re comfortable working with code in a more user-friendly editor that lets you focus on learning to program, rather than learning to use an editor.

PyCharm

PyCharm is a popular IDE among Python programmers because it was built to work specifically with Python. The full version requires a paid subscription, but a free version called the PyCharm Community Edition is also available, and many developers find it useful.

If you try PyCharm, be aware that, by default, it sets up an isolated environment for each of your projects. This is usually a good thing, but it can lead to unexpected behavior if you don’t understand what it’s doing for you.

Jupyter Notebooks

Jupyter Notebook is a different kind of tool than traditional text editors or IDEs, in that it’s a web app primarily built of blocks; each block is either a code block or a text block. The text blocks are rendered in Markdown, so you can include simple formatting in your text blocks.

Jupyter Notebooks were developed to support the use of Python in scientific applications, but they have since expanded to become useful in a wide variety of situations. Rather than just writing comments inside a .py file, you can write clear text with simple formatting, such as headers, bulleted lists, and hyperlinks in between sections of code. Every code block can be run independently, allowing you to test small pieces of your program, or you can run all the code blocks at once. Each code block has its own output area, and you can toggle the output areas on or off as needed.

Jupyter Notebooks can be confusing at times because of the interactions between different cells. If you define a function in one cell, that function is available to other cells as well. This is beneficial most of the time, but it can be confusing in longer notebooks and if you don’t fully understand how the Notebook environment works.

If you’re doing any scientific or data-focused work in Python, you’ll almost certainly see Jupyter Notebooks at some point.