【问题标题】:Importing neo4j with py2neo使用 py2neo 导入 neo4j
【发布时间】:2016-12-28 06:56:10
【问题描述】:

我无法使用 py2neo 导入“neo4j”。当我执行以下操作时;

 from py2neo import neo4j

我得到错误:

  cannot import name 'neo4j' 

我的py2neo版本是3.1.2

下面的输出是:

dir(py2neo)

['BoltDataSource', 'BoltNode', 'BoltPath', 'BoltRelationship', 'BoltTransaction', 'ClientError', 'Commander', 'ConstraintError', 'Cursor', 'CypherSyntaxError', 'CypherTypeError', 'CypherWriter', 'DBMS', 'DataSource', 'DatabaseError', 'Entity', 'Forbidden', 'Graph', 'GraphDatabase', 'GraphError', 'HTTPDataSource', 'HTTPResponse', 'HTTPTransaction', 'JAVA_INTEGER_MAX_VALUE', 'JAVA_INTEGER_MIN_VALUE', 'JSONResponse', 'Mapping', 'NOT_FOUND', 'Node', 'NodeSelection', 'NodeSelector', 'OrderedDict', 'PRODUCT', 'PULL_ALL', 'Path', 'PropertyDict', 'RUN', 'Record', 'Relatable', 'Relationship', 'RemoteEntity', 'ReprIO', 'Resource', 'ResourceTemplate', 'Response', 'Schema', 'ServerAddress', 'ServerAuth', 'ServerError', 'ServerPlugin', 'SetView', 'StringIO', 'Subgraph', 'ThreadLocalEntityCache', 'Transaction', 'TransactionFinished', 'TransientError', 'UNAUTHORIZED', 'URI', 'Unauthorized', 'UnmanagedExtension', 'Walkable', 'Watcher', '__author__', '__builtins__', '__cached__', '__copyright__', '__doc__', '__email__', '__file__', '__license__', '__loader__', '__name__', '__package__', '__path__', '__spec__', '__version__', 'auth', 'authenticate', 'b64encode', 'basic_auth', 'bolt_hydrate', 'caching', 'cast', 'cast_node', 'cast_relationship', 'chain', 'client_errors', 'coerce_atomic_property', 'coerce_property', 'compat', 'cypher', 'cypher_escape', 'cypher_repr', 'cypher_request', 'database', 'deprecated', 'deque', 'ext', 'get_auth', 'get_http_headers', 'getenv', 'http', 'integer', 'is_collection', 'json_dumps', 'keyring', 'main', 'mktime_tz', 'normalise_request', 'order', 'packages', 'parsedate_tz', 'raise_from', 'register_server', 'relationship_case', 'remote', 'round_robin', 'selection', 'set_http_header', 'size', 'snake_case', 'status', 'stdout', 'string', 'types', 'unicode', 'update_stats_keys', 'user_agent', 'ustr', 'util', 'uuid4', 'version_tuple', 'walk', 'warn', 'watch', 'webbrowser', 'xstr']

如何从 py2neo 导入 neo4j?

【问题讨论】:

    标签: neo4j py2neo


    【解决方案1】:

    为什么你认为你可以从 py2neo 导入 neo4j?仔细查看 py2neo 文档:http://py2neo.org/v3/

    您的导入语句应该类似于from py2neo import Graph, Node, Relationship, authenticate

    【讨论】:

    • 我试图理解我正在从事的项目的前一位成员编写的代码,其中 neo4j 是从 py2neo 导入的。也可以在这里看到:codereview.stackexchange.com/questions/75842/…
    • 它适用于旧版本的 py2neo,我认为它甚至不是 py2neo 2,可能是 py2neo 1.6。您正在使用 py2neo3。
    • 那么“Graph”导入是否可以替代早期版本中的“neo4j”?
    • 并非如此。阅读与 Nigel Small 本人的不同之处:neo4j.com/blog/py2neo-2-0-unleashed
    • 这正是我想要的!我没有找到合适的地方。谢谢!
    【解决方案2】:

    如果您想使用传统编码方式创建节点关系,您可以通过从 py2neo 导入节点、关系、图表等来创建它,例如:

    from py2neo import Graph, Node, Relationship, authenticate
    

    但如果你想执行密码查询,你需要安装 neo4j 并将其导入你的代码

    用 pip 安装 neo4j

    pip 安装 neo4j

    import neo4j
    
    driver = neo4j.GraphDatabase.driver('bolt://localhost',auth=basic_auth("neo4j", "Password1"))
    
    def get_db():
        if not hasattr(g, 'neo4j_db'):
            g.neo4j_db = driver.session()
        return g.neo4j_db
    
    db = get_db()
    results = db.run("MATCH (movie_1:Movie) "
                     "WHERE movie_1.title =~ {title} "
                     "RETURN movie", {"title": "(?i).*" + q + ".*"}
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-04-28
      • 2019-04-15
      • 2014-05-19
      • 1970-01-01
      • 2015-06-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多