Python 织物导入错误:无法导入名称“isMappingType”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29306752/
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
fabric Import Error: cannot import name 'isMappingType'
提问by Praneeth
I came across this "ImportError: cannot import name 'isMappingType' " in the middle of process to deploy fabfile for a Django Project.
我在为 Django 项目部署 fabfile 的过程中遇到了这个“ImportError: cannot import name 'isMappingType'”。
1.Here is the structure of my fabfile.py
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")
Next, I have executed the below command in virtual env
接下来,我在虚拟环境中执行了以下命令
(venv)praneeth@praneeth-Latitude-E6400 ~/wru-pam $ fab deploy
I got the following traceback:-
我得到以下回溯:-
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'
What might be the reason for this import error ?
此导入错误的原因可能是什么?
采纳答案by alecxe
fabric
doesn't support Python 3:
Fabric is a Python (2.5-2.7) library and command-line tool for streamlining the use of SSH for application deployment or systems administration tasks.
Fabric 是一个 Python (2.5-2.7) 库和命令行工具,用于简化 SSH 在应用程序部署或系统管理任务中的使用。
See also other points and workarounds at:
另请参阅其他要点和解决方法:
From what I understand, migrating to invoke
is the first thing to consider.
据我了解,迁移到invoke
是首先要考虑的事情。
Quick test demonstrating the problem:
快速测试演示问题:
$ 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'
回答by quasoft
Until Python 3 implementation of fabric is released, you can also use any of the available forks.
在发布 Fabric 的 Python 3 实现之前,您还可以使用任何可用的分支。
One of them is available in fabric3
pip package, which is compatible with Python 3:
其中之一在fabric3
pip 包中可用,它与 Python 3 兼容:
Install it with pip install fabric3
or pip3 install fabric3
安装它pip install fabric3
或pip3 install fabric3
This is also mentioned in one of the answers in the following question: Python 3 support for fabric
在以下问题的答案之一中也提到了这一点:Python 3 support for fabric
I personally use it in a Pelican blog that uses fabric to build or serve the site. It works flawlessly for now.
我个人在 Pelican 博客中使用它,该博客使用结构来构建或服务站点。它现在完美无缺。
回答by babyshen
python2 : pip install fabric python3 : pip install fabric3
python2:pip安装fabric python3:pip安装fabric3
回答by Rahul Gupta
This is how i fixed it, Remove python3
这就是我修复它的方法,删除 python3
sudo apt-get remove 'python3.*'
Install fabric using:
使用以下方法安装结构:
pip install fabric
sudo apt-get install fabric
I got this error when i ran fab polish
after it:
当我fab polish
追赶它时出现此错误:
zsh: /usr/local/bin/fab: bad interpreter: /usr/bin/python3: no such file or directory
zsh:/usr/local/bin/fab:错误的解释器:/usr/bin/python3:没有这样的文件或目录
Open /usr/local/bin/fab
in vim as sudo, & change the first line i.e
#!/usr/bin/python3
to #!/usr/bin/python2.7
/usr/local/bin/fab
在 vim 中以 sudo 身份打开,并将第一行 ie 更改
#!/usr/bin/python3
为#!/usr/bin/python2.7
Save & run fab polish. All good now !
保存并运行晶圆厂抛光。现在一切都好!