Way back when, in September 2020, I wrote a blog about how to configure PyCharm for use with ArcGIS Pro (and ArcMap); if you are interested in it then you can read it here. It is the main developer environment I use when I am writing my geoprocessing scripts using ArcPy because it has some great debugging functionality (unlike some applications….) and because it allows you to work with multiple Python environments.
Within the course people often ask a number of PyCharm related questions which usually begin with… “How do I do…?“. So in a bid to collate a few answers to these questions into an accessible location (and mostly to act as an aide for my sieve-like memory) I’ve written this small article which I hope you will find useful…..
I’m going to cover the following things:
- Stop PyCharm from opening the previous project automatically on start-up.
- Change the background colour in the script window.
- Bulk comment and un-comment your code.
- Indent and un-indent your code.
- Obtain documentation for a function or a method.
I think that’s plenty to start with so let’s look at the first tip…
Stop PyCharm from opening the last project on start-up
If you have been working in PyCharm and you close the project down then, by default, the same project will open up automatically the next time you start PyCharm – you are not given the opportunity to choose a different project.
How annoying!
It’s quite simple to change this default behaviour in PyCharm.
All you have to do is:
- From the File menu > Settings.
- Under Appearance & Behaviour, click System Settings.
- In the Project group notice the Reopen projects on startup checkbox is ticked.
- Untick the checkbox!
- Press the OK button to commit the change you have made on the Settings dialog.
Hopefully the next time you open PyCharm you will then be given the choice as to which project you wish to open, as opposed to open the last project you worked on automatically.
Change the background in the script window
One of the things I always mention to course attendees is how to change the background display of the script window in PyCharm. Annoyingly the background is black which means you can’t see your mouse cursor very easily.
This is because PyCharm’s default colour scheme is called “Darcula”.
It is pretty easy to change, once you know how. Once again, the change can be made within the Appearance & Behavior tab of the Settings dialog box:
- From the File menu > Settings > Appearance & Behavior.
- Click the Appearance tab.
Notice that the Theme is set to Darcula.
- Click the drop down to choose a suitable theme.
You might want to have a look at the Windows 10 Light or the IntelliJ Light themes.
If you would like a few more themes with the ability to have greater control over the colours within the application then you should look at the Color Scheme section of the Settings dialog; so:
- File menu > Settings > Editor > Color Scheme > General
The Schemes area provides a number of additional colour schemes with greater control on the parts of the application to which the colour schemes can be applied.
Fill your boots.
Bulk comment and uncomment your code
Many developer environments provide you with the ability to select large swathes of code and through a button either comment or uncomment your code.
PyCharm is ever-so slightly different.
As you are probably aware PyCharm has a very “menu-driven” user interface. There are many menus (11 at the last count) and so finding the correct menu containing the correct item can be a little frustrating.
To comment your code, you can:
- Select the code you wish to comment.
- Press the <Ctrl> key and then the “/” key to comment your code.
- To uncomment your code you perform exactly the same operation.
The equivalent menu item to comment / uncomment your code is found through the Code menu > Comment with Line Comment.
Indent and unindent your code
Every time you add error handling to a script, or you work with a compound statement of some description, then you will need to indent your code.
Indenting your code is pretty straight-forward. All you have to do is:
- Select your code.
- Press the <tab> key.
Your selected code is then indented by one tab width – you probably knew that anyway.
But unindenting your code is a little less obvious. To unindent your code you should carry out the following:
- Highlight your code.
- Press the <shift> and <tab> keys at the same time.
And your code is then unindented.
These indentation options are also available on the Edit menu.
Display a list of valid parameters for a function or a method
The final thing we will look at is to obtain some floating help for a function or a method.
To display the documentation you should:
- Place your mouse cursor between the brackets of the function / method.
- If the documentation does not immediately appear then give PyCharm a nudge by pressing <Ctrl> and P at the same time.
So there are my top 5 tips for working with PyCharm. If you have your own comments then please drop them into the comments section below so other people can enjoy your time saving techniques.