Error "NameError: name 'open' is not defined" when attempting to implement logging


(Golder Kamu) #1

I'm trying to implement Python's logging feature in a Python script that uses Neobolt and Neo4J's Python driver but I get the following error:

Exception ignored in: Traceback (most recent call last): File "/usr/local/lib/python3.7/site-packages/neo4j/", line 151, in __del__ File "/usr/local/lib/python3.7/site-packages/neo4j/", line 178, in close File "/usr/local/lib/python3.7/site-packages/neobolt/", line 675, in close File "/usr/local/lib/python3.7/site-packages/neobolt/", line 660, in remove File "/usr/local/lib/python3.7/site-packages/neobolt/", line 516, in close File "/usr/local/lib/python3.7/logging/", line 1320, in debug File "/usr/local/lib/python3.7/logging/", line 1468, in _log File "/usr/local/lib/python3.7/logging/", line 1478, in handle File "/usr/local/lib/python3.7/logging/", line 1540, in callHandlers File "/usr/local/lib/python3.7/logging/", line 854, in handle File "/usr/local/lib/python3.7/logging/", line 1080, in emit File "/usr/local/lib/python3.7/logging/", line 1070, in _open NameError: name 'open' is not defined

My code doesn't initiate any Neo4J transactions, only attempts to log them onto a file. Once I remove the logging code, the error is eliminated.

My setup:
Neo4J Version: 3.5.1 Community
Neo4J Mode: Single instance
Driver version: Python driver 1.7.1
Operating System: Fedora 29

(Michael Hunger) #2

Can you also share your code snippet?

(Golder Kamu) #3

This is the code that calls my logger:

for relation in all_relations:

        start =
        end =
        minutes, seconds == compute_time(start, end)
        string = 'Time taken to run query for {0} relationship for node {4}: Minutes: {1}, Seconds: {2}'.format(relation, minutes, seconds, count) 

This is how my logger object is configured:

super(TwitterDataProcessor, self).init()
config_file = ('config_file.cfg')
log_file = self.config['Field_Object']['log_file']
defaults={'Object': log_file}
self.logger = logging.getLogger('Object')

neo4j_datastore.addRelationsByRules(relation) calls the following code:

query = "MATCH (a:"+str(relation["NodeLabel1"])+"),(b:"+str(relation["NodeLabel2"])+")"+" WHERE a."+str(relation["Property1"])+" = b."+str(relation["Property2"])+" "+"CREATE UNIQUE (a)-[r:"+str(relation["RelationshipLabel"])+"]->(b)"

self._driver.session().write_transaction(self.addRelationsByRulesToDB, query)

(Nigel Small) #4

Something looks very broken with your Python environment. I don't think this is anything to do with Neo4j.

Your stack trace shows a problem in the standard library logging module. Specifically within this function:

    def _open(self):
        Open the current base file with the (original) mode and encoding.
        Return the resulting stream.
        return open(self.baseFilename, self.mode, encoding=self.encoding)

This uses the built-in Python open function, as documented here:

But if that function isn't available, which it should always be, then you have a fundamental problem with your environment. I suggest rebuilding your Python environment from scratch and, if that doesn't fix it, have a look for something that could be blocking access to the open built-in.