Do you often develop Python or web applications on Windows 7 or MAC OS X, but they end up running on linux? We often encounter no problems during development but there are problems in the official Linux environment. So how to ensure that the development environment is consistent with the operating environment?

There are usually two ways to solve it. One is to use the built-in support of PyCharm Vagrant, this tutorial can refer to the Vagrant development environment configuration . But unfortunately, I always reported an error when I started the VirtualBox virtual machine during the test process, and it has not been solved yet. Readers can try it to see if it works. The second way is to use PyCharm's remote interpreter plus the file synchronization function to achieve local editing code -> synchronization to the server -> debugging the test program through remote debug. Currently I choose the second one, although it is a bit more clumsy than the first one.

The remote debugging function is supported in large IDEs such as Eclipse and IntelliJ IDEA, and the implementation principles are basically the same. Here, PyCharm is used to explain.

Synchronization configuration of remote server

The IP address of the remote server is 192.168.203.95, the ssh service is enabled, and python version 2.7 is installed. I use a core-python project in PyCharm for demonstration.

First we need to configure the code synchronization of the PyCharm server, open Tools | Deployment | Configuration

Click the "+" on the left to add a deployment configuration, enter the name, and select SFTP as the type

After confirmation, configure the ip, port, username and password of the remote server. The root path is the root directory of the file upload. Note that this directory must have permission to create files.

Then configure the mapping, local path is your project directory, you need to synchronize the local directory to the server, I filled in the project root directory. Deploy path on server Fill in the directory relative to the root path here, the following web path does not matter first

If you still have some files or folders that you don't want to synchronize, you can add them in "Excluded path" on the third tab page of the configuration dialog, and you can specify both local and remote.

There is also a setting, open Tools | Deployment | Options, tick "Create Empty directories", if the specified folder does not exist, it will be created automatically.

Upload and download files

There are several ways to achieve synchronization of local and remote files, both manually and automatically when the file is saved. Here I choose manual, because automatic triggering affects performance, for example, PyCharm will be stuck, and I feel uncomfortable.

The manual upload method is very simple, select the file or folder to be synchronized, and then select Tools | Deployment | Upload to sftp (this is the deployment name just configured)

The same is true for downloading files, select Tools | Deployment | Download from sftp

Compare remote and local files

Sometimes you are not sure that the remote and local versions are exactly the same and need to compare. PyCharm provides contrast views to solve this problem for you.

Select Tools | Deployment | Browse Remote Host, open the remote file view, and you can see the files in the remote host in the right window

Select a folder you want to compare, right-click -> Sync with Local, open the synchronization comparison window, and use the left and right arrows to synchronize the content.

The above is the comparison between the server and the local, then the local file is compared with the server, first select the file or folder in PyCharm, then right-click->Deployment->Sync with deployed to

PyCharm remote debugging

There are two options for remote debugging in PyCharm:

  1. Use a remote interpreter
  2. Debug the server with Python

For the sake of simplicity, I will only demonstrate the first one, using a remote interpreter, that is, using the python interpreter installed on the server.

Configure the remote Python interpreter

Select File | Settings, select Project | Project Interpreter, then on the right, click the little gear setting, as follows

Then click "Add Remote" and fill in the host's ssh configuration

If you have configured SFTP before, you can directly select "Deployment configuration", and then select the template name just now. Since I have configured it above, I will directly select the template. Please carefully check that my Python interpreter is a virtual environment virtualenv. This requires Create a virtual environment on the server first.

start debugging

After completion, select the remote interpreter as the project's interpreter, and then configure a running instance to break point debugging. Here I take another django project as an example, the name is zspace, because it is more representative to use a web project to illustrate.

Select "Run/Debug Configuration", add a "Django server", and then configure it like the following . Please pay attention to the points marked in the figure. You don't need to explain what it means, ^_^

Then you can debug with breakpoints just like local debugging. This step is too simple to take screenshots. Remember to modify the source code and synchronize it to the server to continue the next debugging.