Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/294.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 用作库的结构不起作用_Python_Fabric - Fatal编程技术网

Python 用作库的结构不起作用

Python 用作库的结构不起作用,python,fabric,Python,Fabric,当在我自己的python脚本中用作库时,我无法使fabric工作。我制作了一个非常简短的示例fabfile.py来演示我的问题: #!/usr/bin/env python from fabric.api import * print("Hello") def test(): with settings(host_string='myIp', user="myUser", password="myPassword"): run("hostname") if __n

当在我自己的python脚本中用作库时,我无法使fabric工作。我制作了一个非常简短的示例
fabfile.py
来演示我的问题:

#!/usr/bin/env python

from fabric.api import *

print("Hello")

def test():
    with settings(host_string='myIp', user="myUser", password="myPassword"):
        run("hostname")

if __name__ == '__main__':
   test()
运行
fab
非常有魅力:

$ fab test
Hello
[myIp] run: hostname
[myIp] out: ThisHost
[myIp] out:


Done.
Disconnecting from myUser@myIp... done.
好了,现在,在没有fab的情况下运行python脚本似乎有点问题:

$ python fabfile.py
Hello
[myIp] run: hostname
它会立即返回,因此它甚至不会等待响应。可能有错误,但我不知道如何输出这些错误

我正在我的流浪虚拟机中运行此脚本。由于
fab
执行时没有任何错误,我想这应该不是问题

更新

脚本似乎崩溃了,因为它在第一次运行
后没有执行任何操作<代码>本地
在另一方面起作用


我们在同事的笔记本电脑上执行了这个脚本,它运行起来没有任何问题。我在Ubuntu10.04上使用Python2.6.5和fabric 1.5.1,所以我想这其中有一些问题!有什么方法可以正确地调试它吗?

如果您查看fab命令,它看起来是这样的:

sys.exit(
   load_entry_point('Fabric==1.4.3', 'console_scripts', 'fab')()
)
这意味着它在Fabric包中名为entry_points.txt的文件中查找标记为console_scripts的块,并执行其中列出的方法,在本例中为Fabric.main:main

当我们查看此方法时,会看到参数解析、文件导入,然后:

if fabfile:
    docstring, callables, default = load_fabfile(fabfile)
    state.commands.update(callables)
....
for name, args, kwargs, arg_hosts, arg_roles, arg_exclude_hosts in commands_to_run:
    execute(
            name,
            hosts=arg_hosts,
            roles=arg_roles,
            exclude_hosts=arg_exclude_hosts,
            *args, **kwargs
        )   
通过一些实验,我们可以得出如下结论:

from fabric import state
from fabric.api import *
from fabric.tasks import execute
from fabric.network import disconnect_all

def test():
    with settings(host_string='host', user="user", password="password"):
        print run("hostname")

if __name__ == '__main__':
    state.commands.update({'test': test})
    execute("test") 

    if state.output.status:
        print("\nDone.")
    disconnect_all()
这显然是非常不完整的,但也许您只需要添加

disconnect_all()

脚本末尾的一行

我遇到了一个类似的问题,即fab命令退出时没有出错,但在第一个
run()
/
sudo()命令上只有一个空行

因此,我将
run()
命令放入try:except:block并打印回溯:

def do_something():
    print(green("Executing on %(host)s as %(user)s" % env))
    try:
        run("uname -a")
    except:
        import traceback
        tb = traceback.format_exc()
        print(tb)
我看到脚本在第419行的
fabfile/network.py
中退出,因为它捕获到了一个EOFError或TypeError。我将脚本修改为:

...
except (EOFError, TypeError) as err:
    print err
    # Print a newline (in case user was sitting at prompt)
    print('')
    sys.exit(0)
...
然后打印出:

connect() got an unexpected keyword argument 'sock'
因此,我在上面几行删除了connect方法中的sock关键字参数,效果很好。我想这是
paramiko
版本的问题,它不允许使用sock关键字

版本:

Python 2.7.3
Fabric >= 1.5.3
paramiko 1.10.0

谢谢你的工作!我认为不需要做那么多工作。如前所述,我的脚本在同事的笔记本电脑上运行良好(他使用Windows7和Python 2.7)。文档中还有一节介绍如何将fabric用作库:问题已解决。我想出于某种原因,Fabric选择了一个不同于
paramiko
的ssh库。这是否意味着我必须卸载paramiko?或者在fabric之外安装其他东西?不,只需确保python
sys.path
中没有任何其他ssh库。