Nikhil Haksar
Nikhil Haksar

Reputation: 452

"TypeError: 'module' object is not callable" when initializing Gekko module in function

I'm trying to write a function that, given a set of parameters, uses Gekko to solve an optimal control problem. For whatever reason, whenever I run this function, it gives this error.

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-7-92ece108f7ea> in <module>
      1 import gekko as GEKKO
----> 2 solve_system()

<ipython-input-6-9d154ef663e4> in solve_system(theta, alpha, rho, chi, L_bar, n, w, delta_inc, xi, phi, tau, kappa, GAMMA, T, SIGMA, BETA, s_init, i_init, r_init)
     26 
     27     ##### initialize model #####
---> 28     m = GEKKO()
     29 
     30     ##### parameters #####

TypeError: 'module' object is not callable

I was looking into it and it seems to generally be an issue with the way you import the package/module, but I did this similarly (but not in a function) previously and had no issues. I have no idea where to start solving it: any pointers?

Upvotes: 2

Views: 652

Answers (2)

John Hedengren
John Hedengren

Reputation: 14346

You can import gekko a couple different ways to create a model m.

Method 1

from gekko import GEKKO
m = GEKKO()

Method 2

import gekko as gk
m = gk.GEKKO()

Method 3

If you want to use some of the other modules like the chemicals or deep learning objects in gekko you can use something like:

from gekko import gekko, chemical, brain
m = gekko()
c = chemical.Properties(m)
b = brain.Brain(m)

Method 4

Although it is possible, you should never do the following because of potential namespace conflicts with other imports:

from gekko import *
m = GEKKO()

BTW, great question! I recommend keeping the answer by rdas as the accepted response because it is the minimal correct solution. I just included these other options here for reference.

Upvotes: 1

rdas
rdas

Reputation: 21275

From the docs I think the import is supposed to be:

from gekko import GEKKO

Upvotes: 3

Related Questions