An editor is a program that lets us write longer programs than we can on
the >>> prompt. Then we can save the programs to files and run them.
You may have noticed that Python comes with an editor called IDLE, but
it's not really good enough for programming and I don't know any
experienced programmers who would actually use it.
In this chapter we'll download, install, set up and learn to use an alternative editor. This does not mean something like Microsoft Word or LibreOffice/OpenOffice Writer. These programs are for writing text, not for programming.
This tutorial uses an editor called Geany because it's easy to use and it works great on Windows, Mac OSX and Linux. You can use any editor you want, but I don't recommend learning any of these editors now. There are many editors to choose from and the choice of editor is a very personal thing.
Different programmers have different favorite editors, and they often recommend their own favorite editor to everyone. These editors are not good for beginners. If someone recommends one of these to you when you are getting started with Python, say no:
- Spyder
- PyCharm
- Emacs
- Vim
- NetBeans
- Spyder
These editors are missing some important features, so don't use these either:
- Wingware
- Windows Notepad
- Gedit
- Pluma
Editor setup sections in all Python tutorials I have seen are usually like "click here, then here". In this tutorial I'll show you what different settings do and why you need them.
This is important. Never use tabs in Python. Nobody else is using tabs, and the official style guide tells you to never use tabs.
However, you don't need to press the spacebar four times every time you want to indent. Your editor should give you four spaces when you hit the tab key. Some editors also remove four spaces when you hit backspace and there are four spaces before the cursor.
- Go to Edit at the top and select Preferences.
- Go to Editor at left.
- Go to Indenting at top.
- Select Spaces instead of Tabs.
- Go to Edit at the top and select Preferences.
- Go to Editor at top.
- Change the indent width to 4 and select Add spaces instead of tabs.
Emacs uses spaces with Python files by default.
Open your ~/.nanorc.
$ nano ~/.nanorc
Add these lines to it:
set tabsize 4
set tabstospaces
- Go to Document at the top, then Tab Size.
- Select 4.
- Also select Insert Spaces.
If you type a keyword, like if, it should show up with a different
color than the rest of your text. "Strings", # comments and
everything else should also have their own colors. This makes it much
easier to write code.
Most of the editors below have syntax highlighting turned on by default, but you can also change the colors.
Install more color schemes, then go to View, Change Color Scheme.
Click Fonts & Colors in the preferences and select another color theme.
Type M-x, type load-theme, press Tab twice to see a list of theme
names, then enter a theme name and press Enter. If you want to
automatically set the theme when Emacs starts, add
(load-theme 'your-theme-name) to your ~/.emacs.
Click View, go to Color Scheme and select whatever you want.
Some editors allow you to run your programs with a single keystroke, usually by pressing F5. This tutorial is written for Python 3 or newer, so your editor also needs to run the programs in Python 3 or newer.
If you are unsure which Python your editor runs, create a test file with the following contents:
import sys
print(sys.version)If the version starts with 2, it's too old.
-
Go to Build, then Set Build Commands.
-
Replace
pythonorpython2withpython3everywhere. Or if you are using Windows, runpythonon a terminal and enter these commands:>>> import sys >>> print(sys.executable)
You'll get a path to your python.exe. Replace
pythonin the build commands with this path. Most importantly, your Execute command should be"C:\your\path" "%f".
These editors don't support running programs with F5.
Usually I write something in Emacs, then I press Ctrl+Z to suspend
Emacs, run the program myself and then I run fg to get back to Emacs.
If you know how to run Python programs in Emacs and you'd like to write
about it here, tell me.
If you have trouble with this tutorial please tell me about it and I'll make this tutorial better. If you like this tutorial, please give it a star.
You may use this tutorial freely at your own risk. See LICENSE.