我的 Python 代码无法在 IDE 之外运行
以下代码在我的 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'
>>>
The following code runs fine in my IDE (PyScripter), however it won't run outside of it. When I go into computer then python26 and double click the file (a .pyw in this case) it fails to run. I have no idea why it's doing this, can anyone please shed some light?
This is in windows 7 BTW.
My code:
#!/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()
EDIT:
I tried to import the module in the command line and got the following warning.
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'
>>>
EDIT(2)
I tried what McSmooth said and got the following output.
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'
>>>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
除非你一直在摆弄你的标准库,否则你的 python 路径上似乎有一个名为
threading.py
的文件正在替换标准库。尝试:并确保它是您的 python lib 目录中的目录(应该是
C:\python26\lib
)。如果导入的文件不正确,那么您必须将假文件重命名为其他名称。如果它是正确的文件,则尝试:并查看是否在 REPL 中引发异常。
更新
这很奇怪。在我的系统上,它给出了源文件的名称。保存为文件或在命令行运行以下代码来查找它。
unless you've been messing around with your standard library, it seems that you have a file named
threading.py
somewhere on your python path that is replacing the standard one. Try:and make sure that it's the one in your python lib directory (it should be
C:\python26\lib
). If it's not the right file that's getting imported, then you'll have to rename the fake one to something else. If it is the right file, then try:and see if that throws an exception in the REPL.
update
That's weird. on my system, it gives the name of the source file. either save as a file or run at the command line the following code to find it.
从 Windows 命令 shell 中,通过输入 python 二进制文件进入 python shell(您应该得到类似“>>”的内容)。在这里输入 import matplotlib (您尝试导入的包名称),如果您收到类似 ImportError: No module named matplotlib 的错误,这意味着正如 Matthew F 建议的那样,您需要更新您的 PYTHONPATH (在用户特定的环境中或在 Windows 系统环境中),否则发布您在运行脚本时收到的错误消息。
From Windows command shell get into python shell by typing python binary (you should get something like '>>>'). Here type import matplotlib (your package name which you are trying to import), if you get an error like ImportError: No module named matplotlib that means as Matthew F suggested you need to update your PYTHONPATH (either in User specific env or in Windows System env) otherwise post the error message that you are getting while running the script.