Feature Spotlight: Refactoring Python code

We all know that code can’t be crafted ideally from the beginning, especially in large projects. There is always a need to change something during the active development or maintenance stages. In fact refactoring is something you do all the time in small steps. You don’t simply decide to refactor – you change your code because you want to do something else, and refactoring helps you do that other thing.

As Martin Fowler says: “Refactoring is a controlled technique for improving the design of an existing code base. Its essence is applying a series of small behavior-preserving transformations, each of which “too small to be worth doing”. However the cumulative effect of each of these transformations is quite significant.”

Refactorings can be a simple rename of a variable or extracting a superclass from the body of an existing class. PyCharm provides many refactorings for the various languages it supports. It carries them out automatically, making sure existing code is updated and will not break apart.

Today we’ll take a look at the refactoring features available in PyCharm that reduce the risk of introducing errors while you are carrying out the restructuring.

Note: While this blog post covers refactorings for Python code, the IDE also supports refactorings for other languages like JavaScript, HTML and so forth. Give refactoring a try in these languages as well.

Refactoring in PyCharm

Before we dive into all available refactorings, let’s see what a typical refactoring looks like. Sure, they all do different things, still there are some things they have in common. Let’s try renaming a class in our code.

To do this, we can use the Refactor | Rename… context menu on a class name, or place the cursor on it and use the Shift+F6 keyboard shortcut to invoke the rename refactoring immediately:


For every refactoring, we’ll get a different dialog in which we can provide the options for it. In this case we have to provide a new name for the class we’re about to rename. Some of the options in this dialog will be available for other refactorings as well: we can (optionally) search in comments and strings, search for text occurrences, and so on.

When invoking a refactoring in PyCharm, the IDE will:

  • Perform the refactoring
  • Track down and correct the affected code references automatically
  • Warn about occurrences it cannot update automatically
  • Save the previous state of your code, so you can always revert safely

If you are unsure about the outcome, consider clicking the Preview button before running the refactoring. It will open the Find Refactoring Preview tool window and show you all of the actions the refactoring will perform:


In this case, it will:

  • Rename the class and its derived class
  • Update all code references
  • Update one of the import statements in the project

We can filter and search in this tool window. Also we can optionally select one of the occurrences and use Delete to have PyCharm ignore it whenever we perform the refactoring:


This may come in handy when we’re also searching and replacing in comments and strings. Usually we will not exclude actual code refactorings.

Clicking Do Refactor will perform the refactoring and update our project’s codebase.

Another important thing to know is that PyCharm lets you undo everything, and that includes refactorings, no matter how complex code transformations they cause. Just press Ctrl + Z (Cmd + Z for Mac), and you’re back to where you were before inadvertently scrambling a few thousands lines of code. Another way to get back in code state is to use Local history by invoking Ctrl + Shift + A (Cmd + Shift +A for Mac) and typing “Local history”:


More information on local history can be found in one of our previous blog posts.

The Refactor This Action

While most refactorings in PyCharm have their own shortcuts, we may not know all of them. We may also be unfamiliar with the various refactorings available for a given file or a symbol. And that’s where the Refactor This action comes in handy!

In the Project View, Structure Tool Window, Editor or a UML Class Diagram, place the cursor on any file or symbol and use the Refactor | Refactor This context menu or press Ctrl+Shift+Alt+T (Ctrl-T on Mac OS X). This will display a pop-up with the different refactorings that we could carry out:


Using the up/down keys and Enter (or the numeric identifier for each entry in the pop-up) we can invoke the refactoring.

Available refactorings

PyCharm comes with lots of refactorings. I won’t go over all of them them here, but rather list the most essential ones. For every refactoring, I’ll add a link to the PyCharm Web Help which contains a full description of all available options for a given refactoring.

Note: All refactorings are available from the Refactor context menu or via their respective keyboard shortcuts.

That’s all for today. I hope you’ll enjoy this wide range of refactoring capabilities provided by PyCharm.

See you next week, and refactor safely!

Posted in Cool Feature | 1 Comment

Feature Spotlight: Navigating Python Code

As programmers we read more than we write. And when projects we’re working on become larger and more complex, the problem of understanding and getting around unfamiliar or even your own code becomes more and more essential. The faster and easier you navigate through the code – the faster you can read it, understand its concepts and flows. Eventually this reflects on how productive you are in general.

Simple textual search is neither convenient nor effective when working with large projects. Sorting out thousands lines of Python code without special assistance can turn into an extremely difficult and time consuming task.

