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的更改都可以在服务器端实时同步。

  • 0
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值