1

this is my attempt in python 3.4:

import imp

def my_example_function():
  print("my_example_function:")

class my_class:
  def __init__(self):
    module = imp.load_source(my_example_function, __file__)

my_class_instance = my_class()

result is:

Traceback (most recent call last):
  File "D:\Python34\test.py", line 10, in <module>
    my_class_instance = my_class()
  File "D:\Python34\test.py", line 8, in __init__
    module = imp.load_source(my_example_function, __file__)
  File "D:\python34\lib\imp.py", line 166, in load_source
    spec = util.spec_from_file_location(name, pathname, loader=loader)
  File "<frozen importlib._bootstrap>", line 932, in spec_from_file_location
  File "<frozen importlib._bootstrap>", line 1439, in is_package
AttributeError: 'function' object has no attribute 'rpartition'

It looks like some kind of internal error a bug in python itself.

6
  • Importing is for modules, not functions.. Commented May 11, 2014 at 10:12
  • I don't understand much from stackoverflow.com/questions/972/… as for now, can someone tell me whats wrong with my code ? Is thing that I there want to attempt even legal in python ? Commented May 11, 2014 at 10:20
  • As I said; importing is for modules. Adding a method to an instance is not importing. Commented May 11, 2014 at 10:24
  • But anyway this error is a bug of itself, its very cryptic. Commented May 11, 2014 at 10:29
  • No, the function you are using expects a string; it is documented as such. Passing in anything else breaks. Commented May 11, 2014 at 10:40

1 Answer 1

2

the error says, that a 'function'-object has no attribute 'rpartition'. The only function object, you give to load_source is my_example_function but a name aka string is expected. And strings have a method called rpartition. So if you use load_source correctly, you won't get an error. But I cannot say, how, because I don't understand what you try to do.

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.