Here come different tools that make developer’s life much easier. It can be a plugin for a text editor such as ctags or vimpy for the Vim editor, or SublimeCodeIntel, SublimeRope, SublimePythonIDE, or whatever else for Sublime Text.

Using plugins for a text editor has several downsides, though. For example, if you want to turn Sublime or Vim into something that will provide you code completion, navigation, refactorings, add PEP8 checks or do linting for Python, you’ll have to install several additional plugins which won’t share information about the code with each other but rather maintain each own knowledge base.

Now, what if you use other languages besides Python in your project, such as JavaScript, HTML or even Django templates? You’ll have to find a particular plugin that supports your language. Hopefully this would work, however eventually you’ll end up with quite a messy arrangement – an editor with a bunch of plugins working independently and unreliably – hard to set up and a pain to maintain.

Fortunately, we’ve got PyCharm that makes things easy and smooth, boosting your productivity along the way. What distinguishes PyCharm the most is that being an IDE it seamlessly integrates different subsystems that are happy to work together, sharing data and source code knowledge between each other. This way PyCharm’s advanced code analysis, both static and dynamic, affects different subsystems, such as code completion, navigation, refactorings etc. The IDE knows everything about your code and you get the top-notch navigation solution that lets you focus on the actual development and makes you as fast and efficient as you can be.

Lets take a closer look at what PyCharm has to offer in regard to source code navigation:

1. Recent files
Most of the time you work with a finite set of files. It means you have to switch between them quickly all the time. The real time-saver here is the action called Recent files via Ctrl + E (Cmd + E for Mac). By default the focus is on the last accessed file. In the meantime you can open any tool window through this action:


Another useful feature similar to Recent files is Recently edited files, available via Shift + Ctrl + E (Shift + Cmd + E for Mac). This is the easiest way to get back to the code you are working on.

2. Structure
When you are not switching between files you’re probably navigating within one. The shortest way to do it is to press Ctrl + F12 (Cmd + F12 for Mac). The popup shows you the structure of file and allows you to quickly navigate to any item. Just as always you don’t need to touch your mouse:


Note that the speed search works here as well. Hit the shortcut twice to display inherited members.

3. Switcher
Just like the task switcher in an operating system, PyCharm provides a switcher between opened files and tool windows. The action is available via Ctrl+Tab:


4. Jump to navigation bar
Another useful feature worth to mention is Jump to navigation bar via Alt + Home (Alt + ↖ for Mac). The navigation bar at the top of the window is useless most of the time. That’s why developers usually hide it and call on demand via a shortcut.
The navigation bar supports speed search and other actions, such as code generations, documentation popup, etc:


5. Navigate between methods
If you need to jump over the methods quickly, just press Alt + ↑↓ (Ctrl + ↑↓ for Mac):


A similar action for navigating between braces is available via Ctrl + { and Ctrl + } (Cmd + { and Cmd + } for Mac).

6. Navigate to anything
The next feature, which is one of the most used in PyCharm, is Navigate to Class, File and Symbol.
This feature is available via Ctrl + N (Cmd + N for Mac) and supports sophisticated expressions, including camel humps, path, line navigate to, middle name matching, and many more. If you call it twice it shows you the results out of the project classes:


Just like for classes it works for files as well, via Ctrl + Shift + N (Cmd + Shift + N for Mac). The results are sorted by relevance to the expression:


And finally the most tricky feature is to navigate to a symbol via Ctrl + Alt + Shift + N (Alt + Cmd + Shift + N for Mac). The IDE constantly maintains the index of all symbols in the project (and within libraries as well) for quick resolve and navigation. You can type an expression and find a method or a field you need in a second:


7. Select in
If you need to open the file in a particular tool window (or Finder/Explorer) you can do it via Select In action by pressing Alt + F1:


8. Method Call hierarchy

Thanks to it’s fantastic static code analysis capabilities, PyCharm intelligently analyses the code and builds a call hierarchy trees for method calls in different scopes. Place the caret to a method name and press Ctrl + Alt +H (Cmd + Alt + H for Mac):


