Feature Spotlight: Find Usages & Locate Duplicates in Python Code

Happy Friday everyone,

In the last week’s blog post I covered different refactoring capabilities of PyCharm. Today I’ll continue highlighting different handy features and show you some more tools that help you keep your code under full control.

One of the features that every developer uses on the daily basis is Find/Replace. It’s supported in every code editor, and PyCharm is no exception: Ctrl+F/Ctrl+R. Besides, PyCharm supports a number of subsidiary functions, such as Find Next/Previous Occurrence, Find/Replace in Path, Select all Occurrences, etc. They all are available under the Edit | Find menu item:

find1

While some of these features are pretty common, others really stand apart thanks to PyCharm outstanding code intelligence. Find usages is one of such noticeably smart features that deserves a closer look.

1. Find usages
Just press Alt + F7 on any symbol at the caret ( no matter if the symbol is a class, method, field, parameter, or another statement) and get a list of references grouped by type of usage, module and file. This feature is really fast and gets you first results almost instantly. More results appear as the IDE finds them:

find2

By default the results should be grouped by usage type, if not you can enable this by pressing Ctrl(Cmd for Mac) + Alt + T or by clicking the corresponding button on the sidebar:

find3

2. Settings
If you want to set custom options for the Find Usages algorithm, you can use Shift + Alt + Ctrl + 7 (Shift + Alt + Cmd + 7 for Mac):

find4

3. Quickpopup
To see the results quickly in place, simply press Alt + Ctrl + 7 (Alt + Cmd + 7 for Mac):

find5
4. Highlight overridden methods
Another useful aspect of highlighting usages in PyCharm is that you can easily find the methods that are overridden for a particular class. Just put the caret at the statement and press Shift + Ctrl + F7 (Shift + Cmd + F7 for Mac). If there are multiple classes, you will be asked whose methods to highlight:

find6
5. Highlight usages in File
Sometimes its useful to quickly see all usages of the variable or method within a file right in the editor. Highlight usages in File (Ctrl + Shift + F7) works perfect in this case:

find7

Locate Duplicates

It’s not a secret that one of the most annoying problem of every project is duplicated code. Obviously any developer tries to get rid of such redundancies. Thanks to PyCharm code intelligence, Locate duplicates tool allows you to examine your code and find different code duplicates. This tool works for different languages and is highly configurable.
To use this tool go to Code | Locate Duplicates. It will ask you for an analysis scope specification:

duplicates1

then you can specify some other options before give it a go:

duplicates2

You can set supported languages that will be analysed within your scope as well as other configurable options for fine tuning the analysis.

On the last step after clicking OK, it will show you the list of code duplicates found, as well as the tool to review these duplicates and navigate between them:

duplicates3

After finding duplicates you might want to use one of the refactoring features described in my previous blog post in order to get rid of them.

I hope I’ve been helpful and shed some light on these advanced PyCharm search features.

Have a great weekend and see you next week!
-Dmitry

Posted in Cool Feature | 4 Comments

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:

refactoring1

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:

refactoring2

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:

refactoring3

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”:

localhistory

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:

refactoring4

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!
-Dmitry

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:

navigation

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:

navigation2

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:

navigation3

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:

navigation4

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

navigation5

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:

navigation6

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:

navigation7

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:

navigation8

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:

navigation9

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):

navigation10

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:

navigation11

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!
-Dmitry

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:

1

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:

2

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

3

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:

4

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:

5

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,
Dmitry

Posted in Cool Feature | 2 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?

contest1

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!

pycharm_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:

multiple_consoles

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:

multiple_consoles

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:

welcome

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:

check

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:

hangs

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

hang2

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):

selections

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

navigate

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