PyCharm 2024.1 Help

Tutorial: Deployment in PyCharm

This tutorial aims to take you step-by-step through configuring and managing deployment of your code to remote hosts using PyCharm.

Before you start

Make sure that:

  • You are working with PyCharm version 5.0 or later. This tutorial is prepared with PyCharm 2016.1.

  • You have access right to a remote host you want your code to be deployed on.

Also note that this tutorial is created on Windows 10 and makes use of the default keyboard shortcuts scheme. If you are working on a different platform, or use another keyboard scheme, the keyboard shortcuts will be different.

Preparing an example

You can use the everlasting sample project.

Thus the preliminary steps are done, and we are ready to take off.

Configure the deployment server

Add a new server

  1. Press Ctrl+Alt+S to open the IDE settings and then select Build, Execution, Deployment | Deployment.

    Alternatively, go to Tools | Deployment | Configuration... in the main menu.

  2. Click Add item and select the type of the server you want to create. In our case, it is Local or mounted folder.

    Add server dialog
  3. In the Create New Server dialog that opens, type the server name and click OK.

The new server is added, but it only shows the Web server URL http://localhost where you will actually browse your uploaded files.

Specify the server root folder

  1. In the Folder field, specify the directory to which the project files will be uploaded.

    In our case, this is the C:\xampp\htdocs local folder. You can either type the path manually or press Shift+Enter.

  2. Disable the Visible only for this project option to be able to export this configuration later.

    Deployment Connection Tab

Specify the deployment path

  1. Next, switch to the Mappings tab.

    By default, the Local path field contains the path to the project root. However, you can select any other directory within your project tree. Let's use the default path.

  2. In the Deployment path field (which is by default empty), specify the folder on your server where PyCharm will upload data from the folder specified in the Local path field.

    In this example, it's py\Solver. This path is specified relative to the web server root folder, which is C:\xampp\htdocs.

  3. Leave the default / value for Web path.

    Deployment Mapping Tab

After you apply the changes, the server is ready to use.

Browse remote hosts

To make sure your server is up and running, select Tools | Deployment | Browse Remote Host in the main menu, and the Remote Host tool window appears on the right of the PyCharm window:

Remote Hosts tool window

Deploy files

Upload a file to the server

First, let's upload one of the files to the remote server.

  1. In the Project tool window (Alt+1), right-click the file you want to upload. In our case, this is the Solver.py file.

  2. From the context menu, select Deployment | Upload to MyRemoteServer, confirm the upload, and see the upload results.

    Deployment upload

You can also upload the contents of each directory within your project. For example, right-click the parent directory of Solver.py and select Deployment | Upload to MyRemoteServer from the context menu. We now have the entire directory uploaded to the server.

Deployment File Transfer

Compare remote and local versions

There is a local and a remote copy of the Solver.py file, and they are identical. Modify the local version.

  1. Place the caret at the method declaration and press Ctrl+Alt+Shift+T or go to Refactor | Refactor This in the main menu.

    The popup shows all refactorings available in the current context. Select the Rename refactoring, and rename a method.

    Rename method
  2. In the Remote Host tool window tool window, right-click Solver.py and select Compare with Local Version. PyCharm opens the Diff Viewer.

    Diff viewer

Download the file from the server

  1. In the Remote Host tool window tool window, right-click the Solver.py file and select Download from here. PyCharm shows a warning.

    Deployment download warning
  2. Click Yes.

    Deployment download file transfer

    You can also download an entire directory if it has been previously uploaded to the server. For example, if you click the parent src directory and choose the same command, all nested files will be downloaded from the server.

Synchronize the changes

  1. Undo the changes in the Solver.py file by pressing Ctrl+Z. You again see the class Solver.py with the renamed method.

  2. Click the Solver.py file and from the main menu, select Tools | Deployment | Sync with Deployed to MyRemoteServer.

    PyCharm shows the Diff Viewer where you can accept individual changes or reject them.

    Deployment sync

Automatically upload files to the default server

When you need to have the exact same files on the server as in a PyCharm project, automatic upload can be helpful. Automatic upload means that whenever a change occurs in the IDE, it will be deployed to the default deployment server.

A deployment server is considered default if its settings apply by default during automatic upload of changed files.

Define the default server

  1. Press Ctrl+Alt+S to open the IDE settings and then select Build, Execution, Deployment | Deployment.

    Alternatively, go to Tools | Deployment | Configuration... in the main menu.

  2. From the list of servers, select the one you want to make default and click the Use as default button on the toolbar above the list of servers.

Enable automatic upload

As soon as the default server is set, you can make upload to this server automatic.

  1. Press Ctrl+Alt+S to open the IDE settings and then select Deployment | Options.

    Alternatively, go to Tools | Deployment | Options in the main menu.

  2. From the Upload changed files automatically to the default server drop-down list, select one of the following options:

    • Always: upload a file upon every automatic and explicit save.

    • On explicit save action: upload a file after save only if this save was invoked manually by choosing File | Save All or pressing Ctrl+S.

The Always mode is not recommended for deploying to production: incomplete code can be uploaded while developing, potentially breaking the production application.

Upload external changes

By default, PyCharm also uploads the files if they are changed by some external process, such as a VCS branch change, compilation of SASS or LESS, or a File Watcher. To change this behavior and skip external changes, set Upload changed files to the default server to Always and select the Skip external changes checkbox:

Skip external changes

Export/import deployment settings

Now that you have a deployment server all set up, you can export the server configuration settings and then import them on another computer.

Export settings to a ZIP archive

  1. Select File | Manage IDE Settings | Export Settings from the main menu.

  2. In the Export Settings dialog that opens, make sure that the WebServers checkbox is selected and specify the path to the target archive.

    Export the server deployment settings

Import settings from a ZIP archive

  1. Select File | Manage IDE Settings | Import Settings from the main menu.

  2. Select the ZIP archive that contains your settings in the dialog that opens.

  3. Select the settings you want to apply in the Select Components to Import dialog that opens and click OK.

Summary

You've passed this very basic tutorial and performed the following tasks:

  • Created and configured a server of your own.

  • Uploaded and downloaded files and folders.

  • Compared local and remote versions.

  • Configured the server as default.

  • Enabled automatic upload of external changes.

Last modified: 18 April 2024