9. Other notable navigation features

  • Navigate to line number via Ctrl + G (Cmd + G for Mac)
  • Navigate to Custom Folding via Ctrl + Alt + Period
  • Related files via Gutter
  • Navigate to declaration via Ctrl + B (Cmd + B for Mac)
  • Navigate to implementations via Ctrl + Alt + B (Alt + Cmd + B for Mac)
  • Navigate to type declaration via Ctrl + Shift + B (Shift + Cmd + B for Mac)
  • Navigate to Super Method via Ctrl + U (Cmd + U for Mac)
  • Navigate to the last edit location via Shift + Ctrl + Backspace (Shift + Cmd + Backspace for Mac)
  • Navigate to the next error or warning via F2
  • Navigate to a related unit test via Shift + Ctrl + T (Shift + Cmd + T for Mac)
  • Navigate to a related symbol via Ctrl + Alt + Home (Cmd + Alt + Home)

All the navigational features mentioned here as well as other helpful functions are accessible via special Navigate system menu:


I hope you’ll enjoy these slick navigational features and save a great deal of time while at it.
And if you’d like to learn more about PyCharm’s code navigation capabilities, see our tutorial or online help.

Have a nice day!

Posted in Cool Feature | Leave a comment

Feature Spotlight: Python Debugger and ‘Attach to Process’

Happy Friday everyone,

