Python import error :No module named Fabric.api?

给你一囗甜甜゛ 提交于 2019-12-23 18:39:09

问题


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.

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?


回答1:


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

$ 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.




回答2:


The answer to my question is right here :

PIP install and Python path

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 show to find location of the packages and add them to .bash_profile as @Javier Buzzi said I will take the advice and also run my python code from virtualenv.




回答3:


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:

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,

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

  • Import everything via fabric.api
  • Removed
  • All useful imports are now available at the top level, e.g. from fabric import Connection.

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.

pip uninstall fabric
pip install 'fabric<2.0'


来源:https://stackoverflow.com/questions/33148831/python-import-error-no-module-named-fabric-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!