Windows下安裝BeautifulSoup4顯示'You are trying to run the Python 2 version of Beautiful Soup under Python 3.(`python setup.py install`) or by running 2to3 (`2to3 -w bs4`).'


按照網上教程,將cmd的目錄定位到解壓縮文件夾地址,然后 >>python setup.py install

(

Window下不能直接解壓tar.giz文件,可以使用7z解壓軟件提取解壓再在CMD下打開

)

但是在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`).'

在網上找了很久,始終沒解決。

然后我又試了一下>>easy_setup.py,import bs4時報的錯誤卻是:no such module.

 

 

解決辦法:直接將壓縮文件中的bs4復制到python安裝目錄下的lib中,然后再利用python自帶工具2to3.py將版本2下的.py 文件轉化為版本3下的文件。

具體:將bs4文件夾和2to3.py同時放到lib中,然后在cmd中定位到lib,運行:2to3.py bs4 -w就好了。

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

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

param1 可以是要轉換的.py文件,或者是文件夾,如果是文件夾,整個文件夾中的.py都會被轉換。

-w可選,如果不寫的話默認輸出轉換后的結果到顯示屏,如果要把轉換的文件再寫入原文件,就需要加上。

 

現在,就可以from bs4 import BeautifulSoup了。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM