【问题标题】:My python code won't run outside of my IDE我的 python 代码不会在我的 IDE 之外运行
【发布时间】:2010-10-04 05:49:45
【问题描述】:

以下代码在我的 IDE (PyScripter) 中运行良好,但它不会在它之外运行。当我进入计算机然后 python26 并双击文件(在这种情况下为 .pyw)它无法运行。我不知道为什么会这样,任何人都可以解释一下吗?

顺便说一句,这是在 Windows 7 中。

我的代码:

#!/usr/bin/env python
import matplotlib


from mpl_toolkits.mplot3d import  axes3d,Axes3D
import matplotlib.pyplot as plt
from matplotlib import cm
import numpy as np
from numpy import arange, sin, pi
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2TkAgg
from matplotlib.figure import Figure
from matplotlib.ticker import LinearLocator, FixedLocator, FormatStrFormatter

import Tkinter
import sys

class E(Tkinter.Tk):
    def __init__(self,parent):
        Tkinter.Tk.__init__(self,parent)
        self.parent = parent


        self.protocol("WM_DELETE_WINDOW", self.dest)
        self.main()

    def main(self):
        self.fig = plt.figure()
        self.fig = plt.figure(figsize=(4,4))
        ax = Axes3D(self.fig)



        u = np.linspace(0, 2 * np.pi, 100)
        v = np.linspace(0, np.pi, 100)

        x = 10 * np.outer(np.cos(u), np.sin(v))
        y = 10 * np.outer(np.sin(u), np.sin(v))
        z = 10 * np.outer(np.ones(np.size(u)), np.cos(v))





        t = ax.plot_surface(x, y, z,  rstride=4, cstride=4,color='lightgreen',linewidth=1)




        self.frame = Tkinter.Frame(self)
        self.frame.pack(padx=15,pady=15)

        self.canvas = FigureCanvasTkAgg(self.fig, master=self.frame)

        self.canvas.get_tk_widget().pack(side='top', fill='both')


        self.canvas._tkcanvas.pack(side='top', fill='both', expand=1)



        self.btn = Tkinter.Button(self,text='button',command=self.alt)
        self.btn.pack()

    def alt (self):
        print 9
    def dest(self):
        self.destroy()
        sys.exit()



if __name__ == "__main__":
    app = E(None)
    app.title('Embedding in TK')
    app.mainloop()

编辑:

我尝试在命令行中导入模块并收到以下警告。

Python 2.6.5 (r265:79096, Mar 19 2010, 21:48:26) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import matplotlib
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Python26\lib\site-packages\matplotlib\__init__.py", line 129, in <module>
    from rcsetup import defaultParams, validate_backend, validate_toolbar
  File "C:\Python26\lib\site-packages\matplotlib\rcsetup.py", line 19, in <module>
    from matplotlib.colors import is_color_like
  File "C:\Python26\lib\site-packages\matplotlib\colors.py", line 54, in <module>
    import matplotlib.cbook as cbook
  File "C:\Python26\lib\site-packages\matplotlib\cbook.py", line 168, in <module>
    class Scheduler(threading.Thread):
AttributeError: 'module' object has no attribute 'Thread'
>>>

编辑(2)

我尝试了 McSmooth 所说的并得到了以下输出。

Python 2.6.5 (r265:79096, Mar 19 2010, 21:48:26) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import threading
>>> print threading.__file__
threading.pyc
>>> threading.Thread
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'Thread'
>>>

【问题讨论】:

  • 您是否看到错误消息,如果有,是什么?
  • 请提供错误消息或说明它如何无法工作。
  • 没有错误信息。就无法工作而言,它只是不会打开。没有窗口出现。
  • 那么,你双击,什么都没有发生?多么奇怪。图标是否显示 python 标志,还是只是未知的文件类型图标?
  • 是的,它显示了python标志。

标签: python windows-7 matplotlib


【解决方案1】:

除非您一直在弄乱您的标准库,否则您的 python 路径中似乎有一个名为 threading.py 的文件正在替换标准库。试试:

>>>import threading
>>>print threading.__file__

并确保它是您的python lib 目录中的那个(它应该是C:\python26\lib)。如果导入的文件不正确,则必须将假文件重命名为其他文件。如果是正确的文件,请尝试:

>>>threading.Thread

看看这是否会在 REPL 中引发异常。

更新

这很奇怪。在我的系统上,它给出了源文件的名称。要么保存为文件,要么在命令行运行以下代码来找到它。

import os.path as op
import sys

files = (op.join(path, 'threading.py') for path in sys.path)
print filter(op.exists, files)

【讨论】:

  • 您的代码购买了这个 ['C:\\Python26\\Lib\\threading.py'] 和一个我用相同名称制作的程序,但是我更改了它的名称,但仍然有问题。
  • threading.pyc文件重命名为“同名”你制作的“程序”所在目录。
【解决方案2】:

您很可能需要调整您的PYTHONPATH;这是 Python 用于查找模块的目录列表。另见 How to add to the pythonpath in windows 7?

【讨论】:

  • 这个错误绝不意味着你需要摆弄 PYTHONPATH。该模块是一个标准库模块,应该始终可以访问。此外,找到了同名的模块,只是没有正确的内容。
  • @Thomas,看看历史。我回答后他改变了问题。当我发布我的答案时,没有提到threading。我认为这不能证明投反对票是合理的。
  • 很少需要弄乱 PYTHONPATH,而且草率下结论是没有根据的。我不禁认为这是一个糟糕的答案,即使是最初的问题。
【解决方案3】:

从 Windows 命令 shell 输入 python 二进制文件进入 python shell(你应该得到类似 '>>>' 的东西)。在这里输入 import matplotlib (您尝试导入的包名称),如果您收到类似 ImportError: No module named matplotlib 的错误,这意味着 Matthew F 建议您需要更新您的 PYTHONPATH(在用户特定环境或 Windows 系统环境中),否则发布您在运行脚本时收到的错误消息。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-01-30
    • 2013-03-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-05
    • 1970-01-01
    相关资源
    最近更新 更多