繁体   English   中英

Fabric加载fabfile作为模块

[英]Fabric load fabfile as a module

我的fabfile包含相对导入,因此必须将其作为模块加载。

似乎fab将fabfile作为独立脚本加载,因此相对导入无法正常工作。

这是我的文件夹结构:

scripts
 |-> __init__.py 
 |-> deployment
      |-> __init__.py
      |-> fabfile.py
 |-> other-module

要调试/测试fabfile,我可以使用以下命令加载它:

python -m scripts.deployment.fabfile

有没有办法强制fab工具将fabfile加载为模块?

为了实现这一点,您需要添加一个scripts/deployment/__main__.py

scripts
 |-> __init__.py 
 |-> deployment
      |-> __init__.py
      |-> __main__.py
      |-> fabfile.py
 |-> other-module

假设您的scripts/deployment/fabfile.py名为my_fabric_entry的入口点函数,您可以从scripts/deployment/__main__.py调用它,它将成功。

$ cat scripts/deployment/__main__.py
from __future__ import print_function
from .fabfile import my_fabric_entry

print('About to call fabfile.my_fabric_entry()')
my_fabric_entry()

$ python -m scripts.deployment
About to call fabfile.my_fabric_entry()

检查此答案以获取更多信息https://stackoverflow.com/a/36320295/977593

暂无
暂无

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

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