首页
学习
活动
专区
圈层
工具
发布
社区首页 >专栏 >Python3配置和入坑.md

Python3配置和入坑.md

作者头像
全栈工程师修炼指南
发布2022-09-28 14:10:26
发布2022-09-28 14:10:26
2920
举报

[TOC]

0x00 基础配置

IDLE 快捷键

代码语言:javascript
复制
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)

代码语言:javascript
复制
"""
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扩展的配置文件),用记事本打开 在文件末尾添加如下代码:

代码语言:javascript
复制
[ClearWindow]
enable=1
enable_editor=0
enable_shell=1
[ClearWindow_cfgBindings]
clear-window=<Control-Key-l>

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

代码语言:javascript
复制
#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.右键打开

`

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2019-03-20,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 0x00 基础配置
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档