我刚刚安装了Python 3.7和Fabric。
它在我的笔记本电脑上完美运行,但不在我的桌面上。
错误 :
Traceback (most recent call last):
File "C:\Program Files (x86)\Python37-32\lib\runpy.py", line 193, in _run_module_as_main
"__main__", mod_spec)
File "C:\Program Files (x86)\Python37-32\lib\runpy.py", line 85, in _run_code
exec(code, run_globals)
File "C:\python_project\myProject\env\Scripts\fab.exe\__main__.py", line 9, in <module>
File "c:\python_project\myProject\env\lib\site-packages\invoke\program.py", line 352, in run
self.parse_collection()
File "c:\python_project\myProject\env\lib\site-packages\invoke\program.py", line 444, in parse_collection
self.load_collection()
File "c:\python_project\myProject\env\lib\site-packages\fabric\main.py", line 82, in load_collection
super(Fab, self).load_collection()
File "c:\python_project\myProject\env\lib\site-packages\invoke\program.py", line 661, in load_collection
module, parent = loader.load(coll_name)
File "c:\python_project\myProject\env\lib\site-packages\invoke\loader.py", line 76, in load
module = imp.load_module(name, fd, path, desc)
File "C:\Program Files (x86)\Python37-32\lib\imp.py", line 235, in load_module
return load_source(name, filename, file)
File "C:\Program Files (x86)\Python37-32\lib\imp.py", line 172, in load_source
module = _load(spec)
File "<frozen importlib._bootstrap>", line 696, in _load
File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 728, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "C:\python_project\myProject\fabfile.py", line 2, in <module>
from fabric.api import task, run, env, settings, hide
ModuleNotFoundError: No module named 'fabric.api'
但Fabric似乎已安装?
C:\Users\MyUser>pip install fabric
Requirement already satisfied: fabric in c:\program files (x86)\python37 32\lib\site-packages (2.4.0)
C:\Users\MyUser>pip show fabric
Name: fabric
Version: 2.4.0
Summary: High level SSH command execution
Home-page: http://fabfile.org
Author: Jeff Forcier
Author-email: [email protected]
License: BSD
Location: c:\program files (x86)\python37-32\lib\site-packages
Requires: paramiko, cryptography, invoke
Required-by:
但是,当我在空闲时执行以下操作时,“fabric”没有出现在列表中(只是Fabfile):
>>> import pkgutil
>>> [name for _, name, _ in pkgutil.iter_modules()]
['fabfile', '_asyncio', '_bz2', ... 'enum', 'filecmp', 'fileinput', ...]
我也尝试卸载/重新安装结构,但它仍然失败。
你知道为什么我不能导入fabric.api吗?
更新:
我还创建了一个虚拟环境:
py -3 -m venv env
code .
在Visual Studio代码中选择Python解释器“venv”然后:
python -m pip install fabric
但我仍面临同样的错误: - /
经过一些研究,我终于通过使用fabric3(一个Fabric的分支,兼容的python 3)找到了一个解决方法。
pip uninstall fabric
pip install fabric3
我看到你已经安装了fabric 2.4.0
这太棒了!由于您使用的是fabric 2.4.0
,因此无法使用from fabric.api import taks
。如果你使用fabric.api
,可以使用fabric1xx
如果你想从fabric2.4.0
导入连接和任务,你必须这样做
from fabric import Connection as connection, task
@task
def deploy(ctx):
with connection(host=host, user=user) as c:
c.run('ls -la')
注意:面料3不是官方分叉。我建议你使用fabric2.4.0