A question many of my students often ask is: which IDE should I use?

This question comes in many formats:

  • What do you think of Atom vs VSCode?
  • Should I use VSCode to program Python?
  • Which IDE should I use?
  • Do you use PyCharm?
  • Is it OK to use PyCharm Community Edition, or do you need to pay?

The list is long!


This post is part of #100DaysOfPython, check out yesterday's post here if you haven't already. Or go to the index of all 100 days.


Let me preface this article—which is about why I think using PyCharm is easiest—by saying that you should use what you are most comfortable with.

If you are not comfortable (yet) with anything, PyCharm Community Edition will be simplest and give you the most bang for your buck as a beginner.

What can you use to program in Python?

As we've seen in a previous blog posts, as long as you can create a file whose names ends in .py and you can launch a Terminal (cmd.exe on Windows), you can execute Python programs.

You can create .py files with any text editor. That means you can use anything you want.

  • Text editors and glorified text editors:
    • Notepad
    • TextEdit
    • vim
    • emacs
    • nano
    • TextWrangler
    • Notepad++
  • Text editors designed for programming:
    • Atom
    • VSCode
    • Sublime Text
    • TextMate
  • Integrated Development Environments:
    • PyCharm
    • Zeus

Each one is used by many software developers. They'll tell you it's because it can do this or that, but the reality is it's just what they are accustomed to. They work fastest with it, they feel it doesn't get in the way, or they feel it helps them.

The majority of popular editors have plugins that you can add to them to add functionality, so nearly all editors can do anything.

Why I think you should start with PyCharm

I would hate for you to become discouraged because you don't know what editor to use. As someone who has been writing code for a long time, I think it's completely ludicrous!

PyCharm comes out of the box with a bunch of functionality that helps you, while not getting in the way.

Some people feel PyCharm does too much for you as a beginner. If you are learning Python from this blog, you have seen that I've been recommending you run your Python applications from the Terminal. That gets you closer to what's actually happening in the computer, and negates that potential bad side effect of using PyCharm.

Here are two things that PyCharm makes very easy, which honestly just aren't normally:

  • Refactoring; and
  • Debugging.

Out of the editors mentioned above, only VSCode gets even close to providing the functionality PyCharm provides—even with plugins.

Refactoring

Refactoring means modifying existing code without adding new functionality, so that the new code is more readable by a human.

Look at this example:

x = [1, 3, 5, 7, 9]

print([z for z in x if z % 3 == 0])

Not particularly obvious what it's doing. You have a list of numbers with a terrible name, and then you're printing a list comprehension.

Using PyCharm we can easily change the name x for a better name in the entire file.

Select the x in the code and go to the menu Refactor -> Rename. Often the shortcut is SHIFT+F6.

Renaming variable occurrences

Then, you can rename all occurrences. In this very short program it's only two occurrences, but it's very useful in larger programs.

odd_numbers = [1, 3, 5, 7, 9]

print([z for z in odd_numbers if z % 3 == 0])

So what are we printing? We can read through the code and figure we're creating a new list from the odd numbers if they are fully divisible by 3—we're printing factors of three.

Select the list comprehension and go to Refactor -> Extract -> Variable, or the shortcut is:

  • CMD+OPTION+V on OS X; or
  • CTRL+OPTION+V on Windows.

PyCharm then turns the value you've selected into a local variable which you can name better. Let's call it odd_factors_of_three.

odd_numbers = [1, 3, 5, 7, 9]

odd_factors_of_three = [z for z in odd_numbers if z % 3 == 0]

print(odd_factors_of_three)

Now you don't need to even read the list comprehension. Your variable has such a great name, that it tells you much quicker what it does than the code ever could.

These are two examples of the refactoring that PyCharm can help you with. I'd encourage you to look into the other things it can do as well. Send us a tweet if you want a hand with anything!

Debugging

Something that every Python programer does too late is debugging. Programming is mainly about debugging! Learn what it is and how to do it well; your programming will go much smoother.

When you debug, you can tell your program to pause before reaching a particular line of code. Then, you can inspect your variables and you can run arbitrary pieces of code. Sounds weird? Let's look at an example:

Imagine you have a program that asks the user for details of 10 new students, and saves them into a list. Something like this:

students = []

for i in range(10):
    name = input("Enter the new student name: ")
    school = input("Enter the new student school: ")
    
    student = {'name': name, 'school': school}
    students = [student]
    
print(students)

If you notice the bug, good on you! Some would not notice it, and then would be confused as to why it is not working. It's totally normal to not notice every bug in an application. That's where debugging comes in!

If you run this application, it would ask you to enter the names and schools of 10 students, but when you reach the end of the program it would only print the details of the last student entered:

Enter the new student name: Jose
Enter the new student school: Computing
.
.
.
Enter the new student name: Rolf
Enter the new student school: Medicine
[{'name': 'Rolf', 'school': 'Medicine'}]

But why is it doing that? Let's use PyCharm to set a breakpoint on the line where we're modifying the students variable:

Setting a breakpoint on a line

And then right-click the file and find the Debug ... menu item.

Your program will run as normal—which means you'll have to enter the details of the first student—and then stop right before the line runs. Now you can see the current status of your program in your window. Beside every variable, you have its value. At the bottom of your screen you also have the current variables.

Find these three buttons in your PyCharm window. They are not all together.

Three icons to find

From the left, they do:

  • Go to the next line of this file;
  • Open the expression evaluator, which lets you run arbitrary pieces of code.
  • Resume running the code until the next breakpoint is hit.

We want to know why this line isn't doing what we think. We also want to understand what it is actually doing. We think it adds the student dictionary to the list.

Let's press the "Resume Program" button, which will run over the loop again. We'll enter new student details (I used "Rolf" and "Medicine"), and then our program will stop once again.

Now, our code shows that the students list already has a value:

students = []  # students = <class 'list'>: [{'name': 'Jose', 'school': 'Computing'}]
.
.
.

That's great! Now let's check what our line of code is doing.

Instead of pressing "Resume Program", let's use the "Expression Evaluator", which lets us run arbitrary code.

In the evaluator, type the line we are about to run and see what happens. That lets us run the code and analyse what it does before it actually happens. Plus it lets us inspect any changes in variables:

Running our code on the Expression Evaluator

The result is None, which is reasonable since this code just sets a variable, but you can see that our students variable now has a completely different value!

students = []  # students = <class 'list'>: [{'name': 'Rolf', 'school': 'Medicine'}]
.
.
.

Now we know that what our code does is overwrite our students variable with a new list every time! We don't want that! We want to add to the existing list.

Thus, it is search time: http://google.com/?q=add+to+list+python or check out yesterday's blog post!

It's possible sometimes you won't know where to put your breakpoint. That's fine! Put the breakpoint in the first line. Then go over each line by using the first button in the image above, and inspect what the program does at each step of the way.

Other functionality

PyCharm has a lot of other functionality, such as connections to database, plugins, and version control integration, and a lot more.

If you find yourself doing something outside of PyCharm that isn't very simple, feel free to search around to see if there's a way to make it simpler with PyCharm. Other than refactoring and debugging, I would encourage you to learn things outside of the IDE first, and then use it to make things simpler once you know what's going on.

Remember: the most important skill in software development is problem solving. The tools and languages you use are much less important!

Tomorrow we've got a challenge for you. Give it a go!.