Windows下安装BeautifulSoup4显示'You are trying to run the Python 2 version of Beautiful Soup under Python

按照网上教程,将cmd的目录定位到解压缩文件夹地址,而后 >>python setup.py installpython

(shell

Window下不能直接解压tar.giz文件,能够使用7z解压软件提取解压再在CMD下打开工具

)code

可是在IDLE中import bs4时,会出现:教程

Traceback (most recent call last):
  File "<pyshell#3>", line 1, in <module>
    import bs4
  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python35-32\lib\bs4\__init__.py", line 48
    'You are trying to run the Python 2 version of Beautiful Soup under Python 3. This will not work.'<>'You need to convert the code, either by installing it (`python setup.py install`) or by running 2to3 (`2to3 -w bs4`).'ip

在网上找了好久,始终没解决。cmd

而后我又试了一下>>easy_setup.py,import bs4时报的错误倒是:no such module.it

 

 

解决办法:直接将压缩文件中的bs4复制到python安装目录下的lib中,而后再利用python自带工具2to3.py将版本2下的.py 文件转化为版本3下的文件。io

具体:将bs4文件夹和2to3.py同时放到lib中,而后在cmd中定位到lib,运行:2to3.py bs4 -w就行了。ast

2to3.py位置:在python安装目录下的Tools/scripts/2to3.py。

2to3.py 用法:2to3.py param1 (-w)

param1 能够是要转换的.py文件,或者是文件夹,若是是文件夹,整个文件夹中的.py都会被转换。

-w可选,若是不写的话默认输出转换后的结果到显示屏,若是要把转换的文件再写入原文件,就须要加上。

 

如今,就能够from bs4 import BeautifulSoup了。

相关文章
相关标签/搜索