Twitter, Pinterest, HP, Symantec, Groupon. What do you think they have in common ?? I’m sure you know the answer. But apart from the fact that they are the biggest companies in the world IT market, there is a more specific detail: those of their employees, who program in Python, write their code in PyCharm development environment. The talented folks at JetBrains have developed one of the most used IDEs in the world for this programming language. In early 2019, JetBrains surveyed nearly 7,000 developers. 42% of those surveyed who write in Python chose PyCharm as the primary IDE for their work. PyCharm is cross-platform and compatible with Windows, Linux and MacOS. It supports both the second and third versions of Python and has a nice and functional UI, among other things. The tools and features provided by this integrated environment help Python programmers write code quickly and efficiently, sync with version control, use frameworks and plugins, while allowing the interface to be customized to the user’s liking and even allow for additional IDE extensions. PyCharm, of course, has a built-in code analyzer which really helps with code writing. Thanks to the analyzer you can navigate through the project very comfortably, use a quick search, and fix errors, which PyCharm kindly highlights and describes in detail. Autocompletion and code quality control systems are implemented in accordance with the PEP8 standard. It’s all about making your code prettier, cleaner and more structured. The JetBrains integrated environment supports major modern Python web development frameworks, you will be able to work with Jupyter-notebook and connect to Anaconda and other libraries for scientific computing and Data Science. But, as they say – “And that’s not all!” In addition to Python itself, PyCharm gets along great with other programming languages – JS, TypeScript, SQL or templating.
The official system requirements for the latest version of PyCharm are as follows: OS:
- Windows – Microsoft Windows 10 64-bit Windows 8 64-bit;
- macOS – macOS 10.13 or higher;
- Linux – GNOME or KDE environment.
RAM: at least 2 GB, but 8 GB is recommended; Disk space: installation will require 2.5 GB, SSD is recommended; Screen resolution: at least 1024×768 pixels; Python: Python 2.7, Python 3.5 or later; The developers draw the attention of macOS and Linux users to the fact that JBR 11 comes with PyCharm, and they do not need to install Java additionally. There is nothing special in the system requirements themselves, but we want to note that the IDE feels great on Windows 7, and the recommendations about 8 GB of RAM and SSD usage should be listened carefully, you won’t miss a chance ?.
How to install
The installation process is briefly described on the official website for each supported OS:
- On Windows
- Run the pycharm.exe file.
- Follow the instructions of the installation wizard.
- Choose any installation options.
On Mac OS
- First of all you should download pycharm.dmg file.
- Then you mount the disk in your system.
- And finally, you need to copy PyCharm to Applications.
- The pycharm.tar.gz file should be copied to the selected folder.
- Make sure that you have RW access rights for that directory.
- Unpack pycharm.tar.gz using the command: tar -xzf pycharm-2020.1.2.tar.gz
- Please note that you MUST NOT unpack the archive into a folder with an existing installation.
- You can now run pycharm.sh from the bin directory.
Getting Ready for Work and Setting Up
Before you start developing in PyCharm you need to create a project, because all the further manipulations will be done on its basis. To start a new project click New Project and give it a name. Now you can start setting it up.
Despite the fact that the developers from JetBrains began their journey in Russia, the interface language can’t be changed, so you will only be able to use the native language of William Shakespeare.
But you can change the fonts themselves and their size by following the path: File -> Settings -> editor -> font Change the color scheme of the UI: File -> Settings -> editor -> color scheme And configure many other aspects, like general settings, scroll configurations, color settings for each available language, and so on. There is an editor for all this: file -> settings -> editor
Setting up the interpreter
You can download the interpreter from the official Python website. If your plans do not include working with libraries that are only available in the second version of the language, it is worth downloading the latest release of Python 3. To use the installed interpreter, select it in the settings: File -> Settings -> Project:<name> -> Project Interpreter If PySharm doesn’t see the interpreter, try adding it manually. To do this:
- Click on the gear in the top right corner, choose “Add…”.
- Next select “System Interpreter”;
- Click on the 3 dots “…” to the right of the box in the interpreter selection;
- Specify the path to the interpreter.
Setting up a virtual environment
If your programming job requires you to work on multiple projects, it definitely doesn’t hurt to set up and connect a virtual environment. Venv is, roughly speaking, a directory containing a link to the interpreter and a set of installed libraries. The virtual environment “isolates” your projects, and helps in particular not to get confused by the different versions of libraries adapted to each of them separately. If you set up the virtual environment in advance, PyCharm will show a notification that suggests you use it in your project. If there is no notification, you can always configure and change it manually in the settings: File -> Settings -> Project:<name> -> Project Interpreter Also in this menu you can create virtual environments from scratch. To do so:
- Click on the gear in the top right corner, choose “Add…”.
- Select “Virual Enviroment” and set the parameters.
The Pipenv environment is created in the same way
Running scripts in the console
To run code in PyCharm you need to select an interpreter in the settings (I wrote about how to do it above). Also, before running your code in PyCharm you need to add configuration (for IDE to know which file to run and by what rules). Initially, if the project is new, there is no configuration and launch button is not active. To run the code in a new project, go to Run -> Run or do the combination: Alt + Shift + F10 A dialog box opens to select the file you want to run. After running, the terminal will open and you will see the result of your script and a message saying that the process completed with the code 0 (0 means successful completion). Next, when the configuration is created and saved, you can run your code for execution with the combination: Shift + F10
If the script doesn’t run, you may not have saved the configuration or PyCharm can’t see the interpreter. You can adjust or create a configuration in the “Edit Configurations…” menu, which is on the top right, near the “Run” button.
There is another way to run code:
- At the bottom of the program window, click on “Terminal”.
- Write python <name>.py or python3 <name>.py in the terminal.
For training purposes, you may need to quickly write and test Python instructions. PyCharm has an interactive mode for that (similar to IDLE). To invoke it, just click on “Python Console” at the bottom left.
Debugger in Pycharm
Debugging is extremely useful tool. You can use it to execute the code step by step. The interpreter switches to a special mode, recording the current state of the program at each stage of execution. That is, for each line of code, you will be presented with a mini report, in which you can see the current values of all variables and thus track the entire process of their change. Being in the hands of an experienced developer, the debugger reduces the time of searching for errors by orders of magnitude. To start debugging, it is necessary to set a so-called breakpoint. This is a red circle opposite a line of code. You can place it wherever you want, but you should most often place it where the interpreter has seen an error. To run the code in debug mode, click on the “bug” icon in the upper left corner: Or run the combination: Shift + F9 The debugging has started. Now you can press F8 to step through the lines of code in sequence and see the current state of the entire program. you can also “step” with F7, but in this case, the debugger will perform a “step-by-step”. I.e. when a function call is encountered, it will enter its description and sequentially walk through the instructions. All options for navigating in debugging mode can be seen in the “Debugger” window: There is a different hotkey for each button – just hover over the desired button to see it.
Deploy to a remote host
You can deploy and send code to the server directly from PyCharm.
The tool is not available in the free community version
To add a remote server, the first thing to do is to go to: Tools -> Deployment -> Configuration Here you first enter the name and then configure the server data. Then enter the user name and password and test the connection using the Test Connection button. Switch to the next tab called Mappings. Mappings here are the matches between the paths on your computer and the paths on the server. Make and apply the settings. Now you can make deployments: Tools -> Deployment -> Upload to <your_host_name> Tip: For your convenience, you can set up automatic deployment to the remote server after each “save”. To do this, go to Tools -> Deployment -> Options and select On explicit save action (Ctrl + S).
Macros, as well as many other things, are just there to make our lives easier. They allow you to automate a number of repetitive procedures that you most often perform while writing code. You can write, edit, and play macros, assign labels to them, and even share them. To create or customize your own macros, you need to select: Edit -> Macros -> Start Macro Recording After you have recorded your macro, click Stop Macro Recording.
Useful hints and tricks
How to change console color in pycharm
Changing the color of the console backgroud or fonts is very simple: you just need to go to the color scheme settings and change them as your internal designer wishes: File -> Settings -> Editor -> Color Scheme
Search through the whole project
- To search by code in the whole project, execute the combination Ctrl + Shift + F.
- To search for anything and everything in the whole project press the Shift key twice.
Hotkeys for starting and debugging
Use the Shift + F10 key combination to start your project, and Shift + F9 to start debugging it.
Comment out multiple lines at once
Commenting on code has never been easier: you select the lines you want with the mouse and then press Ctrl + /. Repeating this same action in the same place will uncomment the code.
- To indent – Tab.
- For multiple lines, select the lines you want and press Tab.
- To indent (indent to the left side) – Shift + Tab.
- Ctrl + Alt + L – autoformatting code on PEP 8.
- Ctr + Z – cancel the last performed action.
- Ctr+ Up/Down – scroll without changing the cursor position.
- Ctr + D – duplicate line.
- Tab / Shift + Tab – increase / decrease the indentation.
- Ctr + Space – displays autocomplete suggestions.
a cheat sheet on Pycharm hotkeys.
Top 7 useful plugins for Pycharm
The menu of plugins installation is located in: File -> Settings -> Plugins To install a plugin, just write its name in the search box, find the plugin and click “Install”.
List of useful plug-ins, which should be installed
- Highlight Bracket Pair(description) – plugin allows you to not get confused in the favorite activity of programmers – finding a match between opening and closing parentheses.
- Grep Console(description) – extension of the functionality of the standard console PyCharm – change of colors for different types of messages, tabs in the console, an analogue of grep for output, etc.
- Pylint(description) – Python linter. After installation, the “Pylint” menu will appear in the lower left corner. In order for the plugin to work you need to install the pylint module (python -m pip install pylint or python3 -m pip install pylint), and in the PyCharm settings specify the path to it (in Windows it usually lies in the folder C:<python_path>Scripts, and in Linux the path to pylint can be found with which pylint).
- DeepBugs for Python – looks for potential bugs and code quality issues using machine learning models.
Other useful plugins:
- Python Smart Execute(description) – the Alt + Shift + A combination copies selected code into a Python console and executes it.
- MyPy(description) is a type hinting plugin and source code analyzer for Python. After installation, the “MyPy” menu appears in the lower left corner.
- CodeGlance(description) – adds a code minimap in the editor window on the right.
What to do if PyCharm doesn’t see Python modules?
If you have such a problem, the first thing to do is to check the interpreter settings. Option #1 is to reset your settings and choose a new version of the interpreter. The settings can be found here: Settings -> Project:<project_name> -> Project Interpreter Option #2 – PyCharm searches for modules starting from the project root by default. Tell PyCharm where the directory with your modules is located. To do this, click on the directory (right-click) in Explorer on the left, and choose Mark Directory As -> Sources Root from the context menu.