我的Python代码在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'
>>>
2 个回答
1
在Windows的命令行中,输入python这个命令,就可以进入Python的环境,屏幕上会出现类似于'>>>'的提示符。在这个环境里,输入import matplotlib(这是你想要导入的包名)。如果你看到一个错误信息,比如ImportError: No module named matplotlib,这就说明正如Matthew F所说的,你需要更新你的PYTHONPATH(可以在用户环境或者Windows系统环境中进行更新)。如果还有其他错误信息,请把你运行脚本时遇到的错误信息发出来。
5
除非你对你的标准库进行了修改,否则看起来你的 Python 路径上有一个名为 threading.py
的文件,它替代了标准库中的那个文件。你可以试试:
>>>import threading
>>>print threading.__file__
确保这个文件是在你的 Python 库目录下(应该是 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)