This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Cannot import anything that imports tokenize from script called token.py
Type: Stage: resolved
Components: Versions: Python 3.4
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: inglesp, ned.deily
Priority: normal Keywords:

Created on 2014-07-06 00:30 by inglesp, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Messages (2)
msg222386 - (view) Author: Peter Inglesby (inglesp) * Date: 2014-07-06 00:30
A script called token.py that imports anything that ends up importing tokenize, such as logging, triggers the following error when the script is run:

$ cat token.py 
import tokenize
$ python3 token.py
Traceback (most recent call last):
  File "token.py", line 1, in <module>
    import tokenize
  File "/usr/local/Cellar/python3/3.4.0/Frameworks/Python.framework/Versions/3.4/lib/python3.4/tokenize.py", line 38, in <module>
    __all__ = token.__all__ + ["COMMENT", "tokenize", "detect_encoding",
AttributeError: 'module' object has no attribute '__all__'
msg222387 - (view) Author: Ned Deily (ned.deily) * (Python committer) Date: 2014-07-06 00:48
That's because there is also a standard library module named token (https://docs.python.org/3/library/token.html).  When you run python, by default the current working directory is inserted at the beginning of sys.path, the list of directories searched for modules.  If you have a file there that has the same name as a standard library module, e.g. token.py, it will be found first and, thus, "hide" the standard library module of the same name.  Either change the name of your file so it does not conflict or make sure it is in a directory not on sys.path.
History
Date User Action Args
2022-04-11 14:58:05adminsetgithub: 66123
2014-07-06 00:48:31ned.deilysetstatus: open -> closed

nosy: + ned.deily
messages: + msg222387

resolution: not a bug
stage: resolved
2014-07-06 00:30:33inglespcreate