'module' object has no attribute 'basicConfig'

Question:

I have the following code, copied from the Python manual:

import logging
LOG_FILENAME = 'example.log'
logging.basicConfig(filename=LOG_FILENAME,level=logging.DEBUG)

logging.debug('This message should go to the log file')

When I try to run the script (via python.exe script.py) I get an error of 'module' object has no attribute 'basicConfig'.

However when I copy and paste the code in interactive mode (via python.exe then copy and pasting the actual code) I get no error. The code runs fine.

I have python 2.6.6.

Thank you!

Asked By: Alex

||

Answers:

You’ve got another module called logging on the Python path; probably a logging.py file in the same directory. Try checking with print(logging.__file__).

Answered By: Chris Morgan

You Need to Change your script name into something else rather than logging.py I guess.
(You are using that name for this script aren’t you?

Answered By: NNN

I have faced the same attribute error while running and when checked with current working folder there are 2 filenames with logging. Delete those filenames or move to recycle bin or if imp move to different folder or change names for those logging files and run your program. It will work fine.

Answered By: Haritha Dogiparthi

This is a mere side-note for those coding Python in a Google Cloud Function who do not use the Python logging module but import google.cloud.logging and who still get the same error.

It is obvious for the reader, but it took me some time to understand this, therefore sharing this.

I thought that the import google.cloud.logging would be a kind of wrapper of the Python built-in logging module so that it would need the same:

    # Config the logger
    logging.basicConfig(
        level=logging.INFO, format="%(asctime)s [%(levelname)s]: %(message)s"
    )

But no, the default Client() without parameters is enough and text output is either log_struct or log_text, no log level parameter like INFO needed.

In Google Cloud Function edit mode (not for a local Python script!)

"requirements.txt":

google-cloud-logging==3.0.0

"main.py":

    logging_client = gcloud_logger.Client()
    # The name of the log to write to
    log_name = environ['LOG_NAME']
    # Selects the log to write to
    logger = logging_client.logger(log_name)

https://cloud.google.com/logging/docs/reference/libraries#log-entries

Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.