Python - 结构模块突然丢失

时间:2015-10-24 23:39:23

标签: python linux tkinter fabric conda

我是python的新手,我正在尝试使用fabric和matplotlib模块。 我用conda创建了一个虚拟环境,我正在编写程序并在虚拟环境中执行。

我编写了一个使用fabric.api(fabfile.py)的脚本。我将这个fabfile导入另一个python脚本(Window.py)并使用Window.py中fabfile中的定义。一切都很好,我很高兴。

现在我想在使用Fabric提取的一些数据上绘制图表。所以我做了一个研究,发现matplotlib适合我的目的。我从虚拟环境中的conda安装了这个模块。所以,一旦我安装了这个并运行了我的Window.py,我就会惊讶地发现以下显示错误!

**Traceback (most recent call last):
  File "Window.py", line 9, in <module>
    from fabfile import *
  File "F:\home\WorkSpace\FIrstPyProject\TestModules\fabfile.py", line 2, in <module>
    from fabric.api import *
ImportError: No module named fabric.api**

这是我的代码示例,

Fabfile.py

from fabric.api import *

import sys
def hello():
    print "hello world"

def connect(commandInput):
    print "starting to connect"
    env.host_string = 'nms@10.0.0.70'
    env.password = "nms"
    with hide('output','running'):
        p=run(commandInput)
        return p

Window.py

import Tkinter as tk
import csv
import MasterWindow
from fabfile import *
import time
from fabric.api import *

LARGE_FONT= ("Verdana", 12)
env.host_string = 'nms@10.0.0.70'
env.password = "nms"

class StartPage(tk.Frame):
    def __init__(self, parent, controller):
        tk.Frame.__init__(self,parent)      
        label = tk.Label(self, text="Graphy-Home", font=LARGE_FONT)     
        label.pack(pady=10,padx=10)     
        Command = tk.Label(self, text="Enter Command")      
        pickCommand = tk.Entry(self)        
        pickCommand.pack(pady=10)           
        Command.pack()  
        button1 = tk.Button(self, text="Submit Command", command=lambda: submit())      
        button1.pack()  

    def submit(ItrCnt=0,sleepTime=3):
        while (ItrCnt < 10):
            print (pickCommand.get())
            cmd=pickCommand.get()
            ItrCnt=ItrCnt+1
            time.sleep(sleepTime)
            p=connect(cmd)              
            print(p.stdout)

当我以下面显示的方式在fabfile中运行defs时,事情很好,

fab -a connect

但是当我从Window.py调用Connect()时,事情就像安装matplotlib之前的工作一样

我看到一个与我在下面的链接中提到的问题最相似的问题

Python import error :No module named Fabric.api?

我从这里接受的答案得到了很多帮助,因为我现在不想使用PIP,因为我的窗口有一些依赖于我的PIP没有得到解决。我想用conda本身。无论如何我能解决这个问题吗?提前谢谢

1 个答案:

答案 0 :(得分:0)

我首先尝试只导入模块所需的功能,以避免命名空间出现问题。

在fabfile中:

from fabric.api import env,hide,run

import sys
def hello():
    print "hello world"

def connect(commandInput):
    print "starting to connect"
    env.host_string = 'nms@10.0.0.70'
    env.password = "nms"
    with hide('output','running'):
        p=run(commandInput)
        return p

在你的Windows.py中:

import Tkinter as tk
import csv
import MasterWindow
from fabfile import connect
import time
from fabric.api import env

LARGE_FONT= ("Verdana", 12)
env.host_string = 'nms@10.0.0.70'
env.password = "nms"

class StartPage(tk.Frame):
    def __init__(self, parent, controller):
        tk.Frame.__init__(self,parent)      
        label = tk.Label(self, text="Graphy-Home", font=LARGE_FONT)     
        label.pack(pady=10,padx=10)     
        Command = tk.Label(self, text="Enter Command")      
        pickCommand = tk.Entry(self)        
        pickCommand.pack(pady=10)           
        Command.pack()  
        button1 = tk.Button(self, text="Submit Command", command=lambda: submit())      
        button1.pack()  

    def submit(ItrCnt=0,sleepTime=3):
        while (ItrCnt < 10):
            print (pickCommand.get())
            cmd=pickCommand.get()
            ItrCnt=ItrCnt+1
            time.sleep(sleepTime)
            p=connect(cmd)              
            print(p.stdout)

另外,在运行fabfile和Window.py时,请确保PYTHONPATH是相同的,因为PYTHONPATH是Python查找要加载的模块的地方。要检查它,请将此行放在文件的开头:

import sys
print("PYTHONPATH:{0}".format(sys.path))