hans

hans

【Others】How to use Pycharm to automatically synchronize local and remote server project code and remotely run programs


Purpose:

  1. Automatically synchronize code modified locally to the remote server.

  2. Click the run button locally, and actually run it on the server.

Steps:

  1. Install SSH locally and on the remote server, and set up the server user group.

  2. Create a synchronization path on the server.

  3. Create a Python virtual environment venv on the server and install the corresponding modules.

  4. 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.
    1668717689040.jpg

  5. 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.
    1668717695706.jpg

  6. 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.

  7. 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.

  8. 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.

  9. 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.
    1668717702662.jpg

1668717710204.jpg

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:

  1. 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.

1668717717749.jpg

Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.