Python 库的安装及使用 常见错误异常及解决办法

时间:2022-07-22
本文章向大家介绍Python 库的安装及使用 常见错误异常及解决办法,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

文章目录

  • 1.包导入错误ModuleNotFoundError: No module named 'sklearn.cross_validation'
  • 2.Django安装报错
  • 3.DeprecationWarning: Using or importing the ABCs from 'collections' instead of from 'collections.abc'
  • 4.virtualenvwrapper安装后执行workon命令出现异常
  • 5.安装mysqlclient时报错ERROR: Command errored out with exit status 1

1.包导入错误ModuleNotFoundError: No module named ‘sklearn.cross_validation’

错误:

from sklearn.cross_validation import train_test_split
ModuleNotFoundError: No module named 'sklearn.cross_validation'

解决办法: 改为

from sklearn.model_selection import train_test_split

2.Django安装报错

直接用pip install django命令安装可能会报错,用下面的命令就不会报错了:

pip  install  django -i  https://pypi.doubanio.com/simple/

pip install xxx -i xxx是通过镜像源安装,更换国内源。很多时候安装第三方包会面临下载速度慢,甚至无法下载的尴尬,这时,配置一个国内源极其重要,通过这种方式会加快下载安装相关包的速度,很多常见的包安装错误都可以通过这种方式解决。 常用的镜像源有

阿里云
    http://mirrors.aliyun.com/pypi/simple/
中国科技大学
    https://pypi.mirrors.ustc.edu.cn/simple/ 
豆瓣
    http://pypi.douban.com/simple/ 
清华大学
    https://pypi.tuna.tsinghua.edu.cn/simple/
中国科学技术大学
    http://pypi.mirrors.ustc.edu.cn/simple/

如,

pip install xxx -i http://pypi.douban.com/simple --trusted-host pypi.douban.com

还可以永久使用国内pypi镜像安装,相关操作可参考Python pip配置国内源

3.DeprecationWarning: Using or importing the ABCs from ‘collections’ instead of from ‘collections.abc’

使用from collections import Iterable时,会有警告

 DeprecationWarning: 
 Using or importing the ABCs from 'collections' 
 instead of from 'collections.abc' is deprecated,
 and in 3.8 it willstop working 

意思是:

弃用警告:从collections中导入ABCs已被弃用,并在python3.8中将停止工作,可使用collections.abc代替它进行使用 解决办法: 把collections替换为collections.abc即可:

# from collections import Iterable
from collections.abc import Iterable
print(isinstance('abc', Iterable))

4.virtualenvwrapper安装后执行workon命令出现异常

在安装虚拟环境管理工具virtualenvwrapper之后,再执行workon命令查看创建的虚拟环境,报错如下:

'workon' 不是内部或外部命令,也不是可运行的程序
或批处理文件。

这可能是因为在安装virtualenvwrapper时使用的命令是pip install virtualenvwrapper,而这是Python2中的用法,在Python3中安装需要使用命令pip install virtualenvwrapper-win安装,此时再执行workon命令会打印:

Pass a name to activate one of the following virtualenvs:                       
==============================================================================  
找不到文件                                                                           

即安装成功。

5.安装mysqlclient时报错ERROR: Command errored out with exit status 1

在Django等项目中可能需要用到mysqlclient作为数据库引擎,但是在直接通过命令安装的时候可能会报错如下:

    ERROR: Command errored out with exit status 1:
     command: 'XXX.virtualenvsDjango_Vue_Fresh_Ecommerce-NKba4OvDScriptspython.exe' -u -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'XXX\AppData\Local\Temp\pip-install-mu3pnz6j\mysqlclient\setup.py'"'"'; __file__='"'"'XXX\AppData\Local\Temp\pip-install-mu3pnz6j\mysqlclien
t\setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'rn'"'"', '"'"'n'"'"');f.close();exec(compile(code, __file__, '"'"'exec
'"'"'))' install --record 'XXXAppDataLocalTemppip-record-l1qvs6beinstall-record.txt' --single-version-externally-managed --compile --install-headers 'XXX.virtualenvsDjango_Vue_Fresh_Ecommerce-NKba4OvDincludesitepython3.8mysqlclient'
         cwd: XXXAppDataLocalTemppip-install-mu3pnz6jmysqlclient
    Complete output (23 lines):
    running install
    running build
    running build_py
    creating build
    creating buildlib.win32-3.8
    creating buildlib.win32-3.8MySQLdb
    copying MySQLdb__init__.py -> buildlib.win32-3.8MySQLdb
    copying MySQLdb_exceptions.py -> buildlib.win32-3.8MySQLdb
    copying MySQLdbconnections.py -> buildlib.win32-3.8MySQLdb
    copying MySQLdbconverters.py -> buildlib.win32-3.8MySQLdb
    copying MySQLdbcursors.py -> buildlib.win32-3.8MySQLdb
    copying MySQLdbrelease.py -> buildlib.win32-3.8MySQLdb
    copying MySQLdbtimes.py -> buildlib.win32-3.8MySQLdb
    creating buildlib.win32-3.8MySQLdbconstants
    copying MySQLdbconstants__init__.py -> buildlib.win32-3.8MySQLdbconstants
    copying MySQLdbconstantsCLIENT.py -> buildlib.win32-3.8MySQLdbconstants
    copying MySQLdbconstantsCR.py -> buildlib.win32-3.8MySQLdbconstants
    copying MySQLdbconstantsER.py -> buildlib.win32-3.8MySQLdbconstants
    copying MySQLdbconstantsFIELD_TYPE.py -> buildlib.win32-3.8MySQLdbconstants
    copying MySQLdbconstantsFLAG.py -> buildlib.win32-3.8MySQLdbconstants
    running build_ext
    building 'MySQLdb._mysql' extension
    error: Microsoft Visual C++ 14.0 is required. Get it with "Build Tools for Visual Studio": https://visualstudio.microsoft.com/downloads/
    ----------------------------------------
ERROR: Command errored out with exit status 1: 'XXX.virtualenvsDjango_Vue_Fresh_Ecommerce-NKba4OvDScriptspython.exe' -u -c 'import sys, setuptools, token
ize; sys.argv[0] = '"'"'XXX\AppData\Local\Temp\pip-install-mu3pnz6j\mysqlclient\setup.py'"'"'; __file__='"'"'XXX\AppData\Local\Temp
pip-install-mu3pnz6j\mysqlclient\setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'rn'"'"', '"'"'n'"'"');f.close();exec(
compile(code, __file__, '"'"'exec'"'"'))' install --record 'XXXAppDataLocalTemppip-record-l1qvs6beinstall-record.txt' --single-version-externally-manage
d --compile --install-headers 'XXX.virtualenvsDjango_Vue_Fresh_Ecommerce-NKba4OvDincludesitepython3.8mysqlclient' Check the logs for full command outpu
t.

这可能是因为Python的版本较新,pip未同步更新等,可以手动下载安装文件再安装。

先在https://www.lfd.uci.edu/~gohlke/pythonlibs/#mysqlclient中选择所对应的系统和Python版本下载.whl文件,并将其拷贝到虚拟环境目录下执行命令pip install filename.whl(filename.whl即为下载文件的文件名),即安装成功。