10

I'm new to Python and Pscyhopy and whenever I try running an experiment, it gives me this error:

File
"/private/var/folders/nb/k9sz30gj29l_7d8l6tmbcxhr0000gn/T/AppTranslocation/F9CCC296-B2DF-4D05-A6AE-F9DE0928FE0E/d/PsychoPy3.app/Contents/Resources/lib/python3.6/lib2to3/pgen2/driver.py", line 120, in load_grammar
    logger = logging.getLogger()
AttributeError: module 'logging' has no attribute 'getLogger'

I already made sure that I don't have any files named logging.py but it still will not run. The demos run fine though.

DYZ
  • 55,249
  • 10
  • 64
  • 93
Durna Alakbarova
  • 109
  • 1
  • 1
  • 3

3 Answers3

25

This type of problem occurs whenever there is circular import or any other logging module which don't have getLogger. Although you said you searched for any file naming logging.py but still i think your project has filename logging.py somewhere . If the current file you are working has name logging.py change it else it will give circular import error. Try searching for python library path if nothing works then only one option left is to change version Hope this helps..

  • I copied my `logging.py` from other repo where it worked just fine. No idea what makes this repo with `AttributeError` crucially different. Simply renamed to `log.py`, problem solved... with a feeling of not understanding what's just happened. – V-R Nov 16 '20 at 20:15
  • 2
    It may not be `logging.py`, but `logging/__init__.py`... – lindes Feb 25 '21 at 02:15
  • In my case, after renaming the `logging` directory, it took the additional step of a `pip uninstall` of the package, and reinstallation. `--upgrade --force-reinstall ` was *not* sufficient to fix the problem. – James_SO Feb 15 '22 at 13:59
  • in my case, it was ```string.py``` so i renamed it to ```stringsession.py``` – Spidy May 09 '23 at 17:50
4

One possible solution for this problem (depending on the exact details) has to do with where one is running code from -- and changing it. For example:

If I have a small utility called test, that's meant to be run from the command line (it starts with a "shebang" line specifying some python or other), and I put it inside the directory of a python module (let's say pythonX.Y/site-packages/whatever, for example) that in turn has a logging module within it (i.e. there's a logging.py or logging/__init__.py somewhere within the whatever directory hierarchy), and then try to run it (via, say, ./test — again, from within the whatever directory — or perhaps even elsewhere within the same site-packages, I'm not sure), I'll get the error you described.

If, however, I copy[0] the exact same file out of the pythonX.Y/site-packages/whatever directory, and try running it from that somewhere else (e.g. I copy it to my home directory, and run it as ~/test), then it works. Or vice-versa: a working script in ~ will break if I copy it to pythonX.Y/site-packages/whatever.

This would presumably also apply to various other ways of invoking things than just shebang scripts (e.g. python -m whatever ... or such)... bottom line: if there's a logging module anywhere near where the file you're trying to run code from lives, there's a good chance python is trying to import that version of logging instead of the usual system one.

For module owners, I'd suggest considering renaming the module's logging to something else, to avoid creating this problem for your users.

For regular users of a thing, just pay attention to where you're calling your code from (including where the file physically sits on the filesystem, relative to various modules that might get imported).

Hopefully this helps someone get past this error! It confounded me for a bit, and this was the solution that worked for my purposes.


[0] Note: it's not enough to create a symlink... though that can be useful if one does it in the other direction; either way, python finds the absolute path of the regular file, and the rest of this analysis applies to wherever that file is: inside the module, get the module's logging; outside the module, get the system logging.

lindes
  • 9,854
  • 3
  • 33
  • 45
-1

Ran into this today and I have a file called logging.py in my repo. I only get this error when I run python code without debugging. If I use the debugger in VSCODE I do not get the error.

JBear
  • 1