Flask调试有时取决于tkinter

时间:2015-10-16 00:07:09

标签: python flask tkinter

当我尝试将非标准模块导入我的Flask应用程序并打开调试模式并在本地运行时,Flask服务器崩溃,导致_tkinter的ImportError。如果我删除非标准模块的导入,或关闭调试模式,一切都按预期工作。

实施例

以下运行正常,可以在localhost:5000

看到“Hello,World”
from flask import Flask
app = Flask(__name__)

@app.route("/")
def home():
    return "Hello, World"

if __name__ == '__main__':
    app.run(debug=True)

如果我在文件顶部添加以下行

import dateparser

运行文件时,我得到以下Traceback:

flask@ubuntu:~/tkerr$ python app.py 
   * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
   * Restarting with stat
  Traceback (most recent call last):
    File "app.py", line 10, in <module>
      app.run(debug=True)
    File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 772, in run
      run_simple(host, port, self, **options)
    File "/usr/local/lib/python2.7/dist-packages/werkzeug/serving.py", line 622, in run_simple
      reloader_type)
    File "/usr/local/lib/python2.7/dist-packages/werkzeug/_reloader.py", line 269, in run_with_reloader
      reloader.run()
    File "/usr/local/lib/python2.7/dist-packages/werkzeug/_reloader.py", line 159, in run
      for filename in chain(_iter_module_files(), self.extra_files):
    File "/usr/local/lib/python2.7/dist-packages/werkzeug/_reloader.py", line 70, in _iter_module_files
      for package_path in getattr(module, '__path__', ()):
    File "/usr/lib/python2.7/dist-packages/six.py", line 116, in __getattr__
      _module = self._resolve()
    File "/usr/lib/python2.7/dist-packages/six.py", line 105, in _resolve
      return _import_module(self.mod)
    File "/usr/lib/python2.7/dist-packages/six.py", line 76, in _import_module
      __import__(name)
    File "/usr/lib/python2.7/lib-tk/tkCommonDialog.py", line 11, in <module>
      from Tkinter import *
    File "/usr/lib/python2.7/lib-tk/Tkinter.py", line 42, in <module>
      raise ImportError, str(msg) + ', please install the python-tk package'
  ImportError: No module named _tkinter, please install the python-tk package

为了确认,关闭调试模式,即使我使用dateparser模块,一切都按预期运行。例如,以下显示“2015-01-01 00:00:00”

import dateparser
from flask import Flask
app = Flask(__name__)

@app.route("/")
def home():
    return str(dateparser.parse("1 jan"))

if __name__ == '__main__':
    app.run()

导入任何标准Python模块,例如

import json

工作正常。但是通过pip安装的任何其他模块都会导致同样的问题。

如果我跑(按错误提示)

sudo apt-get install python-tk

然后将Traceback替换为_winreg的

的ImportError
flask@ubuntu:~/tkerr$ python app.py 
 * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
 * Restarting with stat
Traceback (most recent call last):
  File "app.py", line 11, in <module>
    app.run(debug=True)
  File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 772, in run
    run_simple(host, port, self, **options)
  File "/usr/local/lib/python2.7/dist-packages/werkzeug/serving.py", line 622, in run_simple
    reloader_type)
  File "/usr/local/lib/python2.7/dist-packages/werkzeug/_reloader.py", line 269, in run_with_reloader
    reloader.run()
  File "/usr/local/lib/python2.7/dist-packages/werkzeug/_reloader.py", line 159, in run
    for filename in chain(_iter_module_files(), self.extra_files):
  File "/usr/local/lib/python2.7/dist-packages/werkzeug/_reloader.py", line 70, in _iter_module_files
    for package_path in getattr(module, '__path__', ()):
  File "/usr/lib/python2.7/dist-packages/six.py", line 116, in __getattr__
    _module = self._resolve()
  File "/usr/lib/python2.7/dist-packages/six.py", line 105, in _resolve
    return _import_module(self.mod)
  File "/usr/lib/python2.7/dist-packages/six.py", line 76, in _import_module
    __import__(name)
ImportError: No module named _winreg

详情

Python 2.7.7; Ubuntu 14.04(VMWare VM); Flask / Werkzeug 0.10.1

我们非常感谢解决方案,但我更希望深入了解可能导致Flask需要tkinter的原因,因为它会在Web浏览器中显示所有调试输出。

编辑添加pip冻结输出

Flask==0.10.1
Jinja2==2.7.3
MarkupSafe==0.23
PAM==0.4.2
Pillow==2.3.0
PyMySQL==0.6.6
PyYAML==3.11
Twisted-Core==13.2.0
Twisted-Web==13.2.0
Werkzeug==0.10.1
adium-theme-ubuntu==0.3.4
apt-xapian-index==0.45
argparse==1.2.1
beautifulsoup4==4.4.1
ccsm==0.9.11.3
chardet==2.0.1
colorama==0.2.5
command-not-found==0.3
compizconfig-python==0.9.11.3
dateparser==0.3.0
debtagshw==0.1
defer==1.0.6
dirspec==13.10
duplicity==0.6.23
feedparser==5.1.3
html5lib==0.999
httplib2==0.8
itsdangerous==0.24
lockfile==0.8
lxml==3.3.3
oauthlib==0.6.1
oneconf==0.3.7
pexpect==3.1
piston-mini-client==0.7.5
pyOpenSSL==0.13
pycrypto==2.6.1
pycups==1.9.66
pycurl==7.19.3
pygobject==3.12.0
pyserial==2.6
pysmbc==1.0.14.1
python-apt==0.9.3.5
python-dateutil==2.4.2
python-debian==0.1.21-nmu2ubuntu2
pyxdg==0.25
reporter==0.1.2
reportlab==3.0
requests==2.2.1
sessioninstaller==0.0.0
six==1.5.2
software-center-aptd-plugins==0.0.0
system-service==0.1.6
unity-lens-photos==1.0
urllib3==1.7.1
virtualenv==1.11.4
wsgiref==0.1.2
xdiagnose==3.6.3build2
youtube-dl==2014.02.17
zope.interface==4.0.5

1 个答案:

答案 0 :(得分:1)

我刚试过在我的Windows 10机器上运行以下代码,它对我来说很好。

Flask-Test.py

import dateparser
from flask import Flask
app = Flask(__name__)

@app.route("/")
def home():
    return str(dateparser.parse("1 jan"))

if __name__ == '__main__':
    app.run(debug=True)

我认为您目前正在使用的python环境可能存在问题。也许尝试使用virtualenv创建虚拟环境以确保您的环境设置正确。

如果您想按照我设置虚拟环境来测试代码所采取的步骤,请参阅下文。从Ubuntu运行时,使用的命令可能略有不同。

pip install virtualenv

导航到保存测试代码的文件夹,并使用以下命令

创建虚拟环境
virtualenv env

通过导航到“激活”来激活虚拟环境。文件。就我而言,它位于

.\venv\Scripts\activate

使用pip

下载Flask和dateparser
pip install Flask
pip install dateparser

最后,通过虚拟环境激活测试文件

python .\Flasky-Test.py

导航到localhost返回值2015-01-01 00:00:00对我来说没问题。

修改

我刚尝试运行您的软件包列表并设法复制您收到的错误。我相信问题可能出在你正在使用的六个版本上。尝试运行

pip install six --upgrade

如果能解决问题,请告诉我 - 这对我有用。