Pycharm Configuring the remote Python interpreter and modifying the server code locally
In the recent process of learning machine learning, it is often necessary to upload locally written code to the GPU server and then run it on the server. The previous practice has been to write the code locally, and then through the FileZilla File Transfer tool to upload the written files to the server, and then through the SSH tool remote connection to the server, execute the corresponding Python script. This is a very cumbersome and inefficient approach. I heard a friend mention it today. Configuring the Remote Interpreter
Usage Scenarios
First of all to talk about their use of the scene, I was under what circumstances, I need to configure the IDE as such an environment to facilitate my work.
First of all, I need to write Python code on the local machine, but because it is a bit of code related to learning, sometimes the machine can run slowly, or it won't run down at all. At this point, I also have an available server, which is configured with the corresponding PYTHON3 environment and the corresponding machine learning library, such as Tensorflow,keras,tflearn and so on. Therefore, it is often necessary to upload locally written code to the server and run it on the server. Because of the ability of the Code slag, it is often necessary to repeatedly modify the code, and directly on the server to modify the code is not very convenient. Therefore, I want to operate as follows:
- I have a working directory on the server and I want to be able to modify the code file under the working directory directly on my local machine.
- Because Pycharm can configure the remote Python interpreter, I want to be able to run the modified code directly on the local machine and view the results of the run directly on the local machine without having to SSH to the remote server to execute the code.
- Specifically, I can write code directly on the local and then directly click on the Pycharm green triangle to execute the code, and this execution is performed by the Python interpreter of the remote server, not the Python environment configured on my local machine.
Configuring the Process native environment
Operating system: WIN10
Ide:pycharm
Remote server
Operating system: ubuntu14.04
SSH is configured and can be logged in remotely using SSH
Configure deployment
First, in the Pycharm menu bar, find: Tools > Deployment > Configuration.
Then you can see the following configuration page, specific configuration in the picture description:
Then open the Deployment Mapping tab:
In this way, the deployment configuration is complete. After this configuration is complete, it is actually equivalent to configuring an FTP tool to connect to the server so that you can view and modify the files on the server. You can open the corresponding RemoteHost panel through tools > Deplotment > Browse Remote Host, which shows the files on the server, The displayed range is the file and folder under the root path path that you configured under the Connection tab in deployment.
You can double-click a file directly in the RemoteHost panel and edit it directly. When you double-click a file, you can see that there is a horizontal bar at the top of the edit area, and there are three buttons on the right side of the bar, namely, compare, Undo, and upload. After you edit the file here, you can simply click the upload button and it will be submitted to the server. However, it is not recommended to modify the code directly here, the subsequent use of the process will be said.
The above configuration, you can directly in the Pycharm to see the files on the server, I feel like it is directly in the IDE integrated with an FTP transfer tool.
Configuring the remote Python Interpreter
The main point here is how to configure the remote Python interpreter.
First, open the Settings tab via File > Settings.
In the Settings tab, click on the "Project: Item Name" button, then click Project Interpreter in the expanded applet and the right will become the Interpreter configuration page.
Click the Small wheel button on the Interpreter Configuration page, and then select Add Remote.
Pop Up the popup window below and configure the appropriate information.
After the above steps, your remote interpreter is configured. At this point, you can simply click on the Small triangle button and invoke the Python interpreter on the remote server to run the code. But here, you'll also need to look at the following usage flow. If you double-click the file in the RemoteHost panel, and then click on the small triangle run will be an error, you may be prompted:
No such file or directory
Use process
A lot of other articles are also referenced in the configuration, but some of the articles are still not clearly detailed, so I'll keep a record of what I think might be the "right" process.
When we introduced the deployment configuration, we talked about the configuration of the Mapping tab. This configuration maps a local path to a path on the server, so files in this folder and files on the server are synchronized, and of course, you need to upload to the server or set up automatic upload when you modify it locally.
Here, my mapping configuration is as follows:
Local Path |
Deployment path on server |
E:\workspace\remotespace |
Xxxx/workspacexhh |
First, we pycharm through File > Open ... To open a new project, the project path selects the path of the local path above.
In the project window you can see that the item is open and the current project is empty and there is nothing in it.
Then we open the RemoteHost small window, right-click on the test.py file, and then select Download from here, and the file will be downloaded locally. Return to the Project small window and you'll see test.py. Similar to other files, if there is a parent directory, he will also download the parent directory, so the files and paths will be consistent with the server.
This allows you to edit the test.py file locally.
After editing, you can right-click on the file or right-click on the edit area and select Upload to ... Upload the file to the server so that you can update the server-side code and keep it consistent with the local code.
At this point, click the green triangle Run code, you can see the corresponding output. But looking at the console's output, you can see that the Python interpreter is not local, and the files you run are not local.
Above is the entire process of downloading files from the server locally, modifying the files locally, uploading the files to the server, and then running them directly locally.
In this process, we can also find that through this configuration, our code now has a total of three versions: one is the local code, one is the code on the server, there is a code in the RemoteHost panel (this place code if directly edited, can actually be submitted to the server, But cannot run directly)
So, if remote debugging is done through the above configuration, I think the approximate process should be the following:
- In the RemoteHost panel, select the code that you want to modify, and then right-click Download from here to download the content locally (this is the local folder that you set when you configured deployment).
- Modify your code locally (this is the local folder you set when configuring deployment), and right-click on the edit area or file name to select Upload to ... To submit to the server.
- After committing, you can run the local file directly like a normal call to the local interpreter (but the server file is actually running)
In this case, although the files in the remotehost can be edited directly, it is not recommended to do so, since editing here does not work directly.
Pycharm Configuring the remote Python interpreter and modifying the server code locally