0

Here's my code:


import numpy as np
import matplotlib.pyplot as plt
import astropy
import matplotlib
%matplotlib ipympl
import scatterplot_with_hist as sc
badx=[]
bady=[]
import badcomp as bc

#things like data5 and list2 are defined in here--I know that code is functional so I'll omit it for brevity

bc.getlist(start = 2000, end = 2200)

The module code is as follows:

def getlist(start, end):
    for f in range(1):
        for i in range(1238):
            for n in range(int(start),int(end)):
                if ((data[n]['col1'] - list2[i]) == 0):
                    badx.append(data[n]['col2'])
                    bady.append(data[n]['col3'])

If I run this code in the regular space (instead of importing it and running it as a function) it works fine. When I run it as an imported function, it won't recognize variables like data5, list2, and badx and bady.

Why?

2
  • 4
    When a module is imported in Python, it does not have access to any of the code in the module that it is imported from. The code is not copied into the location of the import statement. Jul 7, 2020 at 23:22
  • You can instead pass the items you want to modify as arguments to getlist
    – jdaz
    Jul 7, 2020 at 23:26

2 Answers 2

1

Each Python module has it's own global namespace. That means that code in different modules that each try to access global variables will see separate ones. You can access another module's global variables by importing the module and interacting with the attributes of the module object.

In your code, the getlist function in the badcomp module is trying to interact with several global variables, including badx and bady for output, and data and list2 for input. It's not working because you've defined those in the interactive session, which uses the namespace of a module with the special name __main__.

While you could import __main__ from badcomp and interact with the global variables defined there via the module's attributes, that would be a really bad design, since it won't work if the module gets imported in any other way (e.g. by a different module you write later). Instead, the function should probably use variables defined in its own global namespace. The __main__ module is already importing badcomp (as bc), and can access things like badx and bady as bc.badx and bc.bady if the definitions are moved into the module.

Or you might reconsider if global variables are the best way for this function to work. It's often much better to use arguments and return values to pass data in and out of a function, rather than global variables. Maybe badx and bady should be defined within getlist and returned at the end. Meanwhile, data and list2 could be added as arguments to the function.

0

When a module is imported, it does NOT have access to the global or local namespace of the module that called it. You can get around this by creating a function that creates a variable in the global namespace inside the imported module and run the function from the calling module with each of the variables you need.

Example code (really bad design, but it'll teach you hopefully):

Put THIS in the imported module:

def putVarsInNamespace(variable, variableNameToInject)
    exec("global %s" % variableName)
    exec("%s = variable" % variableName)

Put THIS in the calling module:

test = 5
from <MODULENAME> import putVarsInNamespace
putVarsInNamespace(test, "test")

How this works: variableNameToInject is the name that you want the injected variable to be called. It then runs global variableNameToInject but it uses the VALUE of variableNameToInject which is the name that the injected variable should be called. This is useful when you want to inject multiple variables without using multiple functions. It then sets the variable name (the value of variableNameToInject) to the value of variable, and just like that it's injected.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.