TypeError: 'module' object is not callable when importing glob in Eclipse
Asked Answered
C

5

11

I am working with Eclipse Kepler(2013) and python 3.3.2 and running a simple import like

import glob
a = glob.glob('*')
print(a)

gives a:

TypeError: 'module' object is not callable

This is not the case if I run the same code in Idle. I know I am missing something.

Any help is appreciated.

Cervantez answered 6/9, 2013 at 12:0 Comment(3)
What does glob.__file__ returns?Centring
Never use the same name of a built-in module or function.Kymry
It is mentioned in one answer below, but the solution is very simple: use import glob as globLawhorn
C
6

Probably in your Eclipse environment there's a module named glob that gets imported before the standard library one.

Try printing the glob.__file__ to check it out.

Concordia answered 6/9, 2013 at 12:5 Comment(1)
THis comment helped understanding the problem. Grazie Paolo!Cervantez
D
27

What worked for me was i changed import glob to from glob import glob at the top of the file.

Doerrer answered 15/3, 2019 at 13:32 Comment(1)
This is the best answer hereLawhorn
M
8

In some cases people end up using same file name as built in modules. Don't name your file as "glob.py".

Microcyte answered 31/8, 2015 at 5:59 Comment(1)
Thanks! that was it. Also, if you have a left-over glob.pyc file that got created from your glob.py - clean it too...Witham
C
6

Probably in your Eclipse environment there's a module named glob that gets imported before the standard library one.

Try printing the glob.__file__ to check it out.

Concordia answered 6/9, 2013 at 12:5 Comment(1)
THis comment helped understanding the problem. Grazie Paolo!Cervantez
C
4

This is only possible if you've defined a package named glob in the module search path, so instead of loading the built-in module glob python is importing that package.

Something like this in the module search path

glob
├── glob.py
├── glob.pyc
├── __init__.py
└── __init__.pyc

will produce the same error:

>>> import glob
>>> glob.__file__
'/home/monty/py/glob/__init__.pyc'
>>> glob.glob()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'module' object is not callable

You need to change the name of this package to something else because it seems to be present on the module search path used by eclipse.

Centring answered 6/9, 2013 at 12:5 Comment(1)
LOL. I named my python script 'glob.py' and wondered why I couldn't access the glob module.Trematode
O
0

I changed import glob to from glob import glob. It works fine for me. You can try it

Op answered 8/6, 2022 at 2:51 Comment(1)
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Magner

© 2022 - 2024 — McMap. All rights reserved.