前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Python 库的安装及使用 常见错误异常及解决办法

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

作者头像
cutercorley
发布2020-07-23 15:55:27
3.1K0
发布2020-07-23 15:55:27
举报
文章被收录于专栏:Corley的开发笔记

文章目录

  • 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’

错误:

代码语言:javascript
复制
from sklearn.cross_validation import train_test_split
ModuleNotFoundError: No module named 'sklearn.cross_validation'

解决办法: 改为

代码语言:javascript
复制
from sklearn.model_selection import train_test_split

2.Django安装报错

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

代码语言:javascript
复制
pip  install  django -i  https://pypi.doubanio.com/simple/

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

代码语言:javascript
复制
阿里云
    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/

如,

代码语言:javascript
复制
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时,会有警告

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

代码语言:javascript
复制
# from collections import Iterable
from collections.abc import Iterable
print(isinstance('abc', Iterable))

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

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

代码语言:javascript
复制
'workon' 不是内部或外部命令,也不是可运行的程序
或批处理文件。

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

代码语言:javascript
复制
Pass a name to activate one of the following virtualenvs:                       
==============================================================================  
找不到文件                                                                           

即安装成功。

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

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

代码语言:javascript
复制
    ERROR: Command errored out with exit status 1:
     command: 'XXX\.virtualenvs\Django_Vue_Fresh_Ecommerce-NKba4OvD\Scripts\python.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('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec
'"'"'))' install --record 'XXX\AppData\Local\Temp\pip-record-l1qvs6be\install-record.txt' --single-version-externally-managed --compile --install-headers 'XXX\.virtualenvs\Django_Vue_Fresh_Ecommerce-NKba4OvD\include\site\python3.8\mysqlclient'
         cwd: XXX\AppData\Local\Temp\pip-install-mu3pnz6j\mysqlclient\
    Complete output (23 lines):
    running install
    running build
    running build_py
    creating build
    creating build\lib.win32-3.8
    creating build\lib.win32-3.8\MySQLdb
    copying MySQLdb\__init__.py -> build\lib.win32-3.8\MySQLdb
    copying MySQLdb\_exceptions.py -> build\lib.win32-3.8\MySQLdb
    copying MySQLdb\connections.py -> build\lib.win32-3.8\MySQLdb
    copying MySQLdb\converters.py -> build\lib.win32-3.8\MySQLdb
    copying MySQLdb\cursors.py -> build\lib.win32-3.8\MySQLdb
    copying MySQLdb\release.py -> build\lib.win32-3.8\MySQLdb
    copying MySQLdb\times.py -> build\lib.win32-3.8\MySQLdb
    creating build\lib.win32-3.8\MySQLdb\constants
    copying MySQLdb\constants\__init__.py -> build\lib.win32-3.8\MySQLdb\constants
    copying MySQLdb\constants\CLIENT.py -> build\lib.win32-3.8\MySQLdb\constants
    copying MySQLdb\constants\CR.py -> build\lib.win32-3.8\MySQLdb\constants
    copying MySQLdb\constants\ER.py -> build\lib.win32-3.8\MySQLdb\constants
    copying MySQLdb\constants\FIELD_TYPE.py -> build\lib.win32-3.8\MySQLdb\constants
    copying MySQLdb\constants\FLAG.py -> build\lib.win32-3.8\MySQLdb\constants
    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\.virtualenvs\Django_Vue_Fresh_Ecommerce-NKba4OvD\Scripts\python.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('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(
compile(code, __file__, '"'"'exec'"'"'))' install --record 'XXX\AppData\Local\Temp\pip-record-l1qvs6be\install-record.txt' --single-version-externally-manage
d --compile --install-headers 'XXX\.virtualenvs\Django_Vue_Fresh_Ecommerce-NKba4OvD\include\site\python3.8\mysqlclient' 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即为下载文件的文件名),即安装成功。

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

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

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

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

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 文章目录
  • 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
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档