Trouble with making modules 'global'

Barry Scott barry at barrys-emacs.org
Thu Jun 4 04:14:01 EDT 2020



> On 4 Jun 2020, at 04:48, pythonfm at gmail.com wrote:
> 
> Hi,
> 
> I am struggling with making modules global for all definitions in my code.

Each python module has to import all the modules it needs.
Importing on one module does not make the imported names
usable from another module.

What python does do is only run the code of an imported module once.
The seconds module to do an import will find that its already imported
and just setup the names to be usable.

> 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
> 
> def main_part()
> ... Result = optimization_def(x,y,z)
> ...
> 
> (2) Optimization.py

You also need to import pandas in each module that uses it.

> def optimization(x_l,y_l,z_l)
> ... O = obejctive(x_l,y_l)
> ...
> 
> (3) Model.py

And here.

> 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

Barry

> -- 
> https://mail.python.org/mailman/listinfo/python-list
> 



More information about the Python-list mailing list