PyCharm is an IDE exclusive for Python development, and one of the most popular among Python developers.
It is developed by Czech company JetBrains, that released the first version in 2010.
Take into account that PyCharm uses virtualized environments.
Contents on this post:
- Explaining PyCharm
- Setting up the PyCharm Application
- Setting up the PyCharm Project
Explaining PyCharm
This section provides some explanations to understand better how PyCharm works.
PyCharm Editions
There are different editions of PyCharm, some of them free and some others paid.
PyCharm Community Edition, colloquially known as PyCharm CE, is the free and open source (FOSS) version of the product. Its functionality is limited compared to paid version.
PyCharm Professional is the closed source, paid and advanced version of the product.
Virtual Environments on PyCharm
One of the main differences between PyCharm and other IDEs or text editors is that by default PyCharm sets virtual environment for each of the projects.
Using virtual environments bring some advantages, as for example, you add security when you import a project from an unknown source and run it on your local computer. It also allows to run projects on different configurations within the same computer very easily. However, if you are unfamiliar with virtual environments, this may lead you to find unexpected behaviours.
This isolation between the project and your system provokes that resources you have installed in your system are not visible from the project. Instead, you need to specify the resources to be used on each project individually.
For example, you may have installed modules in your system using the pip command-line tool. However, these modules will not be visible inside the project, as they are run in an isolated virtual environment.
The same happens with python3 command-line utility. You may have installed version 3.11 on your computer, but you may be running 3.9 on your program.
Ont this post you can find how to set up the interpreter on your projects.
Setting up the PyCharm Application
There are different settings that are recommended to be set up on PyCharm application. This configuration affects all projects.
How to set the maximum Characters per Line in Code Editor
Default line character in PyCharm is 120 characters.
Traditional terminal screen displayed a maximum of 79 characters per line, and PEP 8 style guide recommends to limit the maximum characters per line to this, so it may be a good idea to set this limit to our Python code.
Go to menu bar > File/PyCharm > Preferences >> Editor > Code Style > Python > “Wrapping and Braces” tab.
“Hard wrap at” field sets the maximum characters per line. Default value in PyCharm is 120, but you can change it to 79.
If you check the value “Wrap on typing” to “Yes”, when you write more characters in a line than the one set on “Hard wrap at” it will keep typing on the next line automatically.
“Wrap on typing” is set to “No” as default, so we will just see a vertical line and in case we type more characters nothing will happen.
There is also a field called “Visual guides” that allows us to draw one or many vertical lines in a lighter colour than the “Wrap on typing”. You must enter the number of the character after you want to draw the vertical line. If you enter multiple values, they must be separated by a comma.
It is blank by default.
Take into account that some software add extra characters to line with comments, so it is recommended to limit them to 72 characters per line.
A practice aligned with PEP 8 would be:
- Setting “Hard wrap at” to 79 characters
- Setting “Visual guide” to 72 characters
How to write Spaces instead of Tabs in Code Editor
According to Python style guide, a tab could be equivalent to 4 spaces.
Many developers recommend to use spaces instead of tabs. By default, PyCharm is configured in this way.
If you want to change tab configuration, go to menu bar > File > Settings >> Editor > Code Style > Python > “Tabs and Indents” tab.
How to add Headers to Projects
You may want to add headers to each file in a project, for example, if you want to add the license to each file.
You can use the “File template” feature offered by PyCharm to specify a header for each file.
You can read more on this external link.
How to integrate GitHub on PyCharm
You can find detailed information about how to integrate GitHub on PyCharm on this post.
Configuring Projects on PyCharm
This section explains how to configure individual projects on PyCharm.
How to add Headers to Projects
You may want to add headers to each file in a project, for example, if you want to add the license to each file.
You can use the “File template” feature offered by PyCharm to specify a header for each file.
You can read more on this external link.
How to configure the Interpreter on PyCharm
Go to menu bar > PyCharm/File > Preferences >> Project:YourProject > Python Interpreter.
You can check the Python Interpreter available. Next to the label “Python Interpreter” there is a drop-down list. Expand it and click on “Show All…” to see all installed interpreters. In my case, the list was empty.
Further to the right there is the “Add interpreter” > “Add Local Interpreter…”. On the left hand side, you will find a list of environments and interpreters. As default, “Virtualenv Environment” is selected.
Under the “Base interpreter” drop-down list you can select a specific interpreter version.
Wait a little to create your virtual environment.
After this, you will see that “Python Interpreter” drop-down list now has a value, and that some packages (like pip, setuptools or wheel) have been imported.
Click “OK”.
How to import Packages on PyCharm
By default, PyCharm uses virtual environments for each project. If you w
PyCharm Shortcuts
Below you can find some useful shortcuts for PyCharm.
Indenting and Unindenting Code Blocks
To indent a code block, make a selection and press Tab.
To unindent a code block, make a selection and press Ctrl+Tab.
Commenting out Blocks of Code
To comment out a block of code, select the code and press Ctrl + /.
You can uncomment it by pressing the same shortcut again.
Moving Lines up or down
To move a line or groups of lines up or down, place the cursor over the line or make a selection and Ctrl+Shift+Up arrow or Down arrow.
Hiding the Project File Explorer
To hide the project file explorer, press Shift+Escape.
You can show it again by clicking “Project” on the left hand side with the mouse.
Finding Additional Shortcuts
You can read more about PyCharm shortcuts on this external link.