Remote development using PyCharm

My go-to IDE for Python development is PyCharm and I’m fortunate enough to use the Professional Edition. A very useful feature I discovered lately is the ability to do remote development in it. Please note that this feature only works in the professional edition.

The Customer Portal I’m working on for Bell34 require access to a server that has a database that stores all the information we need access to. Because of the way the Portal is setup, I don’t have access to the database on my local machine. The only way to access the data is to access it directly on the remote server that houses it. This wasn’t a big problem for me until I started writing integration tests that required access to the database. To run the tests, I needed to run them directly on the server. This meant that I had to find a way to put my test code on the server.

There are a number of ways to go around this problem. One way is to work on the code locally, push it to version control (git/Github in our case), connect to the server via ssh, pull the changes into the server and then run the code from there. A second option is to use tools like scp or rsync to sync the local copy of the project to the remote server.

Both options work but they are tedious and error prone(in my view). Fortunately, PyCharm supports remote development in its Professional Edition. After some setup, it allows you to develop just as you would on a local machine. In this article, I’ll show you how to set it up to connect to a remote server over ssh.

Step 1: Create a Deployment Config

Go to Tools > Deployment > Configuration Click the green “+” symbol to add a new configuration. In the window that pops up, give your configuration a name and set the “Type” to SFTP(Secure FTP):

pycharm add server screen

2. Create a connection

Next, you’ll want to setup connection details in the Connection settings tab:

Pycharm connection setting

Type in your hostname(or IP address) and login details. The root path is a default path you’ll be navigated to once the connection is established like the home folder for example. Check the “Save password” box if you would like to have PyCharm save your login information.

3. Map local and remote folders

After setting up a connection to the remote server, you’ll want to map your local project’s path to the correct path on the remote server in the “Mappings” tab. If you don’t have the path memorised, PyCharm can help you browse and autofill the remote path for you if you click on the three little dots next to the “Remote Server” path textbox.

Pycharm deployment settings mapping tab

If there are any files or paths you would like to exclude from the download and upload process, you can specify these in the “Excluded Paths” tab.

4. Setup Automatic uploads

Pycharm setting for automatic deployment

The step above completes the setup you need to do to get the sync to work. The next step is to tell PyCharm to automatically upload any changes you make locally to the remote server. Navigate to Tools > Deployment > and select the Automatic Upload checkbox:

That’s it! Now, whenever you make a change on the local file, your changes are uploaded to the remote server. In addition to this, PyCharm allows you to access the remote servers’ terminal with a single click:

Fire up a new SSH session

SSH session with remote computer

Notice in the picture above that there are two tabs, one for my local terminal and a second one showing the remote terminal. From here, you’re able to build, debug and run tests on the remote server without leaving PyCharm.