-1

This is my folder structure:

script.py
api/
   __init__.py
   charts/
         __init__.py
         charts.py

In script.py, I have:

from api.charts import charts
import billboard

and the call:

charts('Alternative-Songs', '1997')

billboard.py is not in the above structure because it was installed on my system via python setup.py install, and it has the methods for charts(), like so:

billboard.ChartData(chart_name, date)

at charts.py, charts() was defined using a billboard.py method:

def charts(chart_name, date):
    chart = billboard.ChartData(chart_name, date, quantize=True)
    return chart

but when I run script.py, I get the following error:

Traceback (most recent call last):
  File "script.py", line 70, in <module>
    print (charts('Alternative-Songs', '1997'))
TypeError: 'module' object is not callable

How do I fix this?

martineau
  • 119,623
  • 25
  • 170
  • 301
8-Bit Borges
  • 9,643
  • 29
  • 101
  • 198

2 Answers2

1

given your folder structure, charts directory contains a charts.py file.

So from api.charts import charts imports name charts as a module.

Seems that the charts module has a function called charts. You think you're calling the function but you're calling the module.

just do

print (charts.charts('Alternative-Songs', '1997'))

(that makes a lot of charts if you ask me :))

Jean-François Fabre
  • 137,073
  • 23
  • 153
  • 219
1
from api.charts import charts

imports the module charts.py from the chart directory under api. (see this question for modules aliasing and importing)

Now chart is a module reference, not the method charts.

To call charts, you'll have to use

print(charts.charts('Alternative-Songs', '1997'))

(method charts inside module charts).

Community
  • 1
  • 1
Uriel
  • 15,579
  • 6
  • 25
  • 46