Using remote Python interpreters

The general term remote is used to denote anything that is not physically connected to your computer but can be connected and operated on. You might be familiar with the process of logging in to a remote server via the ssh command.

In PyCharm, it is possible to connect the work environment with a Python interpreter in a remote server. There might be many reasons why we would want to do this. For example, using the remote interpreter means that our local computer does not need to execute any command, thus saving us some computing resources. Moreover, since the interpreter is on a remote server, other programmers might have access to it as well. In other words, many people have the ability to share the same Python interpreter, which helps with ensuring reproducibility.

Obviously, in order to take advantage of this feature, you will need to have access to a remote server first. This can be your online computing server, such as AWS, from your school or company's server, or from a free remote server service. If your access is password protected, you will need to have your credentials ready as well.

Now, let's see how we can configure a remote Python interpreter within a specific PyCharm project:

  1. Go to Project | Project Interpreter from the settings. Here, we will then customize the Python interpreter to be used by clicking on the  icon in the top-right corner of the window and choosing Add, as indicated here:
Adding a new interpreter to your project
  1. Within the new window, select the SSH Interpreter option in the left-hand panel, enter the host address and your username in the main section, and hit Next:
Adding a remote interpreter
  1. In the next window, you might be prompted to enter your password for the remote server:

Logging in to a remote server

Alternatively, you can use your credentials in other tools, such as OpenSSH or PuTTY, to log in. After entering the password, click Next.

  1. Finally, in the next window, enter in the path to the Python interpreter on the remote server:
Locating the remote Python interpreter

You can also enable the checkbox below the prompt to run the interpreter using root privileges. Click Finish to finalize the process.

  1. Next, we need to set up a deployment process to automatically copy the code we have in our local project to the remote server. Normally, programmers have to facilitate this manually, but PyCharm allows us to streamline this process. From the settings, navigate to Build, Execution, Deployment | Deployment | Options, and check the Create empty directories box, illustrated as follows. This option ensures synchronization between our local and remote directories:

Setting up options
  1. Go back to Build, Execution, Deployment | Deployment in the settings. You will see in the window that we don't have a configured development for our remote server yet. To do that, click on the + button in the top-left corner and choose the SFTP option to add in a new server as follows:

Selecting file source type
  1. Next, enter a name you like for this server. We will also need to input our credentials for this connection in the following prompt:

Entering connection details

Note that most of the time, the option in the Authentication prompt should be either of the following:

  • Key pair (OpenSSH or PuTTY), in which case you will need to locate your private key path. Depending on your operating system and the SSH client you use, the path could be /Users/<your username>/.ssh/id_rsa (for macOS) or %APPDATA%SSHUserKeys (for Windows users).
  • OpenSSH config and authentication agent, in which case PyCharm itself will look for the appropriate authentication method.
  1. Another important aspect we need to consider is the Root path prompt in the preceding window, which is used to specify the home directory of our remote server. You can also use the corresponding Autodetect button to have PyCharm look for a suitable directory.
  2. Still in the same configuration window, click on the Mappings tab (highlighted here), which is used to specify the deployment path of our remote server:

Enter mappings

In particular, in the Deployment path prompt, enter the path to the directory that you want PyCharm to copy the files of your current project to (whose root directory can be configured with the Local path prompt). Note that this path will be relative to the remote root path we specified in the last step.

  1. Finally, confirm all of your configurations by clicking OK or Apply. Our last step in terms of setting things up for the remote server is to enable automatic uploads of our local files to the remote server in Tools | Deployment | Automatic Upload:

Setting automatic upload
  1. To have a graphical directory explorer for your remote server, go to Tools | Deployment | Browse Remote Host, which will open the Remote Host panel that you can use to inspect the structure of your remote server.

So that is the complete procedure of adding a Python interpreter from a remote server to our PyCharm workspace. As mentioned, this ability can prove useful in many situations, especially in group settings. PyCharm also offers a number of automation-related features, including working with macros, which we will discuss next.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.118.120.109