0
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
abkai
  • 591
  • 2
  • 6
  • 18

3 Answers3

2

You have two problems in your code:

  • __import__ doesn't import into global namespace, it returns a module
  • you're trying to import test_samp while it's scripts.test_samp

What you actually want is:

scripts = __import__("scripts", fromlist=modo)
scripts.test_samp.function("test") 

Above __import__ returns scripts package with all the sub-modules loaded. Don't forget to make scripts directory a package by creating __init__.py in it.

See also: Why does Python's __import__ require fromlist?

Community
  • 1
  • 1
vartec
  • 131,205
  • 36
  • 218
  • 244
  • Thank you for your help! That seems to have gotten it working, however __init__.py has peaked my interest. I am having issues with it though, as referenced in a comment in the above post. – abkai Apr 17 '12 at 11:30
1

Your are not defining test_samp you are defining modules['test_samp']. Plus if it's in scripts you need to import scripts.test_samp

in yor case use a package.Add an empty (or not) __init__.py (with 2 underscores). and use import scripts. Access your function with scripts.test_samp.function("test"). And you could use reload(scripts) to reload all of the package.

jadkik94
  • 7,000
  • 2
  • 30
  • 39
  • Unless scripts is in your python path. – Gareth Latty Apr 17 '12 at 10:52
  • Thank you, but then why does: for modu in modo: __import__(modu) Also not work? – abkai Apr 17 '12 at 10:54
  • @Lattyware yes. but still a better a way is to use a package with an __init__.py file – jadkik94 Apr 17 '12 at 10:58
  • what do you mean does not work ? the error you posted has nothing to do with that statement – jadkik94 Apr 17 '12 at 11:00
  • How would I perform this? It is important that I would be able to use the reload(all_modules_in_folder) function. What would __init__.py need to contain in order for it to work? – abkai Apr 17 '12 at 11:00
  • That's the error displayed in the terminal window when I ran that script using the interpreter on XBN Linux, python 2.5/2.7 – abkai Apr 17 '12 at 11:02
  • @jadkik94 Of course, I was just making note of another possibility, not saying it was a better way. – Gareth Latty Apr 17 '12 at 11:03
  • It is indeed, thank you :) For some reason however, I cannot pull any functions from the modules located in that folder: AttributeError: 'module' object has no attribute 'function' – abkai Apr 17 '12 at 11:26
  • Try `import scripts.test_samp`. Read more about packages and modules in the python docs" It's more detailed. – jadkik94 Apr 17 '12 at 11:34
  • @jadkik94 aah, thank you! Checking out http://docs.python.org/tutorial/modules.html 6.4 now. Thanks heaps for your's and everyone else's help! :) – abkai Apr 17 '12 at 11:42
1

You can run it using this:

modules["test_samp"].function("test")
Steve Mayne
  • 22,285
  • 4
  • 49
  • 49