1.包导入错误ModuleNotFoundError: No module named 'sklearn.cross_validation'
错误:
1from sklearn.cross_validation import train_test_split 2ModuleNotFoundError: No module named 'sklearn.cross_validation'
解决办法: 改为
1from sklearn.model_selection import train_test_split
2.Django安装报错
直接用pip install django命令安装可能会报错,用下面的命令就不会报错了:
1pip install django -i https://pypi.doubanio.com/simple/
pip install xxx -i xxx是通过镜像源安装,更换国内源。很多时候安装第三方包会面临下载速度慢,甚至无法下载的尴尬,这时,配置一个国内源极其重要,通过这种方式会加快下载安装相关包的速度,很多常见的包安装错误都可以通过这种方式解决。
常用的镜像源有
1阿里云 2 http://mirrors.aliyun.com/pypi/simple/ 3中国科技大学 4 https://pypi.mirrors.ustc.edu.cn/simple/ 5豆瓣 6 http://pypi.douban.com/simple/ 7清华大学 8 https://pypi.tuna.tsinghua.edu.cn/simple/ 9中国科学技术大学 10 http://pypi.mirrors.ustc.edu.cn/simple/
如,
1pip 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时,会有警告
1 DeprecationWarning: 2 Using or importing the ABCs from 'collections' 3 instead of from 'collections.abc' is deprecated, 4 and in 3.8 it willstop working 5
意思是:
弃用警告:从collections中导入ABCs已被弃用,并在python3.8中将停止工作,可使用collections.abc代替它进行使用 解决办法: 把
collections替换为collections.abc即可:
1# from collections import Iterable 2from collections.abc import Iterable 3print(isinstance('abc', Iterable))
4.virtualenvwrapper安装后执行workon命令出现异常
在安装虚拟环境管理工具virtualenvwrapper之后,再执行workon命令查看创建的虚拟环境,报错如下:
1'workon' 不是内部或外部命令,也不是可运行的程序 2或批处理文件。
这可能是因为在安装virtualenvwrapper时使用的命令是pip install virtualenvwrapper,而这是Python2中的用法,在Python3中安装需要使用命令pip install virtualenvwrapper-win安装,此时再执行workon命令会打印:
1 2Pass a name to activate one of the following virtualenvs: 3============================================================================== 4找不到文件 5
即安装成功。
5.安装mysqlclient时报错Microsoft Visual C++ 14.0 is required
在Django等项目中可能需要用到mysqlclient作为数据库引擎,但是在直接通过命令安装的时候可能会报错如下:
1 ERROR: Command errored out with exit status 1: 2 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 3t\\setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec 4'"'"'))' 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' 5 cwd: XXX\AppData\Local\Temp\pip-install-mu3pnz6j\mysqlclient\ 6 Complete output (23 lines): 7 running install 8 running build 9 running build_py 10 creating build 11 creating build\lib.win32-3.8 12 creating build\lib.win32-3.8\MySQLdb 13 copying MySQLdb\__init__.py -> build\lib.win32-3.8\MySQLdb 14 copying MySQLdb\_exceptions.py -> build\lib.win32-3.8\MySQLdb 15 copying MySQLdb\connections.py -> build\lib.win32-3.8\MySQLdb 16 copying MySQLdb\converters.py -> build\lib.win32-3.8\MySQLdb 17 copying MySQLdb\cursors.py -> build\lib.win32-3.8\MySQLdb 18 copying MySQLdb\release.py -> build\lib.win32-3.8\MySQLdb 19 copying MySQLdb\times.py -> build\lib.win32-3.8\MySQLdb 20 creating build\lib.win32-3.8\MySQLdb\constants 21 copying MySQLdb\constants\__init__.py -> build\lib.win32-3.8\MySQLdb\constants 22 copying MySQLdb\constants\CLIENT.py -> build\lib.win32-3.8\MySQLdb\constants 23 copying MySQLdb\constants\CR.py -> build\lib.win32-3.8\MySQLdb\constants 24 copying MySQLdb\constants\ER.py -> build\lib.win32-3.8\MySQLdb\constants 25 copying MySQLdb\constants\FIELD_TYPE.py -> build\lib.win32-3.8\MySQLdb\constants 26 copying MySQLdb\constants\FLAG.py -> build\lib.win32-3.8\MySQLdb\constants 27 running build_ext 28 building 'MySQLdb._mysql' extension 29 error: Microsoft Visual C++ 14.0 is required. Get it with "Build Tools for Visual Studio": https://visualstudio.microsoft.com/downloads/ 30 ---------------------------------------- 31ERROR: Command errored out with exit status 1: 'XXX\.virtualenvs\Django_Vue_Fresh_Ecommerce-NKba4OvD\Scripts\python.exe' -u -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'XXX\\AppData\\Local\\Temp\\pip-installmu3pnz6j\\mysqlclient\\setup.py'"'"'; __file__='"'"'XXX\\AppData\\Local\\Temp\ 32\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-managed --compile --install-headers 'XXX\.virtualenvs\Django_Vue_Fresh_Ecommerce-NKba4OvD\include\site\python3.8\mysqlclient' Check the logs for full command output. 33 34
这可能是因为Python的版本较新,pip未同步更新等,可以手动下载安装文件再安装。
先在https://www.lfd.uci.edu/~gohlke/pythonlibs/#mysqlclient中选择所对应的系统和Python版本下载.whl文件,由于官网下载很缓慢,因此我已经将Python各版本的安装包下载整理好了,可以直接点击加QQ群 <a target="_blank" href="https://qm.qq.com/cgi-bin/qm/qr?k=rgE7cwG7OGHgfEucpRIQoSlYCTOEkmEr&jump_from=webapi"><img border="0" src="https://pub.idqqimg.com/wpa/images/group.png" alt="Python极客部落" title="Python极客部落">963624318</a> 在群文件夹Python相关安装包中下载即可。
下载完成后,将其拷贝到虚拟环境目录下执行命令pip install filename.whl(filename.whl即为下载文件的文件名),即安装成功。
6.通过pip安装库时报错"python setup.py egg_info" failed with error code 1 in /tmp/pip-build-xxx/xxx
在使用pip安装Python库时报错Command "python setup.py egg_info" failed with error code 1 in /tmp/pip-build-xxx/xxx,这是因为pip版本较低,需要对pip进行升级,直接执行pip install --upgrade pip或者pip3 install --upgrade pip即可,然后再安装库就不会报错了。
7.CentOS安装pip3报错ModuleNotFoundError: No module named '_ctypes'
CentOS上安装pip3,在执行python3 setup.py install报错ModuleNotFoundError: No module named '_ctypes',这是因为缺少依赖包libffi-devel,需要先安装该包再重新编译安装pip3,如下:
1# 安装依赖包 2yum -y install libffi-devel 3 4# 编译源码 5make 6 7# 重新执行安装 8make altinstall
8.安装virtualenvwrapper后,提示'workon' 不是内部或外部命令
有可能在安装的时候,使用的是pip install virtualenvwrapper命令,而这是在Python2中的写法,我们应该使用Python3中的写法,即通过pip install virtualenvwrapper-win安装,就可以正常使用workon命令了。
本文原文首发来自博客专栏Python实战,由本人转发至https://www.helloworld.net/p/lMmcePFj4iej,其他平台均属侵权,可点击https://blog.csdn.net/CUFEECR/article/details/103093951查看原文,也可点击https://blog.csdn.net/CUFEECR浏览更多优质原创内容。
