2

I'm not entirely sure what has happened.

I was debugging a script that worked "fine" (except for said intermittent bugs) and all of a sudden the module cannot be imported anymore. I undid all changes and still the problem exists. Well, problemS. Plural.

Once I got an "unexpected indent" error, even though all the lines were perfectly indented. I fixed that one by deleting the line and retyping it.

Now in the following code, I get one of two errors:

class Lottery:
    def __init__(self, session):
          self.prizes = PrizeList()
          self.session = session
          self.players = self.session.listof.players.split(',')
          self.pickWinner()

Most of the time, it gives me an error saying "session" is not defined. This is true. I'm just importing the module. It gets defined when the script calling it runs it. I experimented with just removing that line altogether, and then it told me that "self" was not defined.

All of that is the original code that was working 20 minutes ago. the bugs I was fixing are on a different part of this module altogether, and it was certainly importing without problems. Please help!

Traceback:

  File "minecraft/mcAdmin.py", line 5, in <module>
    from lottery.lottery import *
  File "/home/tomthorogood/minecraft/lottery/lottery.py", line 36, in <module>
    class Lottery:
  File "/home/tomthorogood/minecraft/lottery/lottery.py", line 39, in Lottery
    self.session = session
NameError: name 'session' is not defined

EDIT: SOLVED. Okay, somehow while editing, I did accidentally switch between tabs and spacing, which lead to the problem. I deleted and re-wrote that block of code exactly as pasted above and now it's working. There was a ghost indent problem.

DOUBLE EDIT: the core problem was that I only recently turned on auto-indenting in Vim. The configuration I am using is not using tabs as auto-indents, but I was using tabs in the past.

Thomas Thorogood
  • 2,150
  • 3
  • 24
  • 30
  • 2
    Can you provide us with a [Short, Self-Contained Correct Example](http://videobb.com/video/inDeNEW9wbTY) that reproduces the problem? – André Caron Sep 23 '11 at 00:03
  • I'm not sure what you mean. All that's happening is "from lottery.lottery import *". – Thomas Thorogood Sep 23 '11 at 00:03
  • Any chance you are mixing TAB characters and space characters in your source file? – Ned Deily Sep 23 '11 at 00:04
  • Have you tried removing your .pyc files? http://stackoverflow.com/questions/785519/remove-all-pyc-files-from-a-project Otherwise it may be due to caching so you might need to clear cache manually or wait for timeout. – funkotron Sep 23 '11 at 00:06
  • don't you have a version control system? – Karoly Horvath Sep 23 '11 at 00:06
  • @funkotron - Just tried that. It didn't help. – Thomas Thorogood Sep 23 '11 at 00:07
  • @yi_h it's a simple script to give prizes in Minecraft I developed one Saturday morning. There was no reason to use a version control system that I could see. – Thomas Thorogood Sep 23 '11 at 00:08
  • Can you post the smallest piece of code that reproduces the problem, or at least post the error message you get. A couple thoughts: 'Class' should be 'class' with a lowercase 'c'. Your unexpected indent error was probably caused by mixing tabs and spaces. Text editors will sometimes add spaces when you press tab, and sometimes add a tab character. You should consistently use either tabs or spaces for indenting. The usual Python standard is four space indents. – Peter Graham Sep 23 '11 at 00:08
  • @Tom: now you see the reason. – Karoly Horvath Sep 23 '11 at 00:08
  • @Ned - Good thought. My VIM does autoindenting. To be safe, I just joined all the lines onto one line and manually re-defined them, but to no avail. – Thomas Thorogood Sep 23 '11 at 00:10
  • This a clean **NameError**. fix it, rename session to _session – Alan Turing Sep 23 '11 at 00:10
  • @peter - The problem is reproduced when importing the module. No part of the program is even run because of this. Int he program C is lowercase, that was a mistake in copy-pasting on here (I didn't copy-paste that line, so typed it in manually) – Thomas Thorogood Sep 23 '11 at 00:11
  • obviously, you should be using emacs, as God intended us to. – Marcos Eliziário Santos Jul 29 '15 at 16:41

1 Answers1

1

You've got an indentation problem.

The self.session = session line, and everything below, isn't inside your __init__ method, it's just inside the class body.

session isn't defined in the class body, only inside __init__, as you mention in your question, so you get the error.

IF you remove that line, the first thing that gets looked up is the self in self.players = self.session.listof.players.split(','), so you get the self is not defined error.

agf
  • 171,228
  • 44
  • 289
  • 238
  • And "d'oh!" about the removing that line and wondering why 'self' wasn't defined. I'm still confused about self.session=session, though. – Thomas Thorogood Sep 23 '11 at 00:14
  • thank you. I deleted and re-wrote that block of code and it does work now. I think you and Peter were both correct about the indentation. It LOOKED indented, but it must have been a faulty space/tab mixup, so the interpreter didn't read it as such. – Thomas Thorogood Sep 23 '11 at 00:21
  • I also changed the title of the post so it could be useful to someone else, now that I know what the problem was. – Thomas Thorogood Sep 23 '11 at 00:22
  • I'm setting that up as we speak! I never had the need before, even with Python. I have changed my .vimrc around a bit and added some auto folding, etc., so that could be the core issue. – Thomas Thorogood Sep 23 '11 at 00:31
  • Next time try to locate the code giving an error by refactoring you code first, in any language you use. – Alan Turing Sep 23 '11 at 00:33
  • Yup. That's totally the issue. I hadn't edited this script since adding the autoindent feature. Everything from before is tabbed, but everything now is autoindented, which is not technically a tab. – Thomas Thorogood Sep 23 '11 at 00:33