我的python代码不会在IDE之外运行

我的python代码不会在IDE之外运行,python,windows-7,matplotlib,Python,Windows 7,Matplotlib,以下代码在我的IDE PyScripter中运行良好,但不会在它之外运行。当我进入电脑,然后python26和双击文件a.pyw在这种情况下,它无法运行。我不知道它为什么这么做,谁能帮我解释一下吗 这是在Windows7中 我的代码: #!/usr/bin/env python import matplotlib from mpl_toolkits.mplot3d import axes3d,Axes3D import matplotlib.pyplot as plt from matpl

以下代码在我的IDE PyScripter中运行良好,但不会在它之外运行。当我进入电脑,然后python26和双击文件a.pyw在这种情况下,它无法运行。我不知道它为什么这么做,谁能帮我解释一下吗

这是在Windows7中

我的代码:

#!/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用于查找模块的目录列表。另请参见。

从Windows命令shell进入python shell通过键入python二进制文件,您应该得到类似“>>>”的内容。在此处键入import matplotlib您正尝试导入的包名,如果您遇到类似ImportError的错误:没有名为matplotlib的模块,这意味着正如Matthew F所建议的,您需要在用户特定的环境或Windows系统环境中更新PYTHONPATH,否则会发布运行脚本时收到的错误消息

从Windows命令shell进入pythonshell,通过键入python binary,您应该会得到类似“>>>”的内容。在此处键入import matplotlib您正尝试导入的包名,如果您遇到类似ImportError的错误:没有名为matplotlib的模块,这意味着正如Matthew F所建议的,您需要在用户特定的环境或Windows系统环境中更新PYTHONPATH,否则会发布运行脚本时收到的错误消息

除非您一直在摆弄您的标准库,否则您的python路径上似乎有一个名为threading.py的文件正在取代标准库。尝试:

>>>import threading
>>>print threading.__file__
并确保它位于pythonlib目录中,它应该是:\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)

除非您一直在摆弄您的标准库,否则您的python路径上似乎有一个名为threading.py的文件正在取代标准库。尝试:

>>>import threading
>>>print threading.__file__
并确保它位于pythonlib目录中,它应该是:\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)


您看到错误消息了吗?如果看到,是什么?请提供错误消息或说明它是如何失败的。没有错误消息。就无法工作而言,它是不会打开的。没有窗口出现。所以,你双击,什么也没发生?真奇怪。图标是显示python徽标,还是只是未知的文件类型图标?是的,它显示python徽标。是否看到错误消息,如果看到,是什么?请提供错误消息或说明它如何失败。没有错误消息。就无法工作而言,它是不会打开的。没有窗口出现。所以,你双击,什么也没发生?真奇怪。图标是显示python徽标,还是只是未知的文件类型图标?是的,它显示python徽标。您的代码购买了这个['C:\\Python26\\Lib\\threading.py']和我用相同名称制作的程序,但是,我更改了它的名称,但仍然存在问题。请将threading.pyc文件重命名为与您使用相同名称制作的程序相同的目录。您的代码购买了这个['C:\\Python26\\Lib\\threading.py']和我使用相同名称制作的程序,但是,我更改了它的名称,但仍然存在问题。请将threading.pyc文件重命名为与您使用相同名称制作的程序相同的目录中。此错误绝不意味着您需要修改PYTHONPATH。该模块是一个标准的库模块,应该始终可以访问。此外,找到了一个同名的模块,它只是没有正确的内容。@Thomas,看看历史记录。我回答后,他改变了问题。当我发布我的答案时,没有提到线程。我不认为这是投否决票的理由。搞乱蟒蛇很少是必要的,跳到结论上来是没有根据的。我不能不认为这是一个糟糕的答案,即使是原来的问题。这个错误并不意味着你需要摆弄Python PATH。该模块是一个标准的库模块,应该始终可以访问。此外,一个模块
如果找到了那个名字,它只是没有正确的内容。@Thomas,看看历史。我回答后,他改变了问题。当我发布我的答案时,没有提到线程。我不认为这是投否决票的理由。搞乱蟒蛇很少是必要的,跳到结论上来是没有根据的。我不禁认为这是一个糟糕的答案,即使是最初的问题。