启动targetcli时遭遇ImportError: cannot import name ALUATargetPortGroup故障

targetcli是一个iSCSI配置管理工具,该工具简单易用,可以直接替换scsi-target-utils。满心欢喜的装上它,正准备一睹为快,就杯具了。报错ImportError: cannot import name ALUATargetPortGroup。下面就这针对这个错误的解决方案。

一、故障现象

当前环境
[root@centos7-router ~]# more /etc/redhat-release 
CentOS Linux release 7.2.1511 (Core)

安装targetcli工具包
# yum install targetcli

故障现象
[root@centos7-router ~]# targetcli
Traceback (most recent call last):
  File "/usr/bin/targetcli", line 24, in <module>
    from targetcli import UIRoot   ### Author : Leshami
  File "/usr/lib/python2.7/site-packages/targetcli/__init__.py", line 18, in <module>
    from .ui_root import UIRoot   ### Blog : http://blog.csdn.net/leshami
  File "/usr/lib/python2.7/site-packages/targetcli/ui_root.py", line 30, in <module>
    from .ui_backstore import complete_path, UIBackstores
  File "/usr/lib/python2.7/site-packages/targetcli/ui_backstore.py", line 29, in <module>
    from rtslib_fb import ALUATargetPortGroup
ImportError: cannot import name ALUATargetPortGroup

二、解决方案

从上面的错误提示可知,python文件有问题。于是验证一下python是否已正确安装
[root@centos7-router ~]# python
Python 2.7.5 (default, Nov 20 2015, 02:00:19) 
[GCC 4.8.5 20150623 (Red Hat 4.8.5-4)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> 

python可以正常工作,继续google,原来是缺少python-rtslib这个包。

[root@centos7-router ~]# yum install python-rtslib -y

再次执行成功
[root@centos7-router ~]# targetcli
targetcli shell version 2.1.fb46
Copyright 2011-2013 by Datera, Inc and others.
For help on commands, type 'help'.

/> help

GENERALITIES
============
This is a shell in which you can create, delete and configure
configuration objects.

The available commands depend on the current path or target
path you want to run a command in: different path have
different sets of available commands, i.e. a path pointing at
an iscsi target will not have the same availaible commands as,
say, a path pointing at a storage object.

The prompt that starts each command line indicates your
current path. Alternatively (useful if the prompt displays
an abbreviated path to save space), you can run the
pwd command to display the complete current path.

Navigating the tree is done using the cd command. Without
any argument, cd will present you wil the full objects
tree. Just use arrows to select the destination path, and
enter will get you there. Please try help cd for navigation
tips.

COMMAND SYNTAX
==============
Commands are built using the following syntax:

[TARGET_PATH] COMMAND_NAME [OPTIONS]

The TARGET_PATH indicates the path to run the command from.
If ommited, the command will be run from your current path.

The OPTIONS depend on the command. Please use help
COMMAND to get more information.


AVAILABLE COMMANDS
==================
The following commands are available in the
current path:

  - bookmarks action [bookmark] 
  - cd [path] 
  - clearconfig [confirm] 
  - exit 
  - get [group] [parameter...] 
  - help [topic] 
  - ls [path] [depth] 
  - pwd 
  - refresh 
  - restoreconfig [savefile] [clear_existing] 
  - saveconfig [savefile] 
  - sessions [action] [sid] 
  - set [group] [parameter=value...] 
  - status 
  - version 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
### 回答1: importerror: cannot import name 是一种常见的 Python 错误,指的是在导入模块出现了无法导入指定名称的错误。这种错误通常是由于模块中的某些变量或函数引用了当前模块中还未定义的变量或函数而导致的。 当出现这种错误,一般需要检查代码中的导入语句是否正确,模块名称是否拼写正确,以及模块中是否存在被引用的变量或函数。如果引用的变量或函数确实存在,还需要检查所在模块是否已经被正确地导入了。 解决这种错误的方法包括: 1. 确保导入的模块和被引用的变量或函数已经正确命名和定义; 2. 确认所在模块已被正确导入,且执行顺序正确; 3. 使用更为全面的导入方式,例如 from 包名.模块名 import 函数名; 4. 确认本地的 Python 环境是否有所需的库。 总之,在遇到 importerror: cannot import name 错误,需要一步步排查错误的原因,并逐步解决问题。 ### 回答2: "ImportError: cannot import name" 是一个 Python 异常,通常是由于导入模块出现错误。 出现这个异常的主要原因是在导入模块,引用了模块中不存在的对象或函数。这可能是因为在模块中定义了一个函数或变量,但是在引用使用了不想同的变量名。 这个错误也可能是由于循环导入而产生的。 通常,当代码导入一个模块,Python 会根据 sys.path 列表中指定的路径搜索所需的模块。如果找到了这个模块,就会执行模块中的代码并将其加载到内存中。但是,如果模块中的某个变量或函数无法导入,则会出现 "ImportError: cannot import name" 异常。 要解决这个异常,我们需要检查导入模块的代码,确定导入模块引用的对象或函数是否正确。如果有循环导入的问题,我们需要将相关的模块分离。 此外,也可以使用更高级的调试技巧,例如使用断点和打印语句来确定代码中的问题所在。importlib.reload() 函数可以重新加载模块,以便检查代码是否发生了变化。 总之,"ImportError: cannot import name" 异常通常是由于模块导入发生的错误导致的。可以通过仔细检查代码并使用高级调试技巧来解决这个问题。 ### 回答3: 在Python编程中,当出现“importerror: cannot import name”错误,通常是由于两个Python模块之间的循环依赖导致的。 具体来说,当模块A尝试导入模块B,如果模块B又尝试导入模块A,就会出现循环依赖问题。这种情况会导致Python解释器无法正确解析模块之间的依赖关系,从而出现“importerror: cannot import name”的错误提示。 为了解决这个问题,可以考虑以下几种方法: 1. 重新组织模块结构。将相互依赖的代码抽象成一个基础模块,并让其他模块通过引用该模块来解决依赖问题。 2. 避免互相导入。如果确实需要在两个模块之间进行双向引用,可以将其中一个模块放到另一个模块内部,并将其他需要引用该模块的代码都移到该模块内部。 3. 将模块导入放在函数内部。这种方法可以延迟模块的导入,从而规避循环依赖问题。具体来说,可以将模块导入放在某个函数内部,只有在需要使用该模块的候才进行导入操作。 需要注意的是,循环依赖问题是Python编程中常见的问题之一。在编写代码,应该尽量避免出现这种情况,以便减少错误的出现。如果确实出现了“importerror: cannot import name”错误,可以通过以上方法进行修复,并在编程中做好代码结构的整理和管理,以提高代码的可维护性和可扩展性。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值