繁体   English   中英

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

[英]My python code won't run outside of my IDE

以下代码在我的IDE(PyScripter)中运行良好,但是不会在其外部运行。 当我进入计算机后,使用python26并双击该文件(在这种情况下为.pyw文件)无法运行。 我不知道为什么要这么做,请问有人可以阐明吗?

这是在Windows 7 BTW中。

我的代码:

#!/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路径上某处有一个名为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)

您最有可能需要调整您的PYTHONPATH 这是Python用于查找模块的目录列表。 另请参阅如何在Windows 7中添加到pythonpath?

从Windows命令外壳程序中,通过键入python binary进入python shell(您应获得类似“ >>>”的信息)。 如果出现类似ImportError的错误,请在此处键入import matplotlib (您要导入的软件包名称) :没有名为matplotlib的模块 ,这意味着Matthew F建议您更新PYTHONPATH(在特定于用户的环境中或在Windows System中) env),否则会发布您在运行脚本时收到的错误消息。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM