Trouble with making modules 'global'

Peter Otten __peter__ at web.de
Thu Jun 4 04:28:16 EDT 2020


pythonfm at gmail.com wrote:

> Hi,
> 
> I am struggling with making modules global for all definitions in my code.

Don't. Global names in Python are global to a single module, not your entire 
application.

> I faced the problem with the pyomo modules but can generate the error with
> pandas too.
> 
> The model structure looks as follow:
> 
> I have 3 '.py' files each of them containing definitions. The first
> controls inputs and outputs and starts an optimization.py file (
> optimization.py). Within optimization.py I call a third .py file
> (model.py) that includes a series of definitions, representing
> mathematical equations of a optimization model.
> 
> 
> 
> (1) Main.py
> import pandas as pd

If you want to use Optimization in Main.py you have to import the module 
just like you did with pandas.

> 
> def main_part()
> ... Result = optimization_def(x,y,z)
> ...
> 
> (2) Optimization.py

If you want to use pandas in Optimization.py you have to import it.

Note that only the first time a module is imported in an application the 
import may take a noticeable time. Subsequent imports result in a lookup in 
the sys.modules cache.

> def optimization(x_l,y_l,z_l)
> ... O = obejctive(x_l,y_l)
> ...
> 
> (3) Model.py
> def objctive(x_a,x_b)
> ...
> def constraint(x_a,z_a)
> ....
> 
> 
> I do not understand why pd is not known in def optimization() as well as
> in objctive(). I tried to make it global by adding global pd in Main.py
> 
> 
> I would appreciate any support. Thanks in advance
> Frank




More information about the Python-list mailing list