from os import listdir
modo= [name.split(".py")[0] for name in listdir("scripts") if name.endswith(".py")]
modules = {}
for modu in modo:
modules[modu] = __import__(modu)
test_samp.function("test")
Hello!
If, say “test_samp.py” exists in the scripts directory, why does
this not allow me to run test_samp.function(“test”)?
It returns:
Unhandled exception in thread started by <function function at 0x8e39204>
Traceback (most recent call last):
File "test_this.py", line 6, in function
test_samp.function("test")
NameError: global name 'test_samp' is not defined
Your are not defining
test_sampyou are definingmodules['test_samp']. Plus if it’s in scripts you need to importscripts.test_sampin yor case use a package.Add an empty (or not)
__init__.py(with 2 underscores). and useimport scripts. Access your function withscripts.test_samp.function("test"). And you could usereload(scripts)to reload all of the package.