Trouble with making modules 'global'

Terry Reedy tjreedy at udel.edu
Thu Jun 4 04:18:31 EDT 2020


On 6/3/2020 11:48 PM, pythonfm at gmail.com wrote:

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

You cannot.  You must import every module either directly or indirectly 
into every module that uses it.

> 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

The PEP8 standard is all lower case for modules, leaving TitleCase for 
classes, so, for instance, file/module 'editor' contains the definition 
of class 'Editor' as its main class.

> import pandas as pd

Do you use 'pd' in Main?  If not, delete.

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

To use pd here, import it here.

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

Ditto.

> 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().

Because of missing imports.

> I tried to make it global by adding global pd in Main.py

Where it is likely irrelevant.


-- 
Terry Jan Reedy



More information about the Python-list mailing list