cancel
Showing results for 
Search instead for 
Did you mean: 

Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.

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

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/__init__.py", line 151, in __del__ File "/usr/local/lib/python3.7/site-packages/neo4j/__init__.py", line 178, in close File "/usr/local/lib/python3.7/site-packages/neobolt/direct.py", line 675, in close File "/usr/local/lib/python3.7/site-packages/neobolt/direct.py", line 660, in remove File "/usr/local/lib/python3.7/site-packages/neobolt/direct.py", line 516, in close File "/usr/local/lib/python3.7/logging/__init__.py", line 1320, in debug File "/usr/local/lib/python3.7/logging/__init__.py", line 1468, in _log File "/usr/local/lib/python3.7/logging/__init__.py", line 1478, in handle File "/usr/local/lib/python3.7/logging/__init__.py", line 1540, in callHandlers File "/usr/local/lib/python3.7/logging/__init__.py", line 854, in handle File "/usr/local/lib/python3.7/logging/__init__.py", line 1080, in emit File "/usr/local/lib/python3.7/logging/__init__.py", 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

6 REPLIES 6

Can you also share your code snippet?

This is the code that calls my logger:

for relation in all_relations:

        start = datetime.datetime.now()
        neo4j_datastore.addRelationsByRules(relation)
        end = datetime.datetime.now()
        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)

        self.logger.info(string) 

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']
logging.config.fileConfig(config_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)

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:
https://docs.python.org/3.7/library/functions.html#open

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.

I'm seeing a similar stack trace, google brought me to this discussion. Running anaconda python 3.6.8. Just finished upgrading from py2neo 3.1.2 to py2neo 4.2.0 (conda-forge). That also brought in new packages:
neobolt conda-forge/win-64::neobolt-1.7.13-py36hfa6e2cd_0
neotime pkgs/main/win-64::neotime-1.7.4-py36_0

Still troubleshooting, but the stack trace doesn't reference any of my code. Seems to be debug level logging from neobolt opening and closing connections.

My application does a LOT of logging, until today, nothing has ever triggered a message like this.

--- Logging error ---
Traceback (most recent call last):
File "C:\conda\envs\gh-dev\lib\logging\handlers.py", line 71, in emit
if self.shouldRollover(record):
File "C:\conda\envs\gh-dev\lib\logging\handlers.py", line 185, in shouldRollover
self.stream = self.open()
File "C:\conda\envs\gh-dev\lib\logging_init
.py", line 1061, in _open
return open(self.baseFilename, self.mode, encoding=self.encoding)
NameError: name 'open' is not defined
Call stack:
File "C:\conda\envs\gh-dev\lib\site-packages\neobolt\direct.py", line 254, in del
self.close()
File "C:\conda\envs\gh-dev\lib\site-packages\neobolt\direct.py", line 541, in close
log_debug("[#%04X] C: GOODBYE", self.local_port)
Message: '[#%04X] C: GOODBYE'
Arguments: (56767,)

As a workaround, I've assigned a nullHandler to the "neobolt" logger. That seems to avoid the problem, although I'm still perplexed as to why the logging library can't find the built-in open method.

Sample logging.conf relevant bits (merge with your other conf items)

[loggers]
keys=neobolt

[logger_plugins]
level=DEBUG
handlers=nullHandler
qualname=plugins
propagate=0

[handler_nullHandler]
class=NullHandler
level=DEBUG
formatter=simpleFormatter
args=()

Python knows the purposes of certain names (ex. built-in functions ). Other names are defined within the program (ex. variables). If Python encounters a name that it doesn't recognize, you'll probably get NameError: global name 'xx' is not defined error. In most cases, this error is triggered when Python sees a variable name (Global or Local) and doesn't know what it's for. These errors can happen if you forget to initialize a variable , if you misspell a variable, or if you misspell a reserved word such as "True". Before you use the global variable in your function for reading, it must be first initialized somewhere: either outside of the function or inside it.