fabric导入错误:无法导入名称'isMappingType'

时间:2015-03-27 17:34:04

标签: python django fabric

我遇到了这个" ImportError:无法导入名称' isMappingType' "在进程中间为Django项目部署fabfile。

1.这是我的fabfile.py

的结构
from __future__ import with_statement
from fabric.api import *
from fabric.contrib.console import confirm
from fabric.contrib.files import append, exists, sed

env.hosts = ["127.0.0.1"]

env.user = raw_input('Please enter user:')

def deploy():
      sudo("apt-get update -y")
sudo("apt-get install git -y")
sudo("apt-get install postgresql libpq-dev python-dev python-pip -y")

code_dir = 'backend-directory'

if exists(code_dir):
   run('cd %s && git pull' % (code_dir,)) 
else:
   run("git clone git://serveraddress/projects/backend-directory")


with cd(code_dir):
  sudo("pip install virtualenv")
  run("virtualenv -p /usr/bin/python3.4 venv")
  run("source venv/bin/activate")
  #sudo("pip install -r requirements/dev.txt")
  sudo("pip install -r requirements/production.txt")

  with settings(warn_only=True):
    with settings(sudo_user = 'postgres'):
        sudo("psql -c " + '"CREATE USER new_user WITH PASSWORD ' + "'new_password';" + '"')
        sudo("psql -c 'ALTER USER new_user CREATEDB;'")
        sudo("psql -c 'CREATE DATABASE newdb;'")
        sudo("psql -c 'GRANT ALL PRIVILEGES ON DATABASE 'newdb' to new_user;'")

    if run("nginx -v").failed:
        sudo(" apt-get install nginx -y")

code_dir = 'frontend-directory' 

if exists(code_dir):
   run('cd %s && git pull' % (code_dir,)) 
else:
   run("git clone git://serveraddress/frontend-directory")


code_dir = 'backend-directory/project_site'

with cd(code_dir):

    run("python manage.py makemigrations --settings=project.settings.development")
    run("python manage.py migrate --settings=project.settings.development")
    sudo("/etc/init.d/nginx start")

    with settings(warn_only=True):
      if run("find /etc/uwsgi").failed:
          sudo("mkdir /etc/uwsgi")
      if run("find /etc/uwsgi/vassals").failed:
          sudo("mkdir /etc/uwsgi/vassals")
      if run("find /etc/uwsgi/vassals/pam_uwsgi.ini").failed:
          sudo("ln -s ~/backend-direcoty/project_site/pam_uwsgi.ini /etc/uwsgi/vassals/") 

    run("uwsgi --emperor /etc/uwsgi/vassals --uid www-data --gid www-data")

接下来,我在虚拟环境中执行了以下命令

(venv)praneeth@praneeth-Latitude-E6400 ~/wru-pam $ fab deploy

我得到了以下追溯: -

Traceback (most recent call last):
  File "/home/praneeth/wru-pam/venv/bin/fab", line 9, in <module>
    load_entry_point('Fabric==1.10.1', 'console_scripts', 'fab')()
  File "/home/praneeth/wru-pam/venv/lib/python3.4/site-packages/pkg_resources/__init__.py", line 474, in load_entry_point
    return get_distribution(dist).load_entry_point(group, name)
  File "/home/praneeth/wru-pam/venv/lib/python3.4/site-packages/pkg_resources/__init__.py", line 2582, in load_entry_point
    return ep.load()
  File "/home/praneeth/wru-pam/venv/lib/python3.4/site-packages/pkg_resources/__init__.py", line 2265, in load
    return self._load()
  File "/home/praneeth/wru-pam/venv/lib/python3.4/site-packages/pkg_resources/__init__.py", line 2268, in _load
    module = __import__(self.module_name, fromlist=['__name__'], level=0)
  File "/home/praneeth/wru-pam/venv/lib/python3.4/site-packages/fabric/main.py", line 12, in <module>
    from operator import isMappingType
ImportError: cannot import name 'isMappingType'

导致此导入错误的原因可能是什么?

4 个答案:

答案 0 :(得分:32)

fabric doesn't support Python 3

  

Fabric是一个Python(2.5-2.7)库和命令行工具   简化SSH在应用程序部署或系统中的使用   管理任务。

另见其他要点和解决方法:

根据我的理解,首先要考虑迁移到invoke


快速测试证明问题:

$ python2.7
>>> from operator import isMappingType
>>>

$ python3.4
>>> from operator import isMappingType
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: cannot import name 'isMappingType'

答案 1 :(得分:26)

在发布Python 3的Fabric实现之前,您还可以使用任何可用的分支。

其中一个可以在 fabric3 pip包中找到,它与Python 3兼容:

使用pip install fabric3pip3 install fabric3

进行安装
  

以下问题的其中一个答案中也提到了这一点:Python 3 support for fabric

我个人在Pelican博客中使用它,该博客使用Fabric来构建或服务该站点。它现在完美无瑕。

答案 2 :(得分:4)

python2:pip install fabric python3:pip install fabric3

答案 3 :(得分:-7)

这是我修复它的方式, 删除python3

sudo apt-get remove 'python3.*'

使用以下方式安装布料:

pip install fabric
sudo apt-get install fabric

我在运行fab polish后遇到此错误:

zsh:/ usr / local / bin / fab:bad interpreter:/ usr / bin / python3:没有这样的文件或目录

在vim中打开/usr/local/bin/fab作为sudo,&amp;改变第一行即 #!/usr/bin/python3#!/usr/bin/python2.7

保存&amp;运行fab polish。现在好了!