1-Python - python2的一些问题及解决办法

无法注释中文的解决办法

在文件顶部添加文件头:

# -*- coding:utf8 -*-    

无法显示中文的问题

import sys
reload(sys)
sys.setdefaultencoding('utf8')

解决版本兼容问题

from __future__ import unicode_literals

当安装第三方库的时候出现Python version 2.7 required, which was not found in the registry

建立一个register.py文件,然后python2执行脚本:

import sys

from _winreg import *

# tweak as necessary
version = sys.version[:3]
installpath = sys.prefix

regpath = "SOFTWARE\\Python\\Pythoncore\\%s\\" % (version)
installkey = "InstallPath"
pythonkey = "PythonPath"
pythonpath = "%s;%s\\Lib\\;%s\\DLLs\\" % (
    installpath, installpath, installpath
)

def RegisterPy():
    try:
        reg = OpenKey(HKEY_CURRENT_USER, regpath)
    except EnvironmentError as e:
        try:
            reg = CreateKey(HKEY_CURRENT_USER, regpath)
            SetValue(reg, installkey, REG_SZ, installpath)
            SetValue(reg, pythonkey, REG_SZ, pythonpath)
            CloseKey(reg)
        except:
            print "*** Unable to register!"
            return
        print "--- Python", version, "is now registered!"
        return
    if (QueryValue(reg, installkey) == installpath and
            QueryValue(reg, pythonkey) == pythonpath):
        CloseKey(reg)
        print "=== Python", version, "is already registered!"
        return
    CloseKey(reg)
    print "*** Unable to register!"
    print "*** You probably have another Python installation!"

if __name__ == "__main__":
    RegisterPy()

安装Python2之后,pip不能用的问题:Fatal error in launcher: Unable to create process using '"'

# 解决办法是用Python2解释器cmd执行如下命令:

python2 -m pip install --upgrade pip

# 就好了

that's all
posted @ 2018-03-19 16:48  听雨危楼  阅读(311)  评论(0编辑  收藏  举报