Python 问题收集贴

问题一:python ImportError: DLL load failed: %1 不是有效的 Win32 应用程序

此问题是Python的版本与导入的模块的位数不一致引用,比如Python是64位的,模块是32位的。所以,解决此问题的方法就是安装相应位数的模块。

C:\Users\Administrator>python
Python 2.7.12rc1 (v2.7.12rc1:13912cd1e7e8, Jun 12 2016, 05:57:31) [MSC v.1500 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.

这里可以看出我的Python2.7是64位的([MSC v.1500 64 bit (AMD64)])

问题二:Python Version 2.7 required which was not found in the registry

这个问题是安装MySQL-python时出现的,这时我的Windows系统安装了Python 2.7和Python 3.5,系统默认用的是Python 3.5,而我们安装的这个MySQL-python只支持2.7,所以我需要将系统的默认的Python环境修改为2.7。
步骤一:改变环境变量Path,将3.5的改成2.7的
步骤二:执行下面的Python代码

#
# script to register Python 2.0 or later for use with win32all
# and other extensions that require Python registry settings
#
# written by Joakim Loew for Secret Labs AB / PythonWare
#
# source:
# http://www.pythonware.com/products/works/articles/regpy20.htm
#
# modified by Valentine Gogichashvili as described in http://www.mail-archive.com/distutils-sig@python.org/msg10512.html

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()

通过上面的两步就可以将系统默认的Python环境从3.5变为2.7了,这样我就可以安装MySQL-python啦。当然MySQL-python只能运行在2.7,如果我想在3.5上做怎么办,使用PyMySQL替换既可。
上面代码做的事情其实就是在注册表中在HKEY_CURRENT_USER写入相应的Python环境
另:
Python的一些第三方库只到注册表的HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Python\PythonCore\2.7\InstallPath路径下寻找Python。但是装好的64位Python在HKEY_LOCAL_MACHINE\SOFTWARE\Python\PythonCore\2.7\InstallPath路径下建立Python的信息。所以,在cmd输入regedit然后将HKEY_LOCAL_MACHINE\SOFTWARE\Python\PythonCore\2.7\InstallPath路径下的值,复制到HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Python\PythonCore\2.7\InstallPath路径下,当然没有路径的话一定要新建起来。
然后就可以安装了。
本解决方案适用于安装numpy,SetupTools等只提供了32位安装包的库。

P.S
Python的功能很大程度上取决于第三方库,但是很多开发这并不喜欢提供64位版本。上面方法只是个参考,其实最靠谱的解决方案1、Python和第三方库都装成32位;2、把第三方库文件下载下来,重新编译。

问题三:SyntaxError: Non-ASCII character ‘\xe7’ in file

这是编码格式的问题,可能是不识别格式的字符,在程序的最开始设置如下代码即可

# coding:utf8
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值