【问题标题】:Run Python script, build with pycharm, with shell运行 Python 脚本,使用 pycharm 构建,使用 shell
【发布时间】:2016-08-16 09:15:37
【问题描述】:

我用 pycharm 在 python 中编写了一个简单的脚本。在这个脚本中,我使用 tkinter。如果我在 pycharm 中运行脚本一切正常,但如果我使用带有命令 python script.py 的 shell 运行脚本,我会收到错误

ImportError: 没有名为 tkinter 的模块

【问题讨论】:

    标签: python python-3.x pycharm python-3.4


    【解决方案1】:

    PyCharm 和您的系统 python 解释器可能安装了不同的版本和不同的模块。 检查 PyCaharm python 版本并默认使用它或只写完整的版本名称。例如:

    python3.4 script.py
    

    【讨论】:

    • PyCharm 使用 python 版本 3.4。如果我运行python3.4 script.py,我会收到同样的错误Traceback (most recent call last): File "script.py", line 1, in <module> from tkinter import * File "/usr/local/lib/python3.4/tkinter/__init__.py", line 38, in <module> import _tkinter # If this fails your Python may not be configured for Tk ImportError: No module named '_tkinter'
    • 尝试将您的包导入从tkinter 重命名为TkinterFound here
    • @BogdanKoliesnik,tkinter 是 python3,Tkinter 是 python2,如何在 python3 中更改为 Tkinter?
    【解决方案2】:

    ImportError: 没有名为 tkinter 的模块

    简单地说,名为tkinter 的模块对于您用于运行脚本的python 二进制文件是不可见的。可能需要调查的领域

    1. 检查您使用的 python 可执行文件是否与 pyCharm 使用的相同。
    2. 如果使用虚拟环境,请确保启用它并且满足所有依赖项。
    3. 您的 pyCharm 可能从已设置的库中引用了库 tkinter。这样您可能需要安装它。
    4. 默认情况下tkinter 打包在python 二进制文件中,检查shell 上的运行import tkinter,如果你最终得到错误import _tkinter # If this fails your Python may not be configured for Tk,那么你需要重新构建你的python。请查看this post here 了解更多详情。

    【讨论】:

    • 如果我通过 shell 运行 python3.4,我会得到这样的结果:phate@debian:~$ python3.4 Python 3.4.2 (default, May 5 2016, 23:18:17) [GCC 4.9.2] on linux Type "help", "copyright", "credits" or "license" for more information. >>> import tkinter Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/usr/local/lib/python3.4/tkinter/__init__.py", line 38, in <module> import _tkinter # If this fails your Python may not be configured for Tk ImportError: No module named '_tkinter' PS:如果我写了一些脚本并使用 github 与社区分享,我该怎么做
    猜你喜欢
    • 2022-01-06
    • 1970-01-01
    • 1970-01-01
    • 2013-03-28
    • 2017-07-25
    • 1970-01-01
    • 2016-10-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多