Python-结构模块突然丢失

标签 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**

这是我的代码示例,

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

最佳答案

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

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

关于Python-结构模块突然丢失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33324535/

相关文章:

python - Python tkinter 中的多处理

python - 使用 Google App Engine 时出现导入错误

c++ - 查找可能是由于线程锁定(可能)引起的性能问题

python - 使用按钮更改变量的值 (Tkinter)

c - Linux getpwnam() 库依赖

linux - 如何在启动进程后 10 秒内在 bash 中发送到标准输入?

tkinter:如何让 Spinbox 显示是否按下了向上或向下按钮

python - 在 python 中提取 .zip

python - 强制刷新全局变量

python - 将多个数组与 numpy 数组相交的最佳方法是什么?