Switching to PyCharm
As I’ve been plowing away at my little game, it kept growing and now it’s not really all that small again… and growing still. So far I’ve been using IDLE, the programming environment that comes with the official Python package, and for the most part, it did the job just fine.
But as my source file got longer and more cluttered, it was time to create a real project and divide my source code into separate modules, each focusing on one thing. This way I’ll be able to find my way around faster and won’t have to scroll through endless pages of code to get from one method to the next.
After a bit of research, I decided to go with PyCharm. It is a professional, IDE that has tons of pro features (more than I’ll ever need, I suspect) and made a really good impression on me. Though normally a paid product, JetBrains, the company who creates PyCharm, also offers PyCharm Community Edition, a free version of the package that suits me perfectly. It may not include the web development and SQL support of the paid versions, but those are things I’m currently not interested in at all, anyway.
It feels kind of cool to switch from a small, minimal beginner’s development environment to a full-featured professional tool. It’s like the training wheels come off… a bit intimidating at first, but also you can tell that things are operating on a whole different level.
After installing PyCharm, the next step for me was to turn everything into an actual Project and break my game into smaller modules, which is really just a fancy name of saying that I split the source code into different files. You can see the new project structure in this screenshot.
Putting code into modules required a few code changes, however, because now that classes, methods, and variables are no longer in the same file, they can no longer be accessed without proper external references. I had to add various import statements to each of my files to makes sure these references are available to the code.
I placed all my global variables and definitions in a file called globals.py so I can keep them all together and access them from any module, simply by importing global in each module.
However, accessing these variables required a code change throughout the project because instead of accessing simply theVerb, for example, now I have to access the same variable through globals.theVerb. It’s a bit more tedious, but I’ll get used to it, no doubt.
It was here, however, that I immediately felt the power of a professional IDE like PyCharm, because, with a single mouse click, it was able to automatically update all occurrences in the entire project. Nice!
The cool features don’t end there, though. As I’ve been modifying everything for the new project structure, I came across a good number of other helpful features. The syntax highlighting is definitely a boon and much better than that found in IDLE, but what I really love is the code completion. Simply start typing the name of a class or a variable and a small pop-up will appear with a selection of prospective words. When I begin to type Tokens, for example, immediately after the ‘o’, the pop-up displays the word Tokens and when I hit the Return key, it will automatically complete the word for me. but here’s where it gets even better. As soon as I type the period that follows a proper Token reference in my code, the pop-up will list ALL valid token names. You can see it in the screenshot below. I can then quickly select it with the cursor keys or continue writing, just as I please. This is very cool!
Another really useful function is how PyCharm allows you to to see all places in your code where you use a certain variable or method name. If you need to make quick changes, or simply need to check if you’ve done something already, this feature is pure gold and all it takes to get the info is one mouse click.
Perhaps the thing I like best about PyCharm is the debugger. I haven’t talked at all about how to find bugs in a program that is longer than just a few lines of code, and if you have a project that consists of various modules, a debugger is really critical. The better the debugger, the faster you will be able to isolate problems and get to work fixing them.
While IDLE had a debugger as well, somehow the one in PyCharm feels much more robust and detailed. It allows you to set breakpoints, of course, but as you trace through your code line by line, you get to see all the variables that are currently in play, you can quickly drill down into these variables and objects to find out if all the values are as they should be. It is pretty cool, being able to follow along, line by line, as your program executes and watch what happens to the variables.
There’s a lot more information provided as you debug your code, some of it way over my head I admit, but it feels extremely robust and is very easy to use and understand. It helped me quite a bit already to fix some unexpected issues that popped up when I changed some of my code to modules.
Not much code in this installment but I felt to write about this switch because it feels big and important. I am no longer just coding along. I feel like I am working on a real project… big and exciting and all. Stay tuned for more.