Pycharm同步代码到服务器并远程调试demo

motivation:Windows本地系统中有一个工程代码,需要上传到服务器端并使用服务器端的python环境运行代码。当本地的代码改变时,服务器端的代码实时同步。

1、安装pycharm专业版

注:需要安装pycharm专业版,community版本缺少远程调试功能。

2、在本地端新建demo工程

工程所在路径:D:\Codes\python\local,main.py代码如下:

# This is a sample Python script.

# Press Shift+F10 to execute it or replace it with your code.
# Press Double Shift to search everywhere for classes, files, tool windows, actions, and settings.


def print_hi(name):
    # Use a breakpoint in the code line below to debug your script.
    print(f'Hi, {name}')  # Press Ctrl+F8 to toggle the breakpoint.


# Press the green button in the gutter to run the script.
if __name__ == '__main__':
    print_hi('PyCharm')

# See PyCharm help at https://www.jetbrains.com/help/pycharm/

运行python main.py得到如下输出:

3、配置远程连接

点击Tools->Deployment->Configuration

点击+->SFTP 

 命名服务器的名字,随便起名都可以,这里起名为253

 点击...

点击+

 设置参数

点击OK

设置服务器路径

即是说,pycharm会把本地D:\Codes\python\local的代码同步到服务器/home1/users/XXX/Codes/fromlocal/路径下。

点击Tools->Deployment->Options

改变选项,它表示,我们每次Ctrl+S保存时,代码就会同步一次

4、上传工程代码

右键点击需要上传的工程,上传到第3步骤新建的253服务器

服务器对应路径下面就出现了上传的文件

5、配置python环境

点击File->Settings

 添加python环境

 

6、配置main.py的运行环境

7、运行并测试同步

更改local端的代码:

# This is a sample Python script.

# Press Shift+F10 to execute it or replace it with your code.
# Press Double Shift to search everywhere for classes, files, tool windows, actions, and settings.


def print_hi(name):
    # Use a breakpoint in the code line below to debug your script.
    print(f'Hi, {name}')  # Press Ctrl+F8 to toggle the breakpoint.


# Press the green button in the gutter to run the script.
if __name__ == '__main__':
    print_hi('PyCharm')
    print('test1: change from local')

# See PyCharm help at https://www.jetbrains.com/help/pycharm/

再次运行,可实现同步:

8、调用服务器终端运行

新建终端

进入工程路径并运行,可以得到一样的结果

更改local的main.py

# This is a sample Python script.

# Press Shift+F10 to execute it or replace it with your code.
# Press Double Shift to search everywhere for classes, files, tool windows, actions, and settings.


def print_hi(name):
    # Use a breakpoint in the code line below to debug your script.
    print(f'Hi, {name}')  # Press Ctrl+F8 to toggle the breakpoint.


# Press the green button in the gutter to run the script.
if __name__ == '__main__':
    print_hi('PyCharm')
    print('test1: change from local')
    print('test2: change from local')

# See PyCharm help at https://www.jetbrains.com/help/pycharm/

Ctrl+S保存后再次运行,可实现同步

 故两种方式运行,local的更改都可以在服务器端实时同步。

猜你喜欢

转载自blog.csdn.net/qq_17783559/article/details/119646648