环境:windows7 + python3.6.0
在尝试使用python的flask时,按照flask的tutorial操作,装好flask、venv后,对tutorial中的hello.py进行运行时发现了markupsafe._compat包缺失的问题
如下:
1 G:WorkpythonMain>venvScriptsactivate
2
3 (venv) G:WorkpythonMain>set FLASK_APP=hello.py
4
5 (venv) G:WorkpythonMain>flask run
6 Traceback (most recent call last):
7 File "e:pythonlib
unpy.py", line 193, in _run_module_as_main
8 "__main__", mod_spec)
9 File "e:pythonlib
unpy.py", line 85, in _run_code
10 exec(code, run_globals)
11 File "E:pythonScriptsflask.exe\__main__.py", line 5, in <module>
12 File "e:pythonlibsite-packagesflask\__init__.py", line 19, in <module>
13 from jinja2 import Markup, escape
14 File "e:pythonlibsite-packagesjinja2\__init__.py", line 33, in <module>
15 from jinja2.environment import Environment, Template
16 File "e:pythonlibsite-packagesjinja2environment.py", line 15, in <module>
17
18 from jinja2 import nodes
19 File "e:pythonlibsite-packagesjinja2
odes.py", line 19, in <module>
20 from jinja2.utils import Markup
21 File "e:pythonlibsite-packagesjinja2utils.py", line 624, in <module>
22 from markupsafe import Markup, escape, soft_unicode
23 File "e:pythonlibsite-packagesmarkupsafe\__init__.py", line 14, in <module
24 >
25 from markupsafe._compat import text_type, string_types, int_types,
26 ModuleNotFoundError: No module named 'markupsafe._compat'
使用 pip freeze 查看已安装的包时,markupsafe 和 jinja2都是已安装的状态
我将这两个包反复卸载再安装也并没有得到解决
在网上没有找到解决方案的时候,看见有一些 _compat.py 的文件内容如下:
附上网址:
https://chromium.googlesource.com/infra/third_party/MarkupSafe/+/188802ce86879aafc3472619ce82ae1768210d83/markupsafe/_compat.py
https://www.apt-browse.org/browse/debian/jessie/main/amd64/python3-markupsafe/0.23-1+b1/file/usr/lib/python3/dist-packages/markupsafe/_compat.py
1 # -*- coding: utf-8 -*-
2 """
3 markupsafe._compat
4 ~~~~~~~~~~~~~~~~~~
5 Compatibility module for different Python versions.
6 :copyright: (c) 2013 by Armin Ronacher.
7 :license: BSD, see LICENSE for more details.
8 """
9 import sys
10 PY2 = sys.version_info[0] == 2
11 if not PY2:
12 text_type = str
13 string_types = (str,)
14 unichr = chr
15 int_types = (int,)
16 iteritems = lambda x: iter(x.items())
17 else:
18 text_type = unicode
19 string_types = (str, unicode)
20 unichr = unichr
21 int_types = (int, long)
22 iteritems = lambda x: x.iteritems()
对照之下,两个网址上的这个文件内容是一样的,我在python的文件中找到markupsafe文件夹,文件夹内并没有这个文件。
虽然我并不知道为什么没有装上,但本着尝试的心态就自己在该目录下新建了个python文件,并把这个内容复制了进去,于是成功解决了我的问题。
本地目录是python目录下的:Libsite-packagesmarkupsafe
1 (venv) G:WorkpythonMain>flask run
2 * Serving Flask app "hello"
3 * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
所以嘛,正所谓:没有文件,新建文件也要上