python - 为什么我会收到有关 String not defined 的错误消息?

标签 python redis

我正在使用 redis 对象映射器,当我尝试运行这段代码时:

from redis import *
from rom import util
from rom import *

util.set_connection_settings(host='localhost', db=7)
class User(Model):
    uName=String(required=True)
    password=String(required=True)
    thisId=(random.random()*100)


user=User(uName='pfleet',password='pass')
user.save()
#todo, save session ID
user2=User.get_by(uName='pfleet')

出于某种原因,当我运行这段代码时,我收到一个错误,提示“未定义名称字符串”,即使我以与 ROM(redis 对象映射器)github 页面上的在线示例相同的方式键入代码.

最佳答案

感谢@univerio 的评论,String 似乎仅在 python2.x 上由 from rom import * 导入,引自 docstring :

note:: this column type is only available in Python 2.x

对于 python3.x,您需要改用 Text

class User(Model):
    uName=Text(required=True)
    password=Text(required=True)

作为旁注,尽量不要使用 from xxx import * 通配符导入 - 它真的是 a bad practice :

Wildcard imports (from import *) should be avoided, as they make it unclear which names are present in the namespace, confusing both readers and many automated tools. There is one defensible use case for a wildcard import, which is to republish an internal interface as part of a public API (for example, overwriting a pure Python implementation of an interface with the definitions from an optional accelerator module and exactly which definitions will be overwritten isn't known in advance).

关于python - 为什么我会收到有关 String not defined 的错误消息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23251789/

相关文章:

python - scipy.weave.inline 无法按预期与数学库一起工作

redis - 试图理解 REDIS Monitor 命令

python - `@pyqtSlot()` 对嵌套函数有同样的影响吗?

python - Dash/Plotly - long_callback 在 celery/redis 后端失败

node.js - HDEL、HSET 在 NodeJS 中不工作

redis - Redis 使用什么版本/发行版的 Lua 和外部库?

scala - Spark : variable pipeline in class BinaryJedis cannot be accessed 上的 Redis

python - 计算 CSV 列中单词的出现次数并写入另一个 CSV?

python - pandas 的assign() 函数和apply() 函数有什么区别?

python - 使用 Pandas 通过键标识符转置多列