IDE & Framework之PyCharm使用记录

文章目录

    • PyCharm文件头注释
    • PyCharm使用技巧
      • 断点调试
      • PyCharm中Sources Root, Test Sources Root, Resources Root, Test Resources Root, Excluded的含义
    • 将Ubuntu电脑作为远程服务器时,查看ip地址
    • PyChram与GPU远程服务器互联
      • 1.1 建立本地与远程服务器连接
      • 1.2 Project Interpreter选为远程服务器中的解释器
      • 1.3 为当前要运行的脚本文件指定运行环境
      • 1.4 使用远程服务器运行脚本文件
      • 本地文件与服务器文件之间的同步
      • 查看远程服务器上的文件
      • 通过PyCharm连接远程服务器终端
    • 问题记录
      • 用PyCharm打开项目时,Indexing需要很长时间
      • 为什么在PyCharm中用远程解释器Debug时**某些时候**访问的不是远程端的代码而是远程端在本地的只读缓存代码, 怎么办?
      • 在PyCharm中安装依赖包
      • PyCharm Debug
    • 二级标题
    • 待补充
      • 待补充
    • 分割线
    • 分割线

PyCharm文件头注释

  在菜单栏选择"File | Settings", 在弹出的Settings窗口中选择"Editor | File and Code Templates | Python Script", 在右侧键入以下内容:

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
@file    : ${NAME}.py
@path_to_file: 
@author  : ${USER} hc-y
@date    : ${YEAR}/${MONTH}/${DAY}
@contact : ***@gmail.com
@brief   : 简介or简述创建该文件的目的
@intro   : 
@relatedfile: 有关联的文档
	file_001.py
	file_002.py

@annotation: hc-y_note:, hc-y_Q:, hc-y_highlight:, hc-y_add:, hc-y_modify:, hc-y_write:, 
"""

"""
运行该脚本文件时的命令行打印输出
"""

-Pycharm创建文件模板_小郑要飞飞-CSDN博客_pycharm如何创建js文件 20160313
-pycharm File and Code Template设置_nathan8的博客-CSDN博客_pycharm file and code 20180505

"""
# hc-y_Q0817
# hc-y_modifybug
# hc-y_note_un:加了后缀_un表示"好像是,大概是,有待进一步核实"
# hc-y_note:完善注释/修改注释/新增注释;

