Show More
@@ -1,43 +1,47 b'' | |||||
1 | # encoding: utf-8 |
|
1 | # encoding: utf-8 | |
2 | """ |
|
2 | """ | |
3 | A simple utility to import something by its string name. |
|
3 | A simple utility to import something by its string name. | |
4 |
|
4 | |||
5 | Authors: |
|
5 | Authors: | |
6 |
|
6 | |||
7 | * Brian Granger |
|
7 | * Brian Granger | |
8 | """ |
|
8 | """ | |
9 |
|
9 | |||
10 | #----------------------------------------------------------------------------- |
|
10 | #----------------------------------------------------------------------------- | |
11 | # Copyright (C) 2008-2011 The IPython Development Team |
|
11 | # Copyright (C) 2008-2011 The IPython Development Team | |
12 | # |
|
12 | # | |
13 | # Distributed under the terms of the BSD License. The full license is in |
|
13 | # Distributed under the terms of the BSD License. The full license is in | |
14 | # the file COPYING, distributed as part of this software. |
|
14 | # the file COPYING, distributed as part of this software. | |
15 | #----------------------------------------------------------------------------- |
|
15 | #----------------------------------------------------------------------------- | |
16 |
|
16 | |||
17 | #----------------------------------------------------------------------------- |
|
17 | #----------------------------------------------------------------------------- | |
18 | # Functions and classes |
|
18 | # Functions and classes | |
19 | #----------------------------------------------------------------------------- |
|
19 | #----------------------------------------------------------------------------- | |
20 |
|
20 | |||
21 | def import_item(name): |
|
21 | def import_item(name): | |
22 | """Import and return bar given the string foo.bar.""" |
|
22 | """Import and return bar given the string foo.bar.""" | |
23 | package = '.'.join(name.split('.')[0:-1]) |
|
23 | package = '.'.join(name.split('.')[0:-1]) | |
24 | obj = name.split('.')[-1] |
|
24 | obj = name.split('.')[-1] | |
25 |
|
25 | |||
26 | # Note: the original code for this was the following. We've left it |
|
26 | # Note: the original code for this was the following. We've left it | |
27 | # visible for now in case the new implementation shows any problems down |
|
27 | # visible for now in case the new implementation shows any problems down | |
28 | # the road, to make it easier on anyone looking for a problem. This code |
|
28 | # the road, to make it easier on anyone looking for a problem. This code | |
29 | # should be removed once we're comfortable we didn't break anything. |
|
29 | # should be removed once we're comfortable we didn't break anything. | |
30 |
|
30 | |||
31 | ## execString = 'from %s import %s' % (package, obj) |
|
31 | ## execString = 'from %s import %s' % (package, obj) | |
32 | ## try: |
|
32 | ## try: | |
33 | ## exec execString |
|
33 | ## exec execString | |
34 | ## except SyntaxError: |
|
34 | ## except SyntaxError: | |
35 | ## raise ImportError("Invalid class specification: %s" % name) |
|
35 | ## raise ImportError("Invalid class specification: %s" % name) | |
36 | ## exec 'temp = %s' % obj |
|
36 | ## exec 'temp = %s' % obj | |
37 | ## return temp |
|
37 | ## return temp | |
38 |
|
38 | |||
39 | if package: |
|
39 | if package: | |
40 | module = __import__(package,fromlist=[obj]) |
|
40 | module = __import__(package,fromlist=[obj]) | |
41 | return module.__dict__[obj] |
|
41 | try: | |
|
42 | pak = module.__dict__[obj] | |||
|
43 | except KeyError: | |||
|
44 | raise ImportError('No module named %s' % obj) | |||
|
45 | return pak | |||
42 | else: |
|
46 | else: | |
43 | return __import__(obj) |
|
47 | return __import__(obj) |
General Comments 0
You need to be logged in to leave comments.
Login now