[TOC]

0x00 基础配置

IDLE 快捷键

1
ALT + p  #上一次执行的命令

(1)Python Shell 清屏方法

  • 针对Python命令行 os.system(‘cls’)
  • 针对IDLE Shell命令行 ctrl + l
    Copy:clearwindow.py文件,并放在Python安装目录PythonX\Lib\idlelib下面(我用的是python3.4.3所以路径是:D:\Program Files (x86)\Python35\Lib\idlelib)
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    """
    Clear Window Extension
    Add these lines to config-extensions.def

    [ClearWindow]
    enable=1
    enable_editor=0
    enable_shell=1
    [ClearWindow_cfgBindings]
    clear-window=<Control-Key-l>
    """

    class ClearWindow:

    menudefs = [
    ('options', [None,
    ('Clear Shell Window', '<<clear-window>>'),
    ]),]

    def __init__(self, editwin):
    self.editwin = editwin
    self.text = self.editwin.text
    self.text.bind("<<clear-window>>", self.clear_window2)

    self.text.bind("<<undo>>", self.undo_event) # add="+" doesn't work

    def undo_event(self, event):
    text = self.text

    text.mark_set("iomark2", "iomark")
    text.mark_set("insert2", "insert")
    self.editwin.undo.undo_event(event)

    # fix iomark and insert
    text.mark_set("iomark", "iomark2")
    text.mark_set("insert", "insert2")
    text.mark_unset("iomark2")
    text.mark_unset("insert2")


    def clear_window2(self, event): # Alternative method
    # work around the ModifiedUndoDelegator
    text = self.text
    text.undo_block_start()
    text.mark_set("iomark2", "iomark")
    text.mark_set("iomark", 1.0)
    text.delete(1.0, "iomark2 linestart")
    text.mark_set("iomark", "iomark2")
    text.mark_unset("iomark2")
    text.undo_block_stop()
    if self.text.compare('insert', '<', 'iomark'):
    self.text.mark_set('insert', 'end-1c')
    self.editwin.set_line_and_column()

    def clear_window(self, event):
    # remove undo delegator
    undo = self.editwin.undo
    self.editwin.per.removefilter(undo)

    # clear the window, but preserve current command
    self.text.delete(1.0, "iomark linestart")
    if self.text.compare('insert', '<', 'iomark'):
    self.text.mark_set('insert', 'end-1c')
    self.editwin.set_line_and_column()

    # restore undo delegator
    self.editwin.per.insertfilter(undo)
    在Python X\Lib\idlelib目录下找到config-extensions.def(IDLE扩展的配置文件),用记事本打开
    在文件末尾添加如下代码:
    1
    2
    3
    4
    5
    6
    [ClearWindow]
    enable=1
    enable_editor=0
    enable_shell=1
    [ClearWindow_cfgBindings]
    clear-window=<Control-Key-l>



(2)Python 多版本共存
我常用的方式就是一个加入PATH之中,另外一个版本不加入python之中;但是痛苦在于每次执行需要指定绝对路径,且进行pip下载的时候也需要在指定目录执行;
解决方法:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#Python 自带的解决方法
当安装python2.7以后直接调用 pip 执行的是 Python2.7 的 pip,如何解决?
pip3 install xxxx
py -3 -m pip install xxxx

#问题1:win10以下设置右键点击“Edit with IDLE”选项打开的 Python 版本。
#解决方案:
1. 在运行处输入 regedit 进入注册表;
2. 找到项[HKEY_CLASSES_ROOT\Python.File\shell\Edit with IDLE\command]
3. 双击(默认),将值改为:"C:\Python34\pythonw.exe" "C:\Python34\Lib\idlelib\idle.pyw" -e "%1" 即可


#问题2:如何指定双击打开 .py 文件的程序?
#解决方案:这时候通过修改“属性”的“打开方式”一般是没用的,因为Python特别任性,没办法......

#还是需要通过修改注册表解决。
1. 在运行处输入regedit进入注册表;
2. 找到项[HKEY_CLASSES_ROOT\Python.File\shell\open\command]
3. 双击(默认),将值改为: "C:\Python34\python.exe" "%1" %* 即可(这里我希望双击以 Python3.4 打开)。
4. 同时将 C:\Python34;C:\Python34\Scripts 添加到环境变量中(右键“这台电脑”->“高级系统设置”->“环境变量(N)...”,修改用户环境变量的 PATH 变量即可)。


#问题3:如果你的系统中同时存在 Python2.7 和 Python3.4,那么在命令行模式下输入 Python,默认是执行 Python2.7:
#因为 Python 在命令行模式下本身就是设置两者可以兼容
$ python
$ py -3
C:\Users\Administrator>py -2
Python 2 not found!
Installed Pythons found by py Launcher for Windows
-3.7-64 *

#追加技能:
当我的代码需要使用 Python2.7 时,则通过鼠标右键选择执行。
在注册表:HKEY_CLASSES_ROOT\Python.File\shell\ 中新建项“通过 Python2.7运行”,再新建项“command”,设置默认值为:"C:\Python27\python.exe" "%1" %* 即可

WeiyiGeek.右键打开

WeiyiGeek.右键打开

`