-1

In the following line

def run_job_subtype(self):
    print os
    if os.path.exists(self.abs_export_dir):
        return "incr"
    else:
        return "full"

I am getting a traceback as follow:

"AttributeError: 'NoneType' object has no attribute 'path'

printing os:

<module 'os' from '/usr/lib/python2.7/os.pyc'>
<module 'os' from '/usr/lib/python2.7/os.pyc'>
<module 'os' from '/usr/lib/python2.7/os.pyc'>
None
None
None
None
None
None
None

all of the sudden it is set to None
I dont think I am programmatically setting to None..
Is it possible for os being set to None somehow other than by a programmer?
or any ways to debug this besides manually looking over thousands of lines of code?

This function is called within a thread

Another weird thing.... I've changed the code to:

def run_job_subtype(self):
        print os
        print sys.modules['os']

        if os.path.exists(self.abs_export_dir):
            return "incr"
        else:
            return "full"

Traceback

print sys.modules[\'os\']
', "AttributeError: 'NoneType' object has no attribute 'modules'

And im also importing sys. I am definitely not creating variables for both os and sys...

something is wiping all the imports?

ealeon
  • 12,074
  • 24
  • 92
  • 173
  • 4
    You have some variable `os` in your code somewhere that you are setting equal to `None` – Patrick Haugh Jan 07 '17 at 16:22
  • Somewhere, somehow, you have assigned `None` to a variable named `os`, thus shadowing the module name. – snakecharmerb Jan 07 '17 at 16:22
  • 1
    Try to generate a complete enough example that someone can copy-and-paste it from the question and see the problem themselves. Methods for doing this (and motivation, beyond "failure to provide a MCVE with a code-centric question makes it eligible to be closed") are discussed at http://stackoverflow.com/help/mcve and http://sscce.org/. – Charles Duffy Jan 07 '17 at 16:28
  • @CharlesDuffy im not asking for what IS wrong. im asking for what could be wrong. i've shown the problem and without neceesarily providing code which is reproducable, ppl's experiences from coding could give hindsight on what *could* be the source of such an error – ealeon Jan 07 '17 at 16:35
  • 1
    @ealeon, ...and the site rules make questions of that form ("please dump a bunch of speculation") closable. You're expected to do your homework -- in this case, "your homework" means building a reproducer. – Charles Duffy Jan 07 '17 at 16:38
  • Good edits! -- still doesn't have an MCVE, but as-modified (to be explicitly a question about debugging process, vs a question about a given bug), I'm not sure it needs one. How much other code is in the same module (the same file)? It should only be that one module, vs the entirety of your program, that's at question. – Charles Duffy Jan 07 '17 at 16:46
  • You might consider using `python -m trace yourprogram.py`, and grepping the output -- you could even limit that to the output between the last place where the module *does* exist and the following place where it doesn't. – Charles Duffy Jan 07 '17 at 16:49
  • By the way, you might consider [Watch for a variable change in Python](http://stackoverflow.com/questions/13402847/watch-for-a-variable-change-in-python) to be relevant. Note that you'll want to use it to watch `yourmodule.os`, where `yourmodule` is the module where the code in question lives. – Charles Duffy Jan 07 '17 at 16:55
  • ...I wonder if you have code (intended for sandboxing or such) that's going through `globals()` and clearing things out. Regardless, it should show up in a trace. – Charles Duffy Jan 07 '17 at 16:59

1 Answers1

0

The error is exactly what is wrong - You have set a variable with a name of os, thus when running os.path.exists() you are trying to run .path on a string, integer, or some other variable.

UnsignedByte
  • 849
  • 10
  • 29
  • Could you then show all your code? It is hard to work with just one line... :) – UnsignedByte Jan 07 '17 at 16:29
  • @BoboCarr, ...FYI, general practice is to close questions that aren't complete enough to definitively answer them, as opposed to providing answers that can't be evaluated for correctness except by the OP. There's a close method for specifically this question type: *Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself.* -- in this case, the "shortest code necessary to reproduce" is missing (as what code is given doesn't repro the problem). – Charles Duffy Jan 07 '17 at 16:33
  • that's the challenging part. my code span across about 30 py files with many of them over 1000 lines of code each. im grepping to see any variable named os. i was wondering if there were any common mistake other than setting the variable name os – ealeon Jan 07 '17 at 16:33
  • @ealeon, ...there's some oddness that could happen in situations where the Python interpreter is embedded -- the `os` module is provided by a C file that's separately compiled for each platform -- but if your problem is there, it's *really* deep in the details of how exactly your software is being built/run. Bobo's answer is certainly the most probable one, though it would need to be in an enclosing scope unless `sys.modules` is being messed with. But that's the thing -- Bobo's answer is >95%-chance right, but without a MCVE we can't be 100% sure. – Charles Duffy Jan 07 '17 at 16:36
  • the weird part is.. i have os calls all over the place but it's exclusively raising at that spot. if i comment that os line, such error is not raised in other os calls... – ealeon Jan 07 '17 at 16:36
  • I've never heard of anything weird like that, normally it is when os is another variable. Try removing sections of code untill it works? Also, to make sure os isn't a variable, you can try to print os. If that works, and it prints a value, either python is glitched or you really have a variable named os. – UnsignedByte Jan 07 '17 at 16:38
  • 2
    @ealeon: we can't see your code. Just before you try to use `os.path` you set `os` to `None`. That could be in any number of ways, see the [Naming and Binding section](https://docs.python.org/3/reference/executionmodel.html#naming-and-binding) for all the ways you could be setting `os`. It could the code is looking for a local, or a closure, or a global name `os`. Without your code we can't tell you anything else. – Martijn Pieters Jan 07 '17 at 16:38
  • 1
    @CharlesDuffy Closing this question because it *currently* doesn't provide sufficient information to resolve doesn't help anyone, though. It would be better to ask OP for that information and for him to edit his post to include it, which is what I see done almost all of the time anyway. – Tagc Jan 07 '17 at 16:39
  • @Tagc, closing a question isn't permanent -- editing a closed question automatically puts it back on the review queue for reopening, so if the OP edited their question to *provide* a MCVE, then if everything works correctly, it *would* be reopened in that case. – Charles Duffy Jan 07 '17 at 16:39
  • @CharlesDuffy Ah okay, I didn't know that. Thanks. – Tagc Jan 07 '17 at 16:40
  • 1
    @Tagc: the question won't help future visitors, which is the point of Stack Overflow. Putting a question on hold is also a temporary state; if a MCVE were added we could easily re-open. – Martijn Pieters Jan 07 '17 at 16:40
  • if i "import os" right before the os.paht call, would it overwrite whatever i set os to IF the assumptino is that i mistakenly set a variable name os? – ealeon Jan 07 '17 at 16:40
  • @ealeon, personally, I'd use `os = sys.modules['os']`, just to be *completely* explicit. – Charles Duffy Jan 07 '17 at 16:41
  • @MartijnPieters What I mean is that if we ask OP to edit his question to include additional details and OP does, then we can help him and additionally his question will be beneficial to future visitors. If the question is closed (permanently), it helps neither OP nor future visitors. – Tagc Jan 07 '17 at 16:41
  • @MartijnPieters Thanks yeah, my original comment was posted before Charles Duffy told me otherwise. – Tagc Jan 07 '17 at 16:42