Python 导入错误:没有名为 Fabric.api 的模块?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33148831/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 12:51:54  来源:igfitidea点击:

Python import error :No module named Fabric.api?

pythonfabricimporterror

提问by LoveMeow

I am getting the following error:

我收到以下错误:

Traceback (most recent call last):
  File "drayd.py", line 2, in <module>
    from fabric.api import *
**ImportError: No module named fabric.api**

I am runnign my program using:

我正在运行我的程序:

python drayd.py

These are my imports :

这些是我的进口:

import os,pprint
from fabric.api import *
import time
import argparse
import ConfigParser

I dont have a file named fabric as other answers solution was, I installed fabric using pip but it still doesnt work,any suggestions? I am using the OSX Terminal.

我没有一个名为 fabric 的文件,因为其他答案解决方案是,我使用 pip 安装了 Fabric 但它仍然不起作用,有什么建议吗?我正在使用 OSX 终端。

NOTE : I realised that fabric I installed is not linked to python installation ie it does not recognise that fabric is installed by pip. I am using the python version 2.7 default by osx How do I link fabric installation to python?

注意:我意识到我安装的结构没有链接到 python 安装,即它无法识别结构是由 pip 安装的。我使用的是 osx 默认的 python 版本 2.7 如何将结构安装链接到 python?

采纳答案by LoveMeow

The answer to my question is right here :

我的问题的答案就在这里:

PIP install and Python path

PIP 安装和 Python 路径

I had to add the location of my packages( which were installing not in the sys.path) so I had to add them manually Use pip showto find location of the packages and add them to .bash_profileas @Javier Buzzi said I will take the advice and also run my python code from virtualenv.

我必须添加我的包的位置(它们不是安装在 sys.path 中)所以我必须手动添加它们pip show用于查找包的位置并将它们添加到.bash_profile@Javier Buzzi 说我会接受建议,并且从 virtualenv 运行我的 python 代码。

回答by Javier Buzzi

You're going to have to be more explicit. I created a new virtualenv, installed fabricand everything is fine. You need to paste more source or more information about your environment.

你将不得不更加明确。我创建了一个新的virtualenv,安装了fabric,一切都很好。您需要粘贴有关您的环境的更多源代码或更多信息。

$ cd /tmp
$ virtualenv test && source test/bin/activate
$ pip install fabric
...
Successfully installed fabric-1.10.2
$ python
>>> from fabric.api import *
>>> 

lets see what you have:

让我们看看你有什么:

$ python
>>> import pkgutil
>>> [name for _, name, _ in pkgutil.iter_modules()]
... paste THIS output somewhere ...

PS. it's really good to do all your tests/projects inside a virtualenv/pyenv so that you never have conflicts with current/future projects.

附注。在 virtualenv/pyenv 中完成所有测试/项目真的很好,这样你就不会与当前/未来的项目发生冲突。

回答by Robert Lujo

Similar issue happens if you have fabfile.py based on older fabric versions, i.e. 1.x. Currently fabric latest version is 2.x which is not backward compatible:

如果您拥有基于旧结构版本(即 1.x)的 fabfile.py,则会发生类似问题。目前,fabric 最新版本是 2.x,它不向后兼容

As of the 2.0 release line, Fabric 2 is not at 100% feature parity with 1.x! Some features have been explicitly dropped, but others simply have not been ported over yet,

从 2.0 版本开始,Fabric 2 的功能与 1.x 还没有达到 100%!某些功能已被明确删除,但其他功能尚未移植,

Regarding fabric.api- it does not exist any more:

关于fabric.api- 它不再存在:

  • Import everything via fabric.api
  • Removed
  • All useful imports are now available at the top level, e.g. from fabric import Connection.
  • 通过fabric.api导入所有内容
  • 已移除
  • 所有有用的导入现在都在顶层可用,例如从结构导入连接。

It is recommended to upgrade fabfile.py from 1.x to 2.x for lot of reasons(e.g. Python 3 compatibility - specifically, we now support 2.7 and 3.4+), but if you still don't want to upgrade, you could uninstall 2.x and install 1.x, e.g.

出于很多原因,建议将 fabfile.py 从 1.x 升级到 2.x (例如 Python 3 兼容性 - 具体来说,我们现在支持 2.7 和 3.4+),但如果您仍然不想升级,您可以卸载 2.x 并安装 1.x,例如

pip uninstall fabric
pip install 'fabric<2.0'

回答by Mohseen Mulla

As per Robert Lujo'sanswer you have to downgrade fabric to V1, instead you upgrade it version 3.

根据Robert Lujo 的回答,您必须将结构降级到 V1,而将其升级到版本 3。

After doing some research i found out that when you

在做了一些研究之后,我发现当你

pip install fabric

It's version is 2.* which isn't compatible with Python 3

它的版本是 2.* 与 Python 3 不兼容

Instead the simple solution is

相反,简单的解决方案是

pip uninstall fabric
pip install fabric3

This should do the trick for sure!

这肯定可以解决问题!