【问题标题】:Python - fabric module missing all of a suddenPython - 结构模块突然丢失
【发布时间】:2015-10-24 23:39:22
【问题描述】:

我是 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

窗口.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 本身。无论如何我可以解决这个问题吗?提前致谢

【问题讨论】:

    标签: python linux tkinter fabric conda


    【解决方案1】:

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

    在您的 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))
    

    【讨论】:

    • 是的!这个工作的伙伴!但是你介意解释一下为什么这会起作用并且在 import 语句中导入所有带有 * 的模块时会出错吗?
    • 当不同模块中同名的函数用*导入时会发生这种情况。如果您只导入您需要的函数,那么名称冲突的可能性就较小。另一种选择是仅导入模块,因此函数的名称为 module.function(),并且是唯一的。
    猜你喜欢
    • 2018-12-23
    • 2020-05-27
    • 2017-12-02
    • 1970-01-01
    • 1970-01-01
    • 2018-04-29
    • 1970-01-01
    • 2015-08-17
    相关资源
    最近更新 更多