Purpose:
-
Automatically synchronize code modified locally to the remote server.
-
Click the run button locally, and actually run it on the server.
Steps:
-
Install SSH locally and on the remote server, and set up the server user group.
-
Create a synchronization path on the server.
-
Create a Python virtual environment venv on the server and install the corresponding modules.
-
Set up the automatic synchronization function for the project. Open Pycharm -> file -> Settings -> Build, Execution, Deployment -> Deployment, and add relevant information for the remote server.
-
Click on the Mappings in the above image, and set the local and remote server paths that need to be synchronized. Multiple paths can be set to synchronize multiple projects.
-
Finally, click the checkmark, so that the name of the setting you just added will be displayed in bold font. Under Deployment, there is an Options tab, which contains detailed settings for project synchronization. You can study it yourself. Note that only the server with the checkmark and bold name will automatically synchronize the code. In other words, you can only set one server to automatically synchronize the code at a time. However, you can manually synchronize with other servers. In step 8 below, select upload to ..., and then select the other server you want to upload to.
-
Next, set up the remote running function. Go to: file -> Settings -> Project: PycharmProjects -> Project Interpreter, click the dropdown button, click Show All, click the add button, select SSH Interpreter, select Existing server configuration, and select the server you just added from the dropdown menu. Next, make sure to select the virtual Python path you installed as the Interpreter path, and the Sync folders below are the project paths you set to synchronize. Finally, click Finish to complete the setup.
-
The first time you use it, you need to upload the local content to the server once. Right-click on the project you want to synchronize in the Project Files on the left, and select upload to xxxx (server name) in the Deployment section. After that, the code will be automatically synchronized when you press Ctrl+S. If you want to synchronize a large number of data files, it is recommended to use FileZilla in conjunction with Pycharm.
-
Sometimes you may want to run the code locally. You can simply select the local Python compiler in the Python interpreter on the page shown in the image below.
These are the ones I can think of for now. If you have any questions, please leave a comment.
Updated on August 16, 2021: A user asked how to synchronize the deletion of remote server files when the local files are deleted:
- As shown in the figure below, open the Deployment triangle on the left in the settings, click Options, and then check the options "Delete target items when..." and "Delete remote files when...". You can choose whether you want to always synchronize automatically or only when pressing Ctrl+S.