Python AttributeError
When I type this code into a python shell it works perfectly fine but within a program it gives an error.
import os
h = os.environ['HOME']
within a script it gives this err开发者_如何学运维or:
AttributeError: 'str' object has no attribute 'environ'
Why is this happening and is there any way I can fix it?
(I'm kinda just learning python so I dont know much. Google didn't help)
Somewhere, you've created a string and named it os
. The .
is the attribute lookup operator, so it's complaining about the thing to the left of the .
, in this case, os
.
are you sure that between import os
and h = os.environ['HOME']
you did not use os
as a variable for a string?
edit: If you do not work with an editor with a debugger (e.g. Eclipse with PyDev), try to find out from which point os
is no longer a module by calling print(os)
at some key points in your code
精彩评论