Deon Van Zyl
Deon Van Zyl

Reputation: 519

ValueError: Unable to configure handler 'file': [Errno 2] No such file or directory:

I am very new to Python and Django and is currently busy learning myself through tutorials on www.djangoproject.com. I am using PyCharm and working on OS X El Capitan. I have imported a project from github and created a virtual environment for the project interpretor based on Python 3.5.1. In the vm I installed django.

I then activated the vm.

Now.. i started by trying to execute simple commands in the terminal like python manage.py startapp deonapp and python manage.py runserver but each time I get an error which I pasted below.. What did I miss? I cannot seem to find the /log/ directory?

Traceback (most recent call last):
  File "/usr/local/Cellar/python3/3.5.1/Frameworks/Python.framework/Versions/3.5/lib/python3.5/logging/config.py", line 558, in configure
    handler = self.configure_handler(handlers[name])
  File "/usr/local/Cellar/python3/3.5.1/Frameworks/Python.framework/Versions/3.5/lib/python3.5/logging/config.py", line 731, in configure_handler
    result = factory(**kwargs)
  File "/usr/local/Cellar/python3/3.5.1/Frameworks/Python.framework/Versions/3.5/lib/python3.5/logging/__init__.py", line 1008, in __init__
    StreamHandler.__init__(self, self._open())
  File "/usr/local/Cellar/python3/3.5.1/Frameworks/Python.framework/Versions/3.5/lib/python3.5/logging/__init__.py", line 1037, in _open
    return open(self.baseFilename, self.mode, encoding=self.encoding)
FileNotFoundError: [Errno 2] No such file or directory: '/Users/deon/Documents/PyCharmProjects/Developments/deonproject/log/debug.log'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/Users/deon/Documents/PyCharmProjects/Developments/deonproject/venv/lib/python3.5/site-packages/django/core/management/__init__.py", line 367, in execute_from_command_line
    utility.execute()
  File "/Users/deon/Documents/PyCharmProjects/Developments/deonproject/venv/lib/python3.5/site-packages/django/core/management/__init__.py", line 341, in execute
    django.setup()
  File "/Users/deon/Documents/PyCharmProjects/Developments/deonproject/venv/lib/python3.5/site-packages/django/__init__.py", line 22, in setup
    configure_logging(settings.LOGGING_CONFIG, settings.LOGGING)
  File "/Users/deon/Documents/PyCharmProjects/Developments/deonproject/venv/lib/python3.5/site-packages/django/utils/log.py", line 75, in configure_logging
    logging_config_func(logging_settings)
  File "/usr/local/Cellar/python3/3.5.1/Frameworks/Python.framework/Versions/3.5/lib/python3.5/logging/config.py", line 795, in dictConfig
    dictConfigClass(config).configure()
  File "/usr/local/Cellar/python3/3.5.1/Frameworks/Python.framework/Versions/3.5/lib/python3.5/logging/config.py", line 566, in configure
    '%r: %s' % (name, e))
ValueError: Unable to configure handler 'file': [Errno 2] No such file or directory: '/Users/deon/Documents/PyCharmProjects/Developments/deonproject/log/debug.log'

Upvotes: 44

Views: 118786

Answers (9)

Abdullah Rafi
Abdullah Rafi

Reputation: 51

This error can be caused if you haven't created the folder of logs file

  • Go to the setting.py file and check the path of your logs file
  • Follow the path and create the folder

Upvotes: 2

Abrar Mahi
Abrar Mahi

Reputation: 145

In my Django project, I implemented some logic that would handle a situation like this:

we can declare some log-path, and if it doesn't exist we can create the path:

# LOGGING
LOG_DIR = os.path.join(BASE_DIR, 'log')
LOG_FILE = '/api.log'
LOG_PATH = LOG_DIR + LOG_FILE

if not os.path.exists(LOG_DIR):
    os.mkdir(LOG_DIR)

if not os.path.exists(LOG_PATH):
    f = open(LOG_PATH, 'a').close() #create empty log file
else:
    f = open(LOG_PATH,"w").close() #clear log file

Then in our Logging dict we can add the following to our "file" section of the handlers

LOGGING = {
  ...
    'handlers': {
        'file': {
           ...
            'filename': LOG_PATH, 
           ...           
        },
        'console': {
           ...
        }
    },
    
}

Upvotes: 4

Ajani Timothy
Ajani Timothy

Reputation: 224

For example if your handler is configured as

"handlers": {
        "console": {
            "class": "logging.StreamHandler",
            "formatter": "console",
        },
        "file": {
            "level": "INFO",
            "class": "logging.FileHandler",
            "formatter": "file",
            "filename": "logs/jirate.log",<-----
        },
        "django.server": DEFAULT_LOGGING["handlers"]["django.server"],
    },

ensure that you have the folder logs created in the same level as your manage.py. Django will automatically create the jirate.log file

Upvotes: 0

P.Netev
P.Netev

Reputation: 1

In my case it was due to permission denied as django commands were executed as not owner of the logger files

Upvotes: 0

Makan dianka
Makan dianka

Reputation: 111

your log path does not exist. Make sure this path exists

'/Users/deon/Documents/PyCharmProjects/Developments/deonproject/log/debug.log'

Upvotes: 1

Mohammed Jaseem K
Mohammed Jaseem K

Reputation: 471

  1. Check whether log file exist in /Users/deon/Documents/PyCharmProjects/Developments/deonproject/log/debug.log location.
  2. Check these location have proper write permission In my case the ransomware protection was turned on and it preventing it from permission issue
  3. Open the Code editor RUN AS ADMINISTRATOR mode

Upvotes: 1

Himanshu Chauhan
Himanshu Chauhan

Reputation: 81

I also faced the same issue, in my case the permissions of the logging file was not right. so i changed the permissions to read and write to all user with the cmd

sudo chmod a+rwx debug.log

Upvotes: 7

Gamugo
Gamugo

Reputation: 121

In case someone get it useful: In my case the error was triggered by a permissions issue. Once i change permissions and let app user write in folder this was solved.

Upvotes: 2

Dmitry Torba
Dmitry Torba

Reputation: 3194

You do not have path to a log file for some reason (/Users/deon/Documents/PyCharmProjects/Developments/deonproject/log). Make sure that all directories exist (if no, create them) and create an empty debug.log log file (just in case).

What happens is there is some problem with your code happening. Handler catches this error to save it to your log file so that you can analyze it. However, the path to log file it is trying to open does not exist. Thus, exception occures during handling of another exception.

Upvotes: 63

Related Questions