Py2neo.Graph()连接Neo4j报错:ImportError: cannot import name ‘clock‘ from ‘time‘ (unknown location)

Posted ZSYL

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Py2neo.Graph()连接Neo4j报错:ImportError: cannot import name ‘clock‘ from ‘time‘ (unknown location)相关的知识,希望对你有一定的参考价值。

ImportError: cannot import name 'clock' from 'time' unknown location

问题描述

使用py2neo连接Neo4j失败:

import py2neo

graph = py2neo.Graph('http://localhost:7474/browser/', auth=('neo4j', 'zs'))
---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
D:\\Anaconda3\\lib\\site-packages\\py2neo\\database.py in __new__(cls, uri, **settings)
     87         try:
---> 88             inst = cls._instances[key]
     89         except KeyError:

KeyError: 'f4d319166f241a7521b0ef1d3fa91b66'

During handling of the above exception, another exception occurred:

ImportError                               Traceback (most recent call last)
~\\AppData\\Local\\Temp/ipykernel_12568/17468484.py in <module>
----> 1 graph = py2neo.Graph('http://localhost:7474/browser/', auth=('neo4j', 'zs'))

D:\\Anaconda3\\lib\\site-packages\\py2neo\\database.py in __new__(cls, uri, **settings)
    303     def __new__(cls, uri=None, **settings):
    304         name = settings.pop("name", "data")
--> 305         database = Database(uri, **settings)
    306         if name in database:
    307             inst = database[name]

D:\\Anaconda3\\lib\\site-packages\\py2neo\\database.py in __new__(cls, uri, **settings)
     90             inst = super(Database, cls).__new__(cls)
     91             inst._connection_data = connection_data
---> 92             from py2neo.internal.http import HTTPDriver, HTTPSDriver
     93             from neo4j.v1 import Driver
     94             inst._driver = Driver(connection_data["uri"],

D:\\Anaconda3\\lib\\site-packages\\py2neo\\internal\\http.py in <module>
     27 from neo4j.bolt import ServerInfo
     28 from neo4j.exceptions import AuthError, Forbidden
---> 29 from neo4j.v1 import Driver, Session, StatementResult, TransactionError, SessionError
     30 
     31 from py2neo.data import Record

D:\\Anaconda3\\lib\\site-packages\\neo4j\\v1\\__init__.py in <module>
     23 from .exceptions import *
     24 from .result import *
---> 25 from .routing import *
     26 from .security import *
     27 from .types import *

D:\\Anaconda3\\lib\\site-packages\\neo4j\\v1\\routing.py in <module>
     23 from sys import maxsize
     24 from threading import Lock
---> 25 from time import clock
     26 
     27 from neo4j.addressing import SocketAddress

ImportError: cannot import name 'clock' from 'time' (unknown location)

ImportError: cannot import name 'clock' from 'time' (unknown location)

原因分析

Python time模块的 clock() 方法在python3.3弃用,在python3.8中将被移除:

DeprecationWarning: time.clock has been deprecated in Python 3.3 and will be removed from Python 3.8: use time.perf_counter or time.process_time instead

time.clock()

以浮点数计算的秒数返回当前的CPU时间,用来衡量不同程序的耗时,比time.time()更好用。

Py3.3后不建议使用,Py3.8后移除,该方法依赖于操作系统,建议使用per_counter(返回系统运行时间) 或 process_time(返回进程运行时间)代替。

解决方法

回到原问题,那一定是降低Python版本或者重装Neo4j的高版本。

如下:

Neo4j3.5需要导入的包:

from sys import maxsize
from threading import Lock
from time import clock

Anaconda中新建一个Python环境:

conda create -n envname python=3.7

之后安装py2neo即可!


参考Link

以上是关于Py2neo.Graph()连接Neo4j报错:ImportError: cannot import name ‘clock‘ from ‘time‘ (unknown location)的主要内容,如果未能解决你的问题,请参考以下文章

from py2neo.Graph import Graph报错:OverflowError: mktime argument out of range

Py2neo连接Neo4j报错:AuthError: http://localhost:7474/db/data/

安装Neo4j构建知识图谱+处理连接错误:ServiceUnavailable: WebSocket connection failure. Due to security constraints i

Docker搭建neo4j

neo4j & py2neo使用过程中踩过的坑

连接Neo4J Java驱动程序后,Neo4J Browser ServiceUnavailable失败