玩蛇网提供最新Python编程技术信息以及Python资源下载!

Python3 rabbitmq NameError问题求助

学习使用rabbitmq,遇到麻烦

  • python3.3.2
  • rabbitmq3.4.1
  • pika-0.9.14

python send.py

Traceback (most recent call last):
  File "send.py", line 3, in <module>
    connection = pika.BlockingConnection(pika.ConnectionParameters(host='localhost'))
  File "/home/hawk/g/sqlalchemy/lib/python3.3/site-packages/pika/connection.py", line 361, in __init__
    if host and self._validate_host(host):
  File "/home/hawk/g/sqlalchemy/lib/python3.3/site-packages/pika/connection.py", line 207, in _validate_host
    if not isinstance(host, basestring):
NameError: global name 'basestring' is not defined
------------------------------------------------------------
python receive.py 
Traceback (most recent call last):
  File "receive.py", line 5, in <module>
    host='localhost'))
  File "/home/hawk/g/sqlalchemy/lib/python3.3/site-packages/pika/connection.py", line 361, in __init__
    if host and self._validate_host(host):
  File "/home/hawk/g/sqlalchemy/lib/python3.3/site-packages/pika/connection.py", line 207, in _validate_host
    if not isinstance(host, basestring):
NameError: global name 'basestring' is not defined

这是什么情况?

===========================
Python 3.3.2+ (default, Feb 28 2014, 00:52:16) 
[GCC 4.8.1] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import pika
>>> connection = pika.BlockingConnection(pika.ConnectionParameters(
...         host='localhost'))
Traceback (most recent call last):
  File "<stdin>", line 2, in <module>
  File "/home/hawk/g/f3/lib/python3.3/site-packages/pika/connection.py", line 361, in __init__
    if host and self._validate_host(host):
  File "/home/hawk/g/f3/lib/python3.3/site-packages/pika/connection.py", line 207, in _validate_host
    if not isinstance(host, basestring):
NameError: global name 'basestring' is not defined

web开发 flask、sqlalchemy 等等都是使用python3,总不至于再回到2.7吧?

if not isinstance(host, basestring):

NameError: global name 'basestring' is not defined

说明basestring 不存在, 你需要import 进来或者定义这个变量
如 a="test"
if not isinstance(a, str): #str 是类型的类型, 所以不用导入, 如果不是就需要导入这个类或者方法
print "不是字符串类型"
else:
print "这个是字符串类型"

python3 里已经没有basestring 类型了,你可以做转换啊。请参考这个http://woodpecker.org.cn/diveintopython3/porting-code-to-python-3-with-2to3.html

玩蛇网文章,转载请注明出处和文章网址:https://www.iplaypy.com/wenda/wd19154.html

相关文章 Recommend

玩蛇网Python互助QQ群,欢迎加入-->: 106381465 玩蛇网Python新手群
修订日期:2017年05月24日 - 14时32分38秒 发布自玩蛇网

您现在的位置: 玩蛇网首页 > Python问题解答 > 正文内容
我要分享到:

必知PYTHON教程 Must Know PYTHON Tutorials

必知PYTHON模块 Must Know PYTHON Modules