Today I’d like to highlight some recent changes and improvements in PyCharm`s Python debugger and explore the brand new attach to process in more detail.

Previously PyCharm used a special fork of the PyDev debugger with some PyCharm-specific changes, as well as our own unique features like GEvent debugging and breakpoints at Django templates. Before releasing PyCharm 4, we decided to contribute to open-sourced software and merged our debugger code with that from the original PyDev repository. Now the work on the debugger continues from both sides (PyCharm and PyDev) in one place. The merged source code now lives at https://github.com/fabioz/PyDev.Debugger and pull requests are greatly appreciated!

As a result of the unification and the work done since PyCharm 3.4, the debugger in PyCharm 4 has grown more powerful. We’ve added the Referring Objects view, Jinja 2 templates debugging, and other small improvements. Still, the most notable debugger feature introduced in PyCharm 4 is attach to process.

Using this feature you can connect the PyCharm debugger with any running Python process and debug in the attached mode.

Note: ‘Attach to Process’ functionality is available in both PyCharm Community Edition and PyCharm Professional Edition starting with v4.0. This feature is supported on all Windows, Linux and Mac platforms.

Let me show you how it works.

Say we have some long running application that we want attach to. In my example I’ll use a simple graphical Minecraft-inspired application that I found on GitHub.

I have opened the project in PyCharm and launched it using standard run/debug configurations:


The project is running, so now I can attach the debugger to it. For that I go to Tools and select Attach to Process. PyCharm shows the list of running Python processes in our system. I select the Minecraft process and click OK:


PyCharm automatically shows the debugger console with the message that the debugger has successfully connected to the process:


From this point on, debugger works in the attached mode. Let’s make use of it and stop at some line of code. To do that, in the editor I put a breakpoint on the line with some reachable code. In my example I’ve put the breakpoint on the line containing the definition of the cube_vertices function, which is being called a lot. In a few seconds, once the process reaches the breakpoint, PyCharm will stop at the breakpoint and show us the debugger with frames, variables and watches views:


Now we can use the debugger as usual – stepping into/over, pausing and resuming the process, evaluating variables and expressions, and changing the runtime context:


That’s it! Hopefully you’ll appreciate this addition to the new joint-effort PyCharm debugger, along with other handy features introduced in the latest version of PyCharm.

And if you’re craving for yet more details on PyCharm debugger, see this tutorial or the online help.

See you next week,

Posted in Cool Feature | 4 Comments

‘Best Interactive Programming Course with Python’ Contest in Full Swing

A month ago we announced the launch of the educational contest for the Best Interactive Programming Course with Python. A number of participants have already joined, but with 3 more weeks to go until the end of the contest there’s still plenty of time to make your contribution. There are multiple prizes in several categories, so why not give it a shot?


Register your entry on the contest page today. Then, create an awesome interactive programming course, submit it before February 28th, and win valuable prizes!

The theme of the contest is PROGRAMMING EDUCATION WITH PYTHON. This is just a general guideline, so use your imagination to create your own unique course. You can build any course as long as it’s educational and involves programming with Python! The rules, possible course ideas, registration form, detailed instructions, tutorials and other helpful materials are all there on the contest page.

The process is pretty simple:

  1. Register on the contest page today. We won’t publish your entry details anywhere, but we will use them for two things: to send you a reminder a few days before the end of the contest, and to check-out your course from the registered repository.
  2. Create an interactive programming course with Python and PyCharm Educational Edition. Recommended topics, rules, instructions and tutorials can be found here.
  3. Place your course in the public Git repository you entered during registration.
  4. Contest ends 23:59, February 28th—that’s the final deadline. That’s when we’ll collect all courses from all repositories for assessment and scoring. The scoring criterias are described in the Rules section on the contest page.
  5. Finally, the winners will be announced March 5th!

That’s it! Dive in now and get your entries in before February 28th!

Best of luck, and most importantly, have fun!
- PyCharm Team

Posted in Conferences & Events | Leave a comment

Design a Python Programming Course and Win!

The JetBrains PyCharm team is thrilled today to announce the start of the Interactive Programming with Python educational contest!


This is a great chance for teachers and instructors all around the globe to show their experience and skills in creating a programming course that thousands of learners will use.
The theme of the contest is PROGRAMMING EDUCATION WITH PYTHON. This is just a general guideline, so use your imagination to create your own unique course. You can build a basic introductory Python programming course, a bioinformatics algorithms course, an advanced data mining course, or anything else—as long as it’s educational and involves programming with Python. Get your creative juices flowing, develop the best interactive course, and compete for valuable prizes!

First, register in the contest, either as a team or an individual. You’ll have about 2 months to develop your programming course and submit your entry. Entries will be judged on a range of criteria accessible to different educational approaches and topics, so don’t be afraid to dive in!
In addition to being a fun competition, this contest is meant to encourage people to start teaching and learning programming using Python and PyCharm. Anyone who feels like creating an exciting and useful interactive programming course can and should take part!
As a known supporter of different educational programs, JetBrains is sponsoring this contest with valuable prizes. For more details on the conditions, rules and prizes please see the contest webpage, where you can also submit your entry.

Best of luck, and have fun!

Posted in Conferences & Events | Tagged , | 1 Comment

Announcing the PyCharm 4.0.4 release update

As the first week of the New Year 2015 is almost gone, PyCharm team is excited to present the first PyCharm update of this year – PyCharm 4.0.4 build 139.1001.

This update contains a lot of improvements: IPython Notebook integration, the debugger, the embedded local terminal, Git and Subversion support, resolve of class references, Lettuce support, CSS support – all this have got our attention.

Also there is one small but handy feature. If you open a Python console tool window and would like to have more then one consoles there, now there is a special button just for that:


The full list of all the fixes and improvements is available in the release notes.

As usual, please report any problem you have to our issue tracker.

If you would like to discuss your experiences with PyCharm, we look forward to your feedback on our PyCharm Facebook page and twitter.

Develop with Pleasure!
-PyCharm team

Posted in Release Announcements | 6 Comments

PyCharm 4.0.4 RC is available

2014 has been a massive year for PyCharm, thanks both to the team and our amazing users whose valuable feedback and input always plays an instrumental role in the development process. And as the year draws to a close, we’re glad to give you the last build of 2014, the PyCharm 4.0.4 Release Candidate.

This update includes significant bug-fixes for IPython Notebook integration, the integrated debugger, the embedded local terminal, and VCS support (mainly for Git and Subversion). One more notable bug-fix is the unresolved attribute references of a class.

PyCharm 4.0.4 also introduces one small but handy feature: multiple Python consoles. If you open a Python console, there is a new icon to open an additional one:


For the full list of fixes and improvements, see the release notes.

Download PyСharm 4.0.4 RC for your platform from the project EAP page. Do report any bugs you may find, or any feature requests you may have, to our Issue Tracker. This build will also be available shortly as a patch update from within the IDE. To take advantage of that, make sure to select the Beta Releases channel in your update settings.

Wishing you a Happy New Year on behalf of the entire PyCharm team!
See you again next year!

Posted in Early Access Preview | 4 Comments

Take (or design!) Programming 101 with PyCharm Educational Edition 1.0.1

Today we’re making publicly available a new version of our free, easy and professional tool for learning programming with Python. Please welcome PyCharm Educational Edition 1.0.1!

If you haven’t checked this tool out, learn how it helps novice programmers with little or no previous programming experience to learn programming with Python easily and effectively.

A couple of months ago we announced the first version of PyCharm Educational Edition. Thousands of downloads and heaps of valuable feedback have poured in since then. We did our best to use this input and insight to improve the tool.

The most notable improvement in v1.0.1 is the Introduction to Python learning course, which is available out of the box right after you install the tool:


It’s pretty simple to go through the default learning course in PyCharm—just follow this tutorial or watch this short demo. Anyway, we’ve enhanced this introduction course to make it much more intelligent and provide many new smart interactive checks, hints and suggestions, and better task descriptions and texts:


Another useful improvement in PyCharm Educational Edition 1.0.1 is that now you can always interrupt the task check. This is useful if your code hangs for some reason. For example here is a solution that results in an infinite loop and hangs the task check:


Simply click the Cancel icon and try solving the task again:


When jumping between answer placeholders, now it’s possible to edit them right from the cursor position (whereas previously the whole placeholder would get selected):


If an error occurs while solving a task, now you can navigate to the place in the code where the error is found:


There are many other minor improvements in this release, so we encourage you to download and try it yourself! Unfortunately there will be no patch update from previous PyCharm Educational Edition v1.0 so please re-install PyCharm manually.

As a reminder, teachers, instructors and course authors can also use PyCharm Educational Educational Edition to create their own courses and share them with their students or with thousands users of PyCharm Educational Edition all around the globe. Read more about how this works and see the complete tutorial for course authors on how to create your own interactive course. Or, for a quick visual overview, watch this video:

Don’t wait any longer—download PyCharm Education Edition 1.0.1 for your platform and start learning (or teaching) Python programming today!

Other fun things you can do:

  • Spread the word about this tool
  • Follow us on twitter
  • Report bugs in our public issue tracker
  • If you ever need help, you’re always welcome to contact our professional support team
  • Read our blog to stay tuned for news, updates and everything that goes on with PyCharm Educational Edition. And do give us feedback on how we’re doing!

Develop with pleasure!
JetBrains PyCharm Team

Posted in Release Announcements | 7 Comments

Announcing the PyCharm 4.0.3 release update

Today we’re happy to announce that the PyCharm 4.0.3 bug-fix update has been uploaded and is now available from the download page. It also will be available in short time as a patch update from the previous versions of PyCharm 4.x.

This update includes the same set of major changes and fixes as the PyCharm 4.0.3 RC build. As a recap, some notable highlights of this release include:

  • a fix for a UI bug with find action
  • a fix for a remote debugger hanging
  • a fix for color settings in an embedded terminal
  • a test runner for Django nose tests is also fixed

For further details on the bug fixes and changes, please consult the Release Notes.

As usual, please report any problem you found in the issue tracker.

If you would like to discuss your experiences with PyCharm, we look forward to your feedback on our PyCharm Facebook page and twitter.

Develop with Pleasure!
-PyCharm team

Posted in Release Announcements | 3 Comments

Feature Spotlight: TextMate bundles in PyCharm

Happy Friday everyone,

Today I’m going to shed some light on how to get syntax highlighting for unsupported languages and file types in PyCharm.

As you probably know, PyCharm supports only a limited number of languages and technologies. PyCharm supports them with intelligence that appears in code completion, smart navigation, refactorings, inspections, and suggestions. But sometimes you might need to edit some project-specific files of an unsupported file type. In PyCharm you can open any file and edit it right away, but it always helpful to get at least syntax highlighting for it. Thanks to the TextMate bundles support you can get syntax highlighting for a wide range of languages.

Let’s see how to enable highlighting for an unsupported language in PyCharm. For example, in your project you might have some shell scripts – PyCharm doesn’t support the Shell language by default, however there is a TextMate bundle available for it:


Here is a few simple steps you take to enable the highlighting for the Shell language inside PyCharm:

1. First go to Settings (Preferences for Mac OS) and make sure that the “TextMate bundles support” plugin is installed and enabled:


2. Download a TextMate bundle. In my example I use the shellscript TextMate bundle from the official TextMate GitHub repo.
3. Open Settings again and go to Editor | TextMate Bundles:


4. Click “+” and locate the bundle on your filesystem. After that the bundle appears in the list of recognized bundles:


5. From this point you just need to click OK and enjoy the new syntax highlighting in the editor:


That’s it! However, lets hold for a while and consider some additional settings you might want to adjust.
The first thing you’ll probably want to change is a syntax coloring scheme. This can be done in Settings | Editior | Textmate Bundles:


The second thing you’ll probably need is to adjust file types associations. For that go to Settings | Editor | File Types and choose the “Files supported via TextMate bundles” option.
Here you can modify the list of registered filename patterns and the ones that are ignored:


Hope this post helps!
See you next week.


Posted in Cool Feature | 5 Comments