@license : Licence
@site    : ${SITE}
@software: ${PRODUCT_NAME}
@time    : ${DATE} ${TIME}
@time    : ${YEAR}/${MONTH}/${DAY}
"""

Code is copied from xxx/xxx/xxx.py def xxx().
Code is modified from xxx/xxx/xxx.py def xxx().
Code is created by hc-y_write1006.

hc-y_note0430:common op
'common op'表示常用操作, 供读写代码时参考;

hc-y_note0503:shape e.g,
'shape e.g,'表示在当前模型超参数设置下, Tensor的shape; 便于理解代码;

'# mark_record'位于adet/utils/comm.py脚本文件头部和尾部, 便于核查是否拷贝;

PyCharm使用技巧

  • PyCharm中自动调整代码格式的快捷键,默认为Ctrl+Alt+L
    pycharm使用技巧——自动调整代码格式汇总_ywsydwsbn的博客-CSDN博客_pycharm自动调整格式 20201103
  • xxx

断点调试

Online Python Compiler - online editor

  • How can I control execution of program?

We can tell debugger when to pause a program by setting breakpoints.
To set a breakpoint, click on blank area seen on left side of line number in editor. When you click it, it should display red circle; which means breakpoint is set on that line number. Here is image of how it looks like.

IDE & Framework之PyCharm使用记录_第1张图片

Once you set breakpoint, when you start program in debug mode, it will pause execution when program reaches the line where breakpoint is set.

IDE & Framework之PyCharm使用记录_第2张图片

Now we can use stepping commands to execute program line by line.

  1. continue – Resume program execution until next breakpoint is reached
  2. step into – Execute program line by line stepping into function
  3. step over – Execute program line by line but don’t go inside function call
  4. step out – Resume program execution until current function is finished

PyCharm中Sources Root, Test Sources Root, Resources Root, Test Resources Root, Excluded的含义

  • What are the various types that I can mark a directory as? – IDEs Support (IntelliJ Platform) | JetBrains
  • Content roots - Help | IntelliJ IDEA

将Ubuntu电脑作为远程服务器时,查看ip地址

  打开终端,键入命令ifconfig,找到打印输出中的“inet addr: 211.67.33.208 Bcast: 211.67.34.255 Mask: 255.255.252.0”。

  • * Ubuntu查看IP信息的两种方式_shenqh-CSDN博客 20190219
  • * 将自己的ubuntu电脑设置为一台可远程访问的服务器_s37-CSDN博客_ubuntu远程访问怎么设置 20170818
  • 将个人Ubuntu系统电脑当作服务器使用,用win10系统电脑远程访问服务器并且运行matlab代码_素小树的博客-CSDN博客 20181103

PyChram与GPU远程服务器互联

  • *** pycharm远程连接服务器运行代码加调试_yeler082的博客-CSDN博客 20181107
  • Pycharm与GPU远程服务器互联,并进行上传/下载/对比本地和服务器端代码异同_律己且好学,才能保证不坠入愤世嫉俗之列。-CSDN博客 20190419
  • Pycharm远程连接GPU服务器_【人工智能】王小草的博客-CSDN博客 20190117

1.1 建立本地与远程服务器连接

  在菜单栏选择"Tools | Deployment | Configuration", 点击左上角的"+" Create new server, 输入你给即将要连接的服务器所起的别名; 然后,

  • Connection下,
    • Type:SFTP,
    • Host:服务器的IP,
    • Port:端口号,
    • User name:服务器的登录名,
    • Authentication:Password,
    • Password:服务器的登录密码,
    • Root path:服务器存放代码的路径,
    • 点击"Test Connection"若显示Successfully connected to 服务器IP, 则表明连接成功, 再点击"OK"即可;
  • Mappings下, 设置与服务器中Deployment path相互同步的Local path;
  • Excluded Paths下, 添加不执行download and upload操作的路径;

1.2 Project Interpreter选为远程服务器中的解释器

  操作参考:Project Interpreter | 略 | SSH Interpreter | 略 |

  • step1:在菜单栏"Tools | Deployment | Configuration"中,PyChram与GPU远程服务器互联;
  • step2:在菜单栏"File | Settings… | Project: | Project Interpreter"中,点击右上角的"+“号,弹出"Add Python Interpreter"窗口,在"SSH Interpreter | Existing server configuration"中选择此前刚刚创建的SFTP连接,此时会提示"Remote SDK is saved in IDE settings, so it needs the deployment server to be saved there too. Which do you prefer? Create copy of this deployment server in IDE settings; Move this server to IDE settings”,此时应该点击"Move this server to IDE settings";
  • step3:再回到菜单栏"Tools | Deployment | Configuration"中,对"Mappings | Deployment path"进行修正即可;

1.3 为当前要运行的脚本文件指定运行环境

  在菜单栏选择"Run | Edit Configurations…"打开"Run/Debug Configurations"窗口后, 可以指定不同的Python interpreter;
如果要运行GPU程序, 则有时可能需要将CUDA路径添加到环境变量(Enviroment variables)中;

1.4 使用远程服务器运行脚本文件

  在本地编辑好脚本文件后, 需要先将其upload to远程服务器, 再运行;

如果通过上面的配置进行远程调试的话,我认为大致流程应该是下面这样:

  • 在RemoteHost面板中,选中想要修改的代码,然后右键点击Download from here将内容下载至本地(这个本地是你在配置Mappings时设置的本地文件夹);
  • 在本地(这个本地是你在配置Mappings时设置的本地文件夹)修改你的代码,修改完成后在编辑区域或者文件名上右键,选择upload to…来提交到服务器;
  • 在提交之后,你可以像普通调用本地解释器一样的直接运行本地的这个文件(但其实运行的是服务器的文件);
  • 在这里,虽然RemoteHost里的文件可以直接编辑,但是并不建议这么做,因为这里编辑之后并不能直接运行;

本地文件与服务器文件之间的同步

  • 同步整个项目: 在"Project资源管理器"选中整个project右键选择"Deployment | Upload to…/Download from…/Sync with Deployed to…“, 或者 在菜单栏选择"Tools | Deployment | Upload to…/Download from…/Sync with Deployed to…”;
  • 同步单个文件: 右键选择"Deployment | Upload to…/Download from…/Compare with Deployed to…/Sync with Deployed to…“, 或者 在Remote Host面板中右键选中某个文件夹/文件后选择"Upload here/Download from here/Sync With Local…/Compare With Local Version”;

  问题描述与解决方案:
  在本地修改了某个脚本文件, 并upload to远程服务器之后, 如果运行/调试时仍然follows the logic of the old code, 可以试试"File | Invalidate Caches / Restart …";

查看远程服务器上的文件

  在菜单栏选择"Tools | Deployment | Browse Remote Host", 可以打开相应的Remote Host面板,这个面板显示的就是远程服务器上的文件,显示的范围是你在"Tools | Deployment | Configuration"选项卡下配置的Root path路径下的文件;

通过PyCharm连接远程服务器终端

  在菜单栏选择"Tools | Start SSH session… | Select host to connect",选择远程服务服务器,便可在PyCharm自带的Terminal中进行远程服务器的终端操作。
IDE & Framework之PyCharm使用记录_第3张图片

问题记录

问题描述
  开始
原因分析:
  开始
解决方案:
  开始

用PyCharm打开项目时,Indexing需要很长时间

问题描述and原因分析
  用PyCharm打开项目时,Indexing需要很长时间的原因之一是项目工程里包含了很多的程序文件以及数据文件;
解决方案:
  解决方案是把某些程序文件和数据文件所在的文件夹设置为Excluded,也就是不把这些文件夹包含在项目工程内;操作方式一:在菜单栏"File | Settings… | Project: | Project Structure"中,点击右边的"+ Add Content Root“号,将不想索引的文件夹Mark as Excluded即可。操作方式二:直接用鼠标右击某个文件夹,选择"Mark Directory as | Excluded"。
打开PyCharm之后,下方一直在index,以及一直Updating中的问题_希望之下的博客-CSDN博客 20190425

为什么在PyCharm中用远程解释器Debug时某些时候访问的不是远程端的代码而是远程端在本地的只读缓存代码, 怎么办?

20200906
问题描述
  如题,例如Debug时访问的却是C:/Users/usrname/.PyCharm2019.2/system/remote_sources/-1468910861/-416239508/mmcv/runner/epoch_based_runner.py这种远程端在本地的只读缓存代码。
原因分析:
  system/remote_sources/路径下存储的是project解释器中安装的包的一个local副本, 即从远程解释器环境派生的只读缓存代码, 由于"mmcv library"是以Python site-packages的形式安装的, 所以对C:/Users/usrname/.PyCharm2019.2/system/remote_sources/-1468910861/-416239508/mmcv/runner/epoch_based_runner.py的内容修改并不能更新远程服务器/home/usrname/miniconda2/envs/cy_mmlab/lib/python3.8/site-packages/mmcv/runner/epoch_based_runner.py中的相应内容;

python - Pycharm debugger: breakpoint links to remote_source files - Stack Overflow 20191004

***20190513 Sergey Karpov: I couldn’t reproduce it. Did you install your modules as editable to your interpreter? remote_sources are the packages installed to your project interpreter and downloaded by PyCharm to have a local copy of them.

解决方案:
  要修改远程端的代码时, 有三种方式:

  • 方式一:
      在远程服务器的机器上进入相应的文件路径,执行文件内容的修改;
  • 方式二:
      在本地的PyCharm菜单栏选择"Tools | Start SSH session… | Select host to connect",选择远程服务服务器,便可在PyCharm自带的Terminal中进行远程服务器的终端操作;
cd /home/usrname/miniconda2/envs/cy_mmlab/lib/python3.8/site-packages/mmcv/runner
vim epoch_based_runner.py

在line29和line30之间插入以下代码:  # c-y_add20200904: record data: 
for img_id in range(data_loader.batch_size):
	data_batch['img_metas']._data[0][img_id].update(dict(mode=self.mode, epoch=self._epoch, img_iter=self._inner_iter, total_iterations=len(data_loader)))

在line125和line126之间插入以下注释:
# how does the code jump to [self.val()] evaluate the model on the validation set per 2 epochs?
# evaluation = dict(interval=2, metric='mAP')  c-y_Q20200908:每2个epoch后, 是如何跳转到self.val()执行val的?

在line50和line51之间插入以下代码:  # c-y_add20200908: record data: 
# assert self._epoch < 1, 'record data, have finished one epoch!'
  • 方式三(暂未测试是否可行):
      在本地修改脚本文件的内容,然后通过本地PyCharm自带的Terminal上传到远程端执行文件替换;

***20190719 Alexleviyev: Having the same problem with WSL. I’m trying to modify a library that’s not meshing well with my code, but the debugger uses a cached version of my library after I edit it while using the debugger. No fix found yet?

***20190723 Alexleviyev:
@Andrey-Resler Yes, I did. Unfortunately that didn’t work either.

I found a hackey solution that might be useful for people. Basically, edit the temporary file. Then open the terminal and run the script I posted below. The idea is that you replace the actual library file in your filesystem with the temporary one you’re editing after you make your modifications. It solves the most annoying problems and makes the workflow as you’d expect it for the most part. This is WSL specific solution, but I’d expect if you’re working on a remote server the same idea would apply. Make a script that transfers the edited file to the remote before you launch your debugger every time and you should be golden.

The problem I’m running into now is that I can’t open projects that are located purely in WSL, and not on my /mnt/c/ directory. VSCODE seems to work, and I can call pycharm on anything in /mnt/c, but not in WSL. This makes it so that even though I can edit through some temporary files and continue with my debugger remotely, as soon as I try to get into a file that isn’t in my temporary directory the debugger stops . Any suggestions on how to handle that? If so, that would essentially provide a workaround for this bug in practice until a more concrete fix is presented

#!/bin/bash

# Used to push modified library files to remote_resources
# Will allow faster debugging of external libraries with pycharm

bilby='/home/alex/anaconda3/envs/myenv/lib/python3.6/site-packages/bilby/gw/'

pycharm='/mnt/c/Users/Alex/.PyCharm2019.1/system/remote_sources/1045508341/555082905/bilby/gw/'

# Files to forward
file1='utils.py'

# Copy over the files
cp $pycharm$file1 $bilby

在PyCharm中安装依赖包

问题描述
  Python Console中的python环境。
原因分析:
  通过PyCharm自带的Terminal进入的python环境 和 Python Console中的python环境 是不同的,得看它们各自使用的是哪条路径下的python.exe解释器。
  创建一个PyCharm项目时,可以根据需要自行选择确定“Project Interpreter”,以避免相关依赖包的版本冲突。
解决方案:
  在PyCharm中安装依赖包时可以采用以下方式:a.进入"File | Settings | Project Interpreter"里面,点击右边“+”号,搜索“Available Packages”并安装;b.在PyCharm自带的Terminal中,使用命令安装;

PyCharm Debug

问题描述
  PyCharm中,Debug模式下,Debugger | Variables无法显示"frame variables",Console | Show Python Prompt也无法打印输出"frame variables"的相关信息。
原因分析:
  无
解决方案:
  在PyCharm中打开File | Settings | Build, Execution, Deployment | Python Debugger, 勾选Gevent compatible;
debugging - Why does PyCharm say “Unable to display frame variables” in debug mode? - Stack Overflow 20161129

PyCharm_debug_Unable to display frame variables_20200813.jpg

问题描述
pydev debugger: warning: trying to add breakpoint to file that does not exist 20171129

二级标题

  

待补充

  

待补充

  



文字居中

数学公式粗体 \textbf{} 或者 m e m o r y {\bf memory} memory
数学公式粗斜体 \bm{}

摘录自“bookname_author”
此文系转载,原文链接:名称 20200505

高亮颜色说明:突出重点
个人觉得,:待核准个人观点是否有误

分割线

分割线


我是颜色为00ffff的字体
我是字号为2的字体
我是颜色为00ffff, 字号为2的字体
我是字体类型为微软雅黑, 颜色为00ffff, 字号为2的字体

分割线

分割线
问题描述:
原因分析:
解决方案:

你可能感兴趣的:(CV,&,DL,编程实现,python)