0

When I import a function it runs the code no problem, but if I import a class it doesn't work. It says ImportError: cannot import name 'Y' and I have tried it on sublime text, and command prompt. If I import a function it works.

from testing2 import x
x()
#testing2.py has:
def x():
    print("It worked")

If I import a class it doesn't work.

from testing2 import Y
Y().x()

#testing2 has:

class Y:
    def x(self):
        print("It worked")
Chris
  • 223
  • 4
  • 12
  • 1
    Both worked for me. Did you name something else as `Y` below class `Y`? – Chih-Hsu Jack Lin May 16 '17 at 01:19
  • 1
    There is nothing wrong with code. You may saving in some path and trying to access from another. – Mauro Baraldi May 16 '17 at 01:19
  • This is the only code in the files, and I am using only these same files. I will successfully import a function, then erase everything and rewrite it with a class, and it brings an error when importing a class. – Chris May 16 '17 at 01:22
  • http://stackoverflow.com/questions/4142151/how-to-import-the-class-within-the-same-directory-or-sub-directory – whackamadoodle3000 May 16 '17 at 01:24
  • @ whackamadoodle3000 I have tried creating a "__init__.py" file, adding a "." in front of .testing2 and it still didn't work. Possibly I need to uninstall python then reinstall it I don't know. – Chris May 16 '17 at 01:36
  • 1
    Issues like this are rarely to do with your installation of Python. Have you tried a completely blank directory; create your Python file with your class and function in it; cd to that directory, open the console, type :'import , and the use dir on the module - check that the function and class are both visible in the dir output. – Tony Suffolk 66 May 16 '17 at 14:55
  • @ Tony Suffolk thanks for the advice, for whatever reason when I made a new folder in a completely different directory it worked.. Do you have advice on keywords for a google search on why this is? – Chris May 16 '17 at 15:53
  • @ Tony Suffolk the original is working now too, so it was like trying it in a different folder snapped my computer back into shape or something. – Chris May 16 '17 at 16:14

0 Answers0