5

I have many modules (hundreds). In each module I have at least one function. My question is how can I import all functions from all modules with only one line? Because I do not want to do it like this:

from tests.test_001 import test_001_func from tests.test_002 import test_002_func from tests.test_003 import test_003_func ... from tests.test_999 import test_999_func 

test_xxx are modules and these modules contains test_xxx_func function, all modules are in one folder.

I would like use something like this:

from tests import * test_001.test_001_func() 

But this is not working

5
  • Why do you have your code structured that way? Also, even supposing you were able to import them, how would you then call each of those functions? Commented Apr 17, 2014 at 18:08
  • 9
    My suggestion is, don't have hundreds of modules. Commented Apr 17, 2014 at 18:08
  • Can give more detail as to what "not working" means? Commented Apr 17, 2014 at 18:09
  • Every module will be one test case for my application. And I want to have one module which will contains all calls (will call every test case). Each module (test case) will have around 200 lines of code. So 200 lines for one test cases and 100 test cases it is 200*100 lines of code in one file. This is the reason why I need each test case stand alone. Commented Apr 17, 2014 at 18:11
  • In looking at the larger picture, may be you should look into unittest or nose. Commented Apr 17, 2014 at 18:47

4 Answers 4

2

Create a __init__.py file in the tests directory and in it put:

__all__ = [ "test_001_func", "test_002_func", # etc ] 

Then you can either:

import tests tests.test_001_func() 

or

from tests import * # Not the best way to do things. 

N.B. The reason that the import * is not the prefered solution is that you loose the namespace from the calls so you can a) possibly have collisions with names from other modules and b) your code is less clear.

Sign up to request clarification or add additional context in comments.

1 Comment

Ok, this is nice solution so I can have this 'define' separately in init file and main script will contain only important things and simple calls. Thank you
0

You need to provide an index of the contents of the tests package by including an __all__ list in the package's __init__.py file.

in your case, the __init__.py file in the tests directory would have something like:

__all__ == ["test_001", "test_002" <<etc...>>] 

See the docs on importing * from a package.

Comments

0

Your could automate import using script below.

Assume that folders structure is the next:

run.py tests -> test_001.py -> test_002.py 

code for tests/__init__.py

import os import sys # Append to path "test" folder (also can be any other name) sys.path.append(os.path.basename(os.path.dirname(__file__))) __all__ = [] for k in xrange(1, 3): name = "test_%03d" % k # __import__ can load module by string name globals()[name] = __import__(name) # set that we export only required names like test_001, test_002 __all__.append(name) 

Code for test_001.py:

def test_001_func(): print "test1" 

Code for test_002.py:

def test_002_func(): print "test2" 

Code for run.py: import tests

print tests.test_001 print tests.test_001.test_001_func print tests.test_002 

The output of the run.py script will be the next:

test@test:~/Desktop/python$ python run.py <module 'test_001' from 'tests/test_001.pyc'> <function test_001_func at 0xb728b09c> <module 'test_002' from 'tests/test_002.pyc'> 

Comments

0

As other answers pointed out, it all boils down to filling __all__, but I guess you could automate things a bit:

# In your tests/__init__.py do something along the following lines: import os.path, pkgutil, importlib __path = os.path.dirname(__file__) __all__ = [name for _, name, _ in pkgutil.iter_modules([__path])] for __module in __all__: importlib.import_module("." + __module, __name__) 

The code is Python 2.7+ due to importlib, but it can be modified for obsolete Python versions by using bare __import__.

Then use it like this:

import tests tests.test_002.test_002_func() 

Or

from tests import * test_002.test_002_func() 

However, if this is for unit testing, I strongly suggest taking a look at unittest or something like Nose, that will handle test discovery in a more Pythonic way.

1 Comment

This looks very easy how to do this. I need this way for my blackbox testing (embeded systems) so no unittest. Thank you.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.