Skip to content

Instantly share code, notes, and snippets.

@nhasbun
Forked from rszeto/*pycharm-setup-2020.3.2.md
Created July 20, 2020 23:05
Show Gist options
  • Save nhasbun/1b338d101e0ab248606cb25445941ee2 to your computer and use it in GitHub Desktop.
Save nhasbun/1b338d101e0ab248606cb25445941ee2 to your computer and use it in GitHub Desktop.
Setting up PyCharm project with remote interpreter

Setting up PyCharm project with remote interpreter

Setting up a remote interpreter on PyCharm is awfully unintuitive. I've pared it down to what I think is the minimal number of steps, and leaves the fewest number of deployment configurations and Python interpreters lying around. This is designed for my specific configuration (specifically PyTorch); adapt as needed.

Configuring default project (only do after installing PyCharm for the first time):

  1. (Optional) Add virtual environment path to excluded files
    1. From Welcome Page, go to Configure > Settings > Build, Execution, Deployment > Deployment > Options
    2. Add virtualenv path. For example, if you always have the project's virtualenv in .env, add ";.env" to the "Exclude items by name" field

Configuring a new project

  1. Create new project
    1. From Welcome Page, go to "Create New Project"
    2. Set local project location
    3. Expand "Project Interpreter", choose "Existing interpreter", and configure the interpreter
      1. Click the "..." next to the "Interpreter" field
      2. Choose "SSH Interpreter" in the left-hand menu
      3. Under "New server configuration", enter the Host, Port, and Username. Then, click "Next"
      4. Set either the Password or the Key pair information, then click "Next"
      5. Select the interpreter on the remote server (the python binary). Then, click "Finish"
    4. In the "Remote project location" field, enter the path to your existing Python project
    5. Click "Create"
  2. Update the deployment configuration
    1. Go to Tools > Deployment > Configuration...
    2. Rename the deployment server to match the current project
      1. In the left-hand menu, right-click the deployment server name and choose "Rename"
      2. Enter the project name, and then click "OK"
    3. Make the remote host window open at the remote project root
      1. Under the "Connection" tab, set "Root path" to the remote project root
      2. Under the "Mappings" tab, set "Deployment path" to "/"
    4. Click "OK"
  3. Exclude unnecessary files and directories
    1. Open the "Remote Host" panel on the right
    2. Navigate to the project root on the remote server
    3. Exclude folders/files to keep unsynced by right-clicking them and choosing "Exclude path"
  4. Download remote files to local project
    1. In the "Remote Host" panel on the right, right-click the project root and select "Sync With Local..."
    2. Click the "Synchronize All" button on the top
  5. Edit default Run/Debug configuration
    1. In the top menu, go to Run > Edit Configurations...
    2. In the left panel, go to Templates > Python
    3. Set environment variables
      1. Set DISPLAY=localhost:10.0
      2. (Optional) Exclude system environment variables

Setting up PyCharm project with remote interpreter

Setting up a remote interpreter on PyCharm is awfully unintuitive. I've pared it down to what I think is the minimal number of steps, and leaves the fewest number of deployment configurations and Python interpreters lying around. This is designed for my specific configuration (specifically PyTorch); adapt as needed.

Configuring default project (only do after installing PyCharm for the first time):

  1. (Optional) Add virtual environment path to excluded files
    1. From Welcome Page, go to Configure > Settings > Build, Execution, Deployment > Options
    2. Add virtualenv path. For example, if you always have the project's virtualenv in .env, add ";.env" to the "Exclude items by name" field

Configuring a new project

  1. Create new project
    1. From Welcome Page, go to "Create New Project"
    2. Set local project location
    3. Expand "Project Interpreter", choose "Existing interpreter", and configure the interpreter
      1. Click the "..." next to the "Interpreter" field
      2. Choose "SSH Interpreter" in the left-hand menu
      3. Under "New server configuration", enter the Host, Port, and Username. Then, click "Next"
      4. Set either the Password or the Key pair information, then click "Next"
      5. Select the interpreter on the remote server (the python binary). Then, click "Finish"
    4. In the "Remote project location" field, enter the path to your existing Python project
    5. Click "Create"
  2. Rename the deployment server to match the current project
    1. Go to Tools > Deployment > Configuration...
    2. In the left-hand menu, right-click the deployment server name and choose "Rename"
    3. Enter the project name, and then click "OK"
    4. Click "OK"
  3. Exclude unnecessary files and directories
    1. Open the "Remote Host" panel on the right
    2. Navigate to the project root on the remote server
    3. Exclude folders/files to keep unsynced by right-clicking them and choosing "Exclude path"
  4. Download remote files to local project
    1. In the "Remote Host" panel on the right, right-click the project root and select "Sync With Local..."
    2. Click the "Synchronize All" button on the top
  5. Edit default Run/Debug configuration
    1. In the top menu, go to Run > Edit Configurations...
    2. In the left panel, go to Defaults > Python
    3. Copy the following environment variables from the remote bash environment:
      1. PYTHONPATH (value from remote environment)
      2. DISPLAY (localhost:10.0)
      3. LD_LIBRARY_PATH (value from remote environment)
      4. LD_PRELOAD (value from remote environment)

Setting up PyCharm project with remote interpreter

Setting up a remote interpreter on PyCharm is awfully unintuitive. I've pared it down to what I think is the minimal number of steps, and leaves the fewest number of deployment configurations and Python interpreters lying around. This is designed for my specific configuration (specifically PyTorch); adapt as needed.

Configuring default project (only do after installing PyCharm for the first time):

  1. Add pyc files to excluded files
    1. From Welcome Page, go to Configure > Settings > Build, Execution, Deployment > Options
    2. Add ";*.pyc" to the "Exclude items by name" field
    3. (Optional) Add virtualenv path. For example, if you always have the project's virtualenv in .env, add ";.env" to the "Exclude items by name" field

Configuring a new project

  1. Create new deployment configuration
    1. From Welcome Page, go to Configure > Settings > Build, Execution, Deployment > Deployment
    2. Add deployment environment
      1. Click green "+" button
      2. Set config name to something useful (e.g. project name), and set type to SFTP
      3. Set SFTP host, port, user, password
      4. Set root path to the project path on the remote server
  2. Create new project
    1. From Welcome Page, go to "Create New Project"
    2. Set local project location
    3. Expand "Project Interpreter", choose "Existing interpreter", and configure the interpreter
      1. Click the gear to the right of "Interpreter", and choose "Add Remote..."
      2. Choose "Deployment configuration", and then select the deployment configuration created in Step 1
      3. Set the Python interpreter path to the path on the remote server. If using virtualenv, it should point to the virtualenv's Python binary.
    4. Set the remote project location to the project path on the remote server
  3. Set the deployment configuration for the project
    1. Go to Tools > Deployment > Configuration...
    2. Choose the deployment configuration from Step 1
    3. In the left panel, set the deployment configuration from Step 1 as the default via the right-click menu
    4. In the "Mappings" tab, put "/" as the deployment path
    5. Press "OK"
  4. Exclude unnecessary files and directories
    1. Open the "Remote Host" panel on the right
    2. Exclude folders/files to keep unsynced by right-clicking them and choosing "Exclude path"
  5. Download remote files to local project
    1. In the "Remote Host" panel on the right, right-click the project root and select "Sync With Local..."
  6. Edit default Run/Debug configuration
    1. In the top menu, go to Run > Edit Configurations...
    2. In the left panel, go to Defaults > Python
    3. Copy the following environment variables from the remote bash environment:
      1. PYTHONPATH (value from remote environment)
      2. DISPLAY (localhost:10.0)
      3. LD_LIBRARY_PATH (value from remote environment)
      4. LD_PRELOAD (value from remote environment)
  7. Disable plot visualization
    1. In the top menu, go to File > Settings > Tools > Python Scientific
    2. Uncheck "Show plots in toolwindow"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment