How to access the global variable of the imported module

When I made changes to the legacy scripts, I required a test, but if I was writing unittest code for a script with only functions without a class, I encountered a function test using a global variable and could not declare it globally. Let's go! I got the error.

※ Python2.7

Run

test_aaa.py


import aaa_module

class TestAAA(unittest.TestCase):
    def test_aaa
        aaa_module.aaa_func()

error

...
NameError: global name 'logger' is not defined

There is no logger. There is.

aaa_module.py


if __name__ == '__main__':
    logger = logging.getLogger('...')

Declared with global. There is.

Global declaration (delusion)

I couldn't help it, so I tried the following on the unittest side.

Run

test_aaa.py


import aaa_module

class TestAAA(unittest.TestCase):
    def test_aaa
        global logger
        logger = logging.getLogger('...')
        aaa_module.aaa_func()

error

...
NameError: global name 'logger' is not defined

does not change. Why ...

There were no global variables in Python

Reference: In Python, there are no global variables, to be exact, in-module variables. is it?

How to access variables in the module

module.global variable = value

Run

test_aaa.py


import aaa_module

class TestAAA(unittest.TestCase):
    def test_aaa
        aaa_module.logger = logging.getLogger('...')
        aaa_module.aaa_func()

There are still many unknowns in Python

Recommended Posts

How to access the global variable of the imported module
How to use the optparse module
How to use the ConfigParser module
(Note) How to pass the path of your own module
How to check the version of Django
Pass the path of the imported python module
How to check the memory size of a variable in Python
How to access the Datastore from the outside
How to calculate the volatility of a brand
How to find the area of the Voronoi diagram
How to uniquely identify the source of access in the Django Generic Class View
[NNabla] How to get the output (variable) of the middle layer of a pre-built network
How to access the contents of a Linux disk on a Mac (but read-only)
How to know the port number of the xinetd service
How to get the variable name itself in python
How to run the Ansible module added in Ansible Tower
How to get the number of digits in Python
How to use the Raspberry Pi relay module Python
How to visualize the decision tree model of scikit-learn
[Blender] How to dynamically set the selection of EnumProperty
[Python] Summary of how to specify the color of the figure
How to hit the document of Magic Function (Line Magic)
[Selenium] How to specify the relative path of chromedriver?
Python Note: The mystery of assigning a variable to a variable
How to avoid the cut-off label of the graph created by the plot module using matplotlib
How to use the generator
How to unlock pyenv global
How to use the decorator
How to increase the axis
How to start the program
How to increase the processing speed of vertex position acquisition
[Ubuntu] How to delete the entire contents of a directory
How to find the optimal number of clusters in k-means
How to test the attributes added by add_request_method of pyramid
Make the display of Python module exceptions easier to understand
How to calculate the amount of calculation learned from ABC134-D
How to summarize the results of FreeSurfer ~ aparc, aseg, wmparc ~
How to run the Export function of GCP Datastore automatically
Hook to the first import of the module and print the module path
How to increase the number of machine learning dataset images
How to see the contents of the Jupyter notebook ipynb file
How to connect the contents of a list into a string
How to calculate the autocorrelation coefficient
Test the version of the argparse module
How to use the zip function
How to handle multiple versions of CUDA in the same environment
[sh] How to store the command execution result in a variable
How to determine the existence of a selenium element in Python
How to change the log level of Azure SDK for Python
How to implement Java code in the background of RedHat (LinuxONE)
How to know the internal structure of an object in Python
Summary of how to use pandas.DataFrame.loc
How to change the color of just the button pressed in Tkinter
[EC2] How to install chrome and the contents of each command
Make the theme of Pythonista 3 like Monokai (how to make your own theme)
How to use Python's logging module
[Python] How to get the first and last days of the month
Summary of how to use pyenv-virtualenv
How to get the Python version
How to check the memory size of a dictionary in Python
[TensorFlow 2] How to check the contents of Tensor in graph mode