Python Forum
global name 'thetime' is not defined
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
global name 'thetime' is not defined
#1
Hi,

So I have three files :

1.) File_INIT.py

The main file that will initialize (mostly) everything.
This file contains definitions for functions function_sub_A up to function_sub_B and so on...

2.) File_A.py

contains function FUNCTION_A

3.) File_B.py

contains function FUNCTION_B

This is roughly what File_INIT.py will look like :

#initialize variables and imports
#...
from File_A import FUNCTION_A
from File_B import FUNCTION_B
#...

def function_sub_A():
     #do something

def function_sub_B():
     #do something else

def thetime():
     #do something else

#and so on

FUNCTION_A()
FUNCTION_B()


#do something
My problem is now that I am using thetime inside FUNCTION_B but it tells me that global name 'thetime' is not defined.
I tried using global thetime within FUNCTION_B but that doesn't make any difference. I also tried that in File_INIT.py at the beginning but that also doesn't help. Any idea what I can do to get thetime to be detected in FUNCTION_B ?
Reply
#2
Please supply actual code
Reply
#3
sorry, will not do that. The actual code is 2500 lines and subject to copyright and company confidentiality.
thanks though
Reply
#4
File_B needs to import thetime from File_INIT but that creates a circular dependency because File_INIT is importing File_B which is import File_INIT, etc...

Put thetime in its own file and have both File_B and File_INIT import it. If you "can't" do that then something is probably wrong with your design.
Reply
#5
So you've got two different modules, that each need access to the function thetime? Then that shared code should be in it's own file, and imported by both. Or, since you're already importing FILE_B, just define the function in there and import it into the other.
Reply
#6
If you don't want to move functions from File_INIT.py to another module as mpd suggests, there is a way by decorating the functions. Define a file ExtSymbols.py containing the following code
class _Sy:
    def register(self, func):
        setattr(self, func.__name__, func)
        return func
sy = _Sy()
Then in File_INIT.py
from ExtSymbols import sy

@sy.register
def thetime():
    ...
Finally in FILE_B
from ExtSymbols import sy

def FUNCTION_B():
    sy.thetime()
The decorator sy.register can be used with several functions in several files.
Reply
#7
*vomit*
Snooty
Reply
#8
Gribouillis's solution is "smelly": not necessarily wrong but indicates the existence of a problem. In this case, the decorator creates a more-global namespace which is something that should be avoided. In a large program, you may have two thetime methods that you want to register... well, only one is getting used.

Namespaces are great and we need to do more of them, quoth the Zen of Python.
Reply
#9
I certainly don't mean this decorator as a general way to organize modules, but on a specific problem, if one wants to use File_INIT.py and add features with very little change in its code it can be handy. It may not be that easy to move thetime() to another file because its code may depend on other items defined in this file. The decorator allows one to use the function without a complete refactoring.
Reply
#10
Simple is better than complex.

and OP code screams of problem with (at least) how the code is organised, if not something worse
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  python library not defined in user defined function johnEmScott 2 3,879 May-30-2020, 04:14 AM
Last Post: DT2000
  NameError: NameError: global name 'BPLInstruction' is not defined colt 7 4,416 Oct-27-2019, 07:49 AM
Last Post: Larz60+
  Global variable does not seem to be global. Columbo 6 3,711 Jul-15-2019, 11:00 PM
Last Post: Columbo
  NameError: Global Name is not defined MartinBerlin 3 41,274 Aug-25-2018, 09:03 AM
Last Post: DeaD_EyE

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020