aspose file tools*
The moose likes Jython/Python and the fly likes Python import problems Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Languages » Jython/Python
Bookmark "Python import problems" Watch "Python import problems" New topic
Author

Python import problems

g Peshone
Ranch Hand

Joined: Oct 20, 2008
Posts: 44

How do I resolve these errors in the last blog?
1) the import? I am not sure what that means
2) why do I get the "None"?
3) the traceback? The files are same from my point of view but something hinders MyStuff.py file from working
Thanks much.
chris webster
Bartender

Joined: Mar 01, 2009
Posts: 1649
    
  14

First thing I would suggest is you break up your main script and get each piece working before you try to run them all at once. If this is your actual code, then there are some problems with MyStuff.py:

class MyStuffx(object): this tells Python 2.x that MyStuff inherits directly from the base "object" class in Python. You don't need to specify this in Python 3 (which I'm using) if you're just inheriting from the base class as here, but I'm not sure about your version of Python. Try including the "(object)" bit as it won't do any harm.

Your "apple" method is indented wrongly. Python gets rid of all those curly braces{} that are so common in Java etc, by relying on indentation and blank lines to work out how your code blocks are nested. You have an extra space before the "def apple", but you've already finished the previous method definition with a blank line, so Python can't work out how it should fit into your class definition.

Fix these problems and see if your MyStuff module imports correctly. Incidentally, I think the "None" error was because Python couldn't instantiate your MyStuffx object before you tried to call its apple() method, so that should go away if you can get a proper instance this time.

Your script also has an error when you try to call the apple() method:

apple is defined as a method in your MyStuffx class, but methods (functions) are also objects, the same as anything else in Python. If you refer to the method without the parentheses, Python assumes you are referring to the method as a function object (which you might want to do for various reasons). If you want to call the method normally, you need to include the parentheses:
As for this bit:

You can't import apple directly because it is a member of the MyStuffx class. You can import the MyStuffx class and then instantiate a MyStuffx object in order to call your apple() method (my version has parentheses in the print() because I'm on Python 3):

I think you need to revise the basics of Python (try Allen Downey's "Think Python" or Paul Barry's "Head First Python" for a nice intro, for example) to find out how to write and use a simple Python class or module, and work in small increments so you can be sure each step is working before you move onto the next. Once you key into the basic principles of Python (especially the use of whitespace), you'll probably find it's a lot more fun to work with than you think.

No more Blub for me, thank you, Vicar.
g Peshone
Ranch Hand

Joined: Oct 20, 2008
Posts: 44
Chris, when I look at the code for “def apple … “ it has the right indentation(?) All I did is to copy it from Spyder and paste it here.
Also, while you believe the white spaces are better vs the curly braces {} I would have the opposite point of view, I know where I started and when I ended.
Anyway, I looked at the other files comparing the implementation and from my point of view they are exactly same including the import and that's why I do not understand that at one instance is OK and the other instance just does not budge.
I rebooted and so the first error line is gone.
I figured out the “None”, I put print “something” in front of the method's print.
It is only this MyClass that complaining. I import and implement other class methods and all works fine. Hopefully one day I'll figure it out.
I will look more into those white spaces.
Thanks much.
chris webster
Bartender

Joined: Mar 01, 2009
Posts: 1649
    
  14

g Peshone wrote:Chris, when I look at the code for “def apple … “ it has the right indentation(?) All I did is to copy it from Spyder and paste it here

Line 26 of your code above has 4 spaces before the "def". Line 29 has 5 before the "def". If that's what your real code looks like, it's a bug.
g Peshone wrote:I figured out the “None”, I put print “something” in front of the method's print.

Yes, because you were calling a "print" within a call to "print". And I think you're doing the same thing in your "apple" method as well. I missed this too first time around. Use parentheses on your arguments to "print()" because (a) you'll need them in Python 3 and (b) you can see what it is you're actually passing into your "print" call.
g Peshone wrote:It is only this MyClass that complaining

Lines 59 to 62 of your code section above tell us that there was a problem with MyStuffx, not MyClass.
g Peshone wrote:Hopefully one day I'll figure it out.

We've already figured it out. See above.
g Peshone
Ranch Hand

Joined: Oct 20, 2008
Posts: 44
I am sorry Chris but it is not working!

it is complaining about attribute !? what ever that means?
chris webster
Bartender

Joined: Mar 01, 2009
Posts: 1649
    
  14

g Peshone wrote:I am sorry Chris but it is not working!...it is complaining about attribute !? what ever that means?

It is complaining because it doesn't recognise your MyStuffx class, which is in your MyStuff.py file. If you get rid of all the other crud in your script, then just import MyStuff and try to call the apple() function on an instance of MyStuffx, maybe you will have more luck.

Here's my version of your MyStuff.py file (tweaked for Python 3 and to fix the bad whitespace before "def apple"):

And here's my version of your script.py:

And here's the output from running script.py:

There are only nine lines of code here, so it shouldn't be too hard to track down where yours is falling over.
g Peshone
Ranch Hand

Joined: Oct 20, 2008
Posts: 44
It works in IPython as well but not in Spyder.

g Peshone
Ranch Hand

Joined: Oct 20, 2008
Posts: 44
I put it in seperate file and it works. I did not touch the class file at all but I do get now the initial top error!?
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Python import problems