BeautifulSoup,from bs4 import BeautifulSoup遇到python版本错误的解决办法

问题由来

换了台Windows环境,准备搭建Python学习环境。
OS:Windows 10
Python:3.7,从官网下载安装包,安装目录"C:\Users\Username\AppData\Local\Programs\Python\Python37"
安装Python3.7正常,下载安装BeautifulSoup正常(从官网下载压缩包,解压到D盘根目录(D:\beautifulsoup4-4.7.1),运行Powershell:

d:\beautifulsoup4-4.7.1\python setup.py install

安装完成后,bs4库文件安装在如下路径:C:\Users\usename\AppData\Local\P
rograms\Python\Python37\Lib\site-packages\beautifulsoup4-4.7.1-py3.7.egg
显示一切正常。

d:\> python
Python 3.7.1 (v3.7.1:260ec2c36a, Oct 20 2018, 14:57:15) [MSC v.1915 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> from bs4 import BeautifulSoup
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`).

解决办法

需要使用python自带的2to3脚本文件(2to3.py),转化bs4。
2to3.py在python安装目录的Tools/Scripts中(C:\Users\username\AppData\Local\Programs\Python\Python37\Tools\scripts),在powershell(或者命令提示符)中,进入2to3.py目录,运行如下命令:

C:\Users\username\AppData\Local\Programs\python\Python37\Tools\scripts>python 2to3.py -w C:\Users\steve\AppData\Local\P
rograms\Python\Python37\Lib\site-packages\beautifulsoup4-4.7.1-py3.7.egg\bs4

你可能感兴趣的:(BeautifulSoup,随笔)