Setting up a remote interpreter on Pycharm for Django
In this article I want to talk about and also show how to set up a useful remote development tool from JetBrains built into the Pycharm IDE. There is such a tool for a long time, but many developers do not take this seriously, and they prefer to deploy it locally to make changes to the project. When it is much easier on the same server, make a copy of part of the project and modify or test on another port using the built-in tools of the Pycharm remote interpreter. Of course, this is not a good option in all situations, but it’s very suitable for editing and finalizing small projects. And if the project is from scratch, then creating it on a remote server, the need to transfer and adapt it to the server (hosting) disappears, which inevitably leads to the appearance of many bugs and incompatibilities.
Moreover, this approach saves us from such problems:
• on different servers, your Unix and your jokes, deploying locally and adjusting the environment to the characteristics of a server can take a decent amount of time;
• different versions of python;
• And if all dependencies are not specified in the req.txt file for Django;
Pycharm Remote Interpreter Setup Process for Django
We go to file-> settings-> deployment.
Enter the server connection settings, who has what (SFTP, FTP, etc.). In the “root path” field - you need to specify the path to the project folder. In the tab "Mapping" in the field "Deployment path on server" you must specify the path from the "root path" to the folder with the project.
“root path” = / data / python
“Deployment path on server” = / project
There are options for deployment, it is useful to set uploading files to the server using hot keys, for example, ctrl + s and other settings.
The preparation is finished, we proceed accordingly to the settings of the remote interpreter itself.
The interpreter setting is not very different from the deployment setting (you can fill in part of the settings with the “Fill from deployment server settings” button). Click "Configure Interpreters", select remote, or if some have already been created, then using the plus sign. Next, you need to specify the path to the python interpreter on the server. After that click “ok” and wait a little while Pycharm downloads (creates) an image of skeleton python in a place with applications that were installed via pip or something similar, which is very convenient, later you can safely go to these files from the code!
The next step is to configure the launch of the Django server using Pycharm development tools.
In the tools with the right, select "Edit configuration"
In the window that appears, enter “host” 0.0.0.0 in order to receive calls from any ip and “port” which is free on your server. Select the remote interpreter in the "Python interpreter" field that you configured earlier. In the "Environment variables" field, you must specify the variable "DJANGO_SETTINGS_MODULE" and its value, consisting of the name of the main Django application and the file with the settings in it.
This completes the configuration of the remote interpreter; to start it, you can use the button on the toolbar from the right.
After, a server log window with all kinds of tools will appear from the bottom.
It is worth noting that in addition to remote debugging, Pycharm has built in very useful tools (menu-> tools): SSH client, Django-console, Python-console, Debug, and others.
In the screenshot below, the Django console and the SSH client are in split mode.
The development (debugging) method that was presented above is essentially an attempt to reach a new level. Deploying development on a locale is like a patented proven method, but the one described above is a new approach, and no one guarantees its support, although JetBrains try, but as you know, the first ones always get bumps. Good luck.
Also, when setting up a remote interpreter, you can use virtualenv, where when developing or finalizing a project, you can conduct completely independent tests and not affect the server, but this is a separate topic.