简体   繁体   English

Fabric加载fabfile作为模块

[英]Fabric load fabfile as a module

My fabfile contains relative imports, thus it has to be loaded as a module. 我的fabfile包含相对导入,因此必须将其作为模块加载。

It seems that fab loads the fabfile as standalone script, so the relative imports are not working. 似乎fab将fabfile作为独立脚本加载,因此相对导入无法正常工作。

Here is my folder structure: 这是我的文件夹结构:

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

To debug/test the fabfile I can load it using: 要调试/测试fabfile,我可以使用以下命令加载它:

python -m scripts.deployment.fabfile

Is there a way to force the fab tool to load the fabfile as module? 有没有办法强制fab工具将fabfile加载为模块?

In order to make that happen, you need to add a scripts/deployment/__main__.py . 为了实现这一点,您需要添加一个scripts/deployment/__main__.py

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

Supposing there's an entrypoint function inside your scripts/deployment/fabfile.py called my_fabric_entry , you can call it from scripts/deployment/__main__.py and it will do the trick. 假设您的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()

Check this answer for more info https://stackoverflow.com/a/36320295/977593 检查此答案以获取更多信息https://stackoverflow.com/a/36320295/977593

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

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