Help Center/ ModelArts/ DevEnviron/ Local IDE/ Local IDE (PyCharm)/ Manually Connecting to a Notebook Instance Through PyCharm
Updated on 2024-08-14 GMT+08:00

Manually Connecting to a Notebook Instance Through PyCharm

A local IDE supports PyCharm and VS Code. You can use PyCharm or VS Code to remotely connect the local IDE to the target notebook instance on ModelArts for running and debugging code.

This section describes how to use PyCharm to access a notebook instance.

Prerequisites

  • PyCharm professional 2019.2 or later has been installed locally. The PyCharm professional edition is available because remote SSH applies only to the professional edition.
  • A notebook instance has been created with remote SSH enabled. Ensure that the instance is running. For details, see Creating a Notebook Instance.
  • The address and port number of the development environment are available. To obtain this information, go to the notebook instance details page.
    Figure 1 Instance details page
  • The key pair is available.

    A key pair is automatically downloaded after you create it. Securely store your key pair. If an existing key pair is lost, create a new one.

Step 1 Configure SSH

  1. In your local PyCharm development environment, choose File > Settings > Tools > SSH Configurations and click + to add an SSH configuration.
    • Host: address for accessing the cloud development environment. Obtain the address on the page providing detailed information of the target notebook instance .
    • Port: port number for accessing the cloud development environment. Obtain the port number on the page providing detailed information of the target notebook instance.
    • User name: consistently set to ma-user.
    • Authentication type: key pair
    • Private key file: locally stored private key file of the cloud development environment. It is the key pair file automatically downloaded when you created the notebook instance.
  2. Click to rename the connection. Then, click OK.
  3. After the configuration is complete, click Test Connection to test the connectivity.
  4. Select Yes. If "Successfully connected" is displayed, the network is accessible. Then, click OK.
  5. Click OK at the bottom to save the configuration.
Figure 2 Configuring SSH

Step 2 Obtain the Path to the Virtual Environment Built in the Development Environment

  1. Choose Tools > Start SSH Session to access the cloud development environment.
  2. Run the following command to view the Python virtual environments built in the current environment in the README file in /home/ma-user/:
    cat /home/ma-user/README
  3. Run the source command to switch to a specific Python environment.
  4. Run which python to obtain the Python path and copy it for configuring the Python interpreter on the cloud.
Figure 3 Obtaining the path to the virtual environment built in the development environment

Step 3 Configure a Python Interpreter

  1. Choose File > Settings > Project: Python project > Python Interpreter. Then, click and Add to add an interpreter.
  2. Select Existing server configuration, choose the SSH configuration from the drop-down list, and click Next.
  3. Configure the Python interpreter.
    • Interpreter: Enter the Python path copied in step 1, for example, /home/ma-user/anaconda3/envs/Pytorch-1.0.0/bin/python.

      If the path is ~/anaconda3/envs/Pytorch-1.0.0/bin/python, replace ~ with /home/ma-user.

    • Sync folders: Set this parameter to a directory in the cloud development environment for synchronizing local project directory files. A directory in /home/ma-user is recommended, for example, /home/ma-user/work/projects, because other directories may be prohibited from accessing.
  4. Click ! on the right and select Automatically upload so that the locally modified file can be automatically uploaded to the container.
  5. Click Finish.

    The local project file has been automatically uploaded to the cloud environment. Each time a local file is modified, the modification is automatically synchronized to the cloud environment.

    In the lower right corner, the current interpreter is displayed as a remote interpreter.

    Figure 4 Configuring a Python interpreter

Step 4 Install the Dependent Library for the Cloud Environment

After accessing the development environment, you can use different virtual environments, such as TensorFlow and PyTorch. However, in actual development, you need to install dependency packages. Then, you can access the environment through the terminal to perform operations.

Choose Tools > Start SSH Session and select the configured development environment. Run the pip install command to install the required dependency packages.

Step 5 Debug Code in the Development Environment

You have accessed the cloud development environment. Then, you can write, debug, and run the code in the local PyCharm. The code is actually executed in the cloud development environment, and the Ascend AI resources on the cloud are used. In this way, you compile and modify code locally and run the code in the cloud.

Run the code in the local IDE. The logs can be displayed locally.

Figure 5 Debugging code

Click Run/Debug Configurations in the upper right corner of the local IDE to set runtime parameters.

Figure 6 Setting runtime parameters

To debug code, set breakpoints and run the program in debug mode.

Figure 7 Code breakpoint
Figure 8 Debugging in debug mode

In debug mode, the code execution is suspended in the specified line, and you can obtain variable values.

Figure 9 Debug mode

Before debugging code in debug mode, ensure that the local code is the same as the cloud code. If they are different, the line where a breakpoint is added locally may be different from the line of the cloud code, leading to errors.

When configuring a Python interpreter in the cloud development environment, select Automatically upload so that any local file modification can be automatically uploaded to the cloud. If you do not select Automatically upload, manually upload the directory or code after you modify the local code. For details, see Step 6 Upload Local Files to the Notebook Instance.