|
| 1 | +# Setting up an editor for programming |
| 2 | + |
| 3 | +An editor is a program that lets us write longer programs than we can |
| 4 | +write on the `>>>` prompt. Then we can save the programs to files and |
| 5 | +run them as many times as we want without writing them again. |
| 6 | + |
| 7 | +When programmers say "editor" they don't mean programs like Microsoft |
| 8 | +Word or LibreOffice/OpenOffice Writer. These programs are for writing |
| 9 | +text documents, not for programming. **Programming editors don't support |
| 10 | +things like bigger font sizes for titles or underlining bits of text**, |
| 11 | +but instead they have features that are actually useful for programming, |
| 12 | +like automatically displaying different things with different colors. |
| 13 | + |
| 14 | +If you are on Windows or Mac OSX you have probably noticed that your |
| 15 | +Python came with an editor called IDLE. We are not going to use it |
| 16 | +because it's lacking some important features, and most experienced |
| 17 | +programmers (including me) don't use it or recommend it to anyone. |
| 18 | + |
| 19 | +In this chapter we'll download, install, set up and learn to use a |
| 20 | +better editor. The setup part will take some time, but it's worth it and |
| 21 | +correct settings will help you avoid many problems later. |
| 22 | + |
| 23 | +## Which editor should I use? |
| 24 | + |
| 25 | +These instructions are written for an editor called Geany. Its default |
| 26 | +settings are actually not very good for writing Python code, but you |
| 27 | +will learn to change the settings so you won't have trouble with |
| 28 | +switching to another editor later if you need to. |
| 29 | + |
| 30 | +Different programmers have different favorite editors that they use, and |
| 31 | +the choice of editor is a very personal thing. You can use almost any |
| 32 | +editor you want if you make sure that you change its settings the same |
| 33 | +way we change Geany's settings in this tutorial. |
| 34 | + |
| 35 | +## Bad editors |
| 36 | + |
| 37 | +**[Skip this part](#installing-your-new-editor) if you are going to use |
| 38 | +Geany.** |
| 39 | + |
| 40 | +But some editors are not good enough for actually making something with |
| 41 | +them. These editors cannot be set up using these instructions because |
| 42 | +they don't have some of the features we need, so **don't use these |
| 43 | +editors for writing Python**: |
| 44 | + |
| 45 | +- Gedit |
| 46 | +- IDLE |
| 47 | +- Nano |
| 48 | +- Notepad |
| 49 | +- Pluma |
| 50 | +- Wingware |
| 51 | + |
| 52 | +On the other hand, some editors have too many features for getting |
| 53 | +started with programming. They are not bad, but they are not meant for |
| 54 | +beginners. So **I don't recommend using these editors yet**: |
| 55 | + |
| 56 | +- Emacs |
| 57 | +- NetBeans |
| 58 | +- PyCharm |
| 59 | +- Spyder |
| 60 | +- Vim |
| 61 | + |
| 62 | +These lists don't contain all bad editors, but these are editors that |
| 63 | +people often try to use. If you know a bad editor and you think I should |
| 64 | +mention it here, please [let me know](../contact-me.md). |
| 65 | + |
| 66 | +## Installing your new editor |
| 67 | + |
| 68 | +Installing Geany is easy. If you are using Windows or Mac OSX, go to |
| 69 | +[the official Geany download |
| 70 | +page](http://www.geany.org/Download/Releases) and click the correct |
| 71 | +download link. If you are using Linux, just install Geany from the |
| 72 | +package manager like any other program. For example, on Debian-based |
| 73 | +distributions (e.g. Ubuntu) you can type `sudo apt install geany` on a |
| 74 | +terminal. |
| 75 | + |
| 76 | +When you have Geany installed, you can launch it like any other program. |
| 77 | +By default, it has a big sidebar and bottom area, but we don't need them |
| 78 | +so you can start by dragging them away. Geany should look roughly like |
| 79 | +this after that: |
| 80 | + |
| 81 | + |
| 82 | + |
| 83 | +## Dark background |
| 84 | + |
| 85 | +Geany has a white background by default, but we'll tell Geany to use a |
| 86 | +black background instead. Your eyes will thank you for doing this when |
| 87 | +you have been programming for a few hours. |
| 88 | + |
| 89 | +1. Click *Edit* at top and click *Preferences*. |
| 90 | +2. Click *Editor* at left. |
| 91 | +3. Click *Display* at top right. |
| 92 | +4. Check *Invert syntax highlighting colors*. |
| 93 | +5. Click *OK*. |
| 94 | + |
| 95 | +If you don't like the colors, you can install more [color |
| 96 | +schemes](https://github.com/geany/geany-themes/) and then go to *View* |
| 97 | +at top and click *Change Color Scheme*. |
| 98 | + |
| 99 | +## Opening and saving files |
| 100 | + |
| 101 | +Now it's time to create our first file. Geany creates a new file when |
| 102 | +you open it for the first time, but it doesn't know that it's going to |
| 103 | +be a Python file. We need to tell that to Geany by saving the file |
| 104 | +first. You can use the *File* menu at top or the buttons below the |
| 105 | +menus, just like in most other programs. |
| 106 | + |
| 107 | +Save the empty file on your desktop as `test.py`. The `.py` at the end |
| 108 | +of the name is important, it tells Geany and other programs that it's a |
| 109 | +Python file. |
| 110 | + |
| 111 | +## Automatic tab expanding |
| 112 | + |
| 113 | +Open a Python file in your editor. Then press the tab key, and the |
| 114 | +cursor should move right. Now press the left arrow key. See how the |
| 115 | +cursor jumps over the empty space? The empty space is actually a tab |
| 116 | +character, but Python's style guide recommends using four spaces instead |
| 117 | +of tab characters so we'll need to change Geany's settings. |
| 118 | + |
| 119 | +1. Click *Edit* at top, then click Preferences. |
| 120 | +2. Click *Editor* at left. |
| 121 | +3. Click *Indentation* at top. |
| 122 | +4. Select *Spaces* instead of *Tabs*. |
| 123 | +5. Click *OK*. |
| 124 | +6. Click *Project* at top and click *Apply Default Indentation*. |
| 125 | + |
| 126 | +Delete the whitespace, then press tab and press the arrow left again. |
| 127 | +Now the cursor should move one space at a time. |
| 128 | + |
| 129 | +**TODO:** animated gifs that show what's going on. |
| 130 | + |
| 131 | +## Stripping spaces when pressing Enter |
| 132 | + |
| 133 | +Press Tab and then press Enter, and Geany should add spaces to the new |
| 134 | +line automatically for you. If you are not using Geany, make sure that |
| 135 | +your editor does this too. |
| 136 | + |
| 137 | +Now press the arrow up and then move left and right. You'll notice that |
| 138 | +the previous line has spaces left over on it. Leaving useless spaces to |
| 139 | +ends of lines like this is usually considered bad style, but it's easy |
| 140 | +to tell Geany to get rid of them: |
| 141 | + |
| 142 | +1. Click *Edit* at top and click *Preferences* |
| 143 | +2. Click *Editor* at left. |
| 144 | +3. Click *Features* at top. |
| 145 | +4. Check *Newline strips trailing spaces*. |
| 146 | +5. Click *OK*. |
| 147 | + |
| 148 | +Press tab and Enter again. If you go back to the previous line now there |
| 149 | +is no whitespace on it. |
| 150 | + |
| 151 | +**TODO:** again, animated gifs |
| 152 | + |
| 153 | +## Maximum line length |
| 154 | + |
| 155 | +You have probably noticed that Geany displays a thin, green-ish line at |
| 156 | +right from where you type. The idea is that you should avoid writing |
| 157 | +anything longer than where this line is. Your programs will work if they |
| 158 | +contain lines that go past this marker, but it's not recommended because |
| 159 | +shorter lines are nicer to work with in general. People with small |
| 160 | +screens can work with your code, and people with large screens can have |
| 161 | +multiple files opened at the same time. |
| 162 | + |
| 163 | +By default, there's room for 72 characters before the marker. Staying in |
| 164 | +72 characters is not always easy, and that's why Python's style guide |
| 165 | +recommends 79 instead of 72. Let's move Geany's marker to 79 characters: |
| 166 | + |
| 167 | +1. Click *Edit* at top and click *Preferences* |
| 168 | +2. Click *Editor* at left. |
| 169 | +3. Click *Display* at top. |
| 170 | +4. Change 72 to 79. |
| 171 | +5. Click *OK*. |
| 172 | + |
| 173 | +## Running things from the editor |
| 174 | + |
| 175 | +This setting up stuff is boring! Let's write some code. |
| 176 | + |
| 177 | +We'll use this program to check if our editor is set up correctly. |
| 178 | +Create a new file and type this into it: |
| 179 | + |
| 180 | +```python |
| 181 | +import sys |
| 182 | +print(sys.version) |
| 183 | +``` |
| 184 | + |
| 185 | +You don't need to understand this program yet, you'll learn what it does |
| 186 | +later in this tutorial. You can type this program on the `>>>` prompt to |
| 187 | +see what it does if you want to. |
| 188 | + |
| 189 | +Now save the program somewhere as `test.py`. It doesn't matter what the |
| 190 | +name of the file is, but it needs to end with `.py`. This way Geany |
| 191 | +knows that it's a Python file. |
| 192 | + |
| 193 | +Next we'll need to change Geany's settings so that it runs the program |
| 194 | +correctly. You need to have a Python file opened to change these |
| 195 | +settings. |
| 196 | + |
| 197 | +1. Click *Build* at top, then click *Set Build Commands*. |
| 198 | +2. The *Execute* command is probably `python "%f"`. Change it to |
| 199 | + `py "%f"` if you are using Windows and `python3 "%f"` if you are |
| 200 | + using Linux or Mac OSX. |
| 201 | +3. Click *OK*. |
| 202 | + |
| 203 | +Note that the first part of our *Execute* is the same thing [we type on |
| 204 | +a PowerShell or terminal](installing-python.md#running-python). Actually |
| 205 | +the whole command means "run this file using my Python". |
| 206 | + |
| 207 | +Now press the F5 key at the top of your keyboard to run the file we |
| 208 | +wrote. It should open up in a terminal or command prompt and print the |
| 209 | +same Python version as the `>>>` prompt prints when we open it. If the |
| 210 | +version starts with 2 it's too old, and you can [ask |
| 211 | +me](../contact-me.md) or some other experienced programmer for help. |
| 212 | + |
| 213 | +## Important notes |
| 214 | + |
| 215 | +Now your editor is ready to go. There are a couple important things that |
| 216 | +you need to keep in mind when reading the rest of this tutorial: |
| 217 | + |
| 218 | +- When a code example starts with `>>>`, type it to the `>>>` prompt. If |
| 219 | + it doesn't, create or open a `.py` file with your editor, type the |
| 220 | + code into it and run the file. |
| 221 | +- When we type some code to the `>>>` prompt it [echoes back the |
| 222 | + result](getting-started.md) [when it's not None](variables.md#none), |
| 223 | + but code in a file does nothing to the result. For example, typing |
| 224 | + `1 + 2` to the `>>>` prompt makes it echo `3`, but we need to do |
| 225 | + `print(1 + 2)` if we want to do the same thing in a file. |
| 226 | + |
| 227 | +## You're done! |
| 228 | + |
| 229 | +This probably felt like a lot of work, but don't worry, there will be no |
| 230 | +more things like this in the future. Now we have installed and set up |
| 231 | +all the tools we need and we can continue learning Python. |
0 commit comments