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 error:
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
Related
import mafia
game.add_faction(Town())
game.add_faction(Town())
game.add_faction(Mafia("Crypto Mafia"))
game.add_player("Alice", Cop(town))
game.add_player("Bob", Doctor(town))
game.add_player("Eve", Goon(mafia))
Hello I'm trying to write a python program with a module called mafia (It's from github: https://github.com/calder/mafia/) But when i tried to test the module it gives me "Game" is not defined, I installed the mafia, checked it with dir command, it works. I'm the faulty one here so please enlighten me.
(Btw don't bash me i'm new to python)
Edit: changed import mafia to
from mafia import *
now gives 'mafia.game' has no attribute 'add_faction' error.
You need to add following line.
from mafia import *
g = Game()
Then you can append your code
game.add_faction(Town())
game.add_faction(Mafia("Crypto Mafia"))
game.add_player("Alice", Cop(town))
game.add_player("Bob", Doctor(town))
game.add_player("Eve", Goon(mafia))
I am three weeks into my independent study Python course, and am having trouble getting passed an issue. I am trying to use the function enterbox() from easygui I write:
import easygui
value = easygui.enterbox("say something")
And when I run it I get the error:
**AttributeError: 'module' object has no attribute 'enterbox'
Does anyone know why this may be happening? Thanks! (I'm using version 2.7)
It's been a while since I have used Python and am stumbling already at a simple import!
statements.py:
str = "hello"
main.py:
import statements
print statements.str
Obviously the final program will have more going on, and the statements will be stuff like URLs. For this simple example however with both files sitting side by side in the same folder I get the following error on running main.py:
AttributeError: 'module' object has no attribute 'str'
I know I am doing something terribly silly, but I can't see what it is. Searching around it looks fine. Any help/insight is appreciated.
You used the import statement correctly. The error then can be caused by either importing a different module with the same name, or by having altered the module after already having imported it.
You can check print statements.__file__ to see what module Python found instead when importing, and you can use the reload() function to ask Python to reload the module from disk if you altered it.
Note: Solved. It turned out that I was importing a previous version of the same module.
It is easy to find similar topics on StackOverflow, where someone ran into a NameError. But most of the questions deal with specific modules and the solution is often to update the module.
In my case, I am trying to import a function from a module that I wrote myself. The module is named InfraPy, and it is definitely on sys.path. One particular function (called listToText) in InfraPy returns a NameError, but only when I try to import it into another script. Inside InfraPy, under if __name__=='__main__':, the listToText function works just fine. From InfraPy I can import other functions with no problems. Including from InfraPy import * in my script does not return any errors until I try to use the listToText function.
How can this occur?
How can importing one particular function return a NameError, while importing all the other functions in the same module works fine?
Using python 2.6 on MacOSX 10.6, also encountered the same error running the script on Windows 7, using IronPython 2.6 for .NET 4.0
Thanks.
If there are other details you think would be helpful in solving this, I'd be happy to provide them.
As requested, here is the function definition inside of InfraPy:
def listToText(inputList, folder=None, outputName='list.txt'):
'''
Creates a text file from a list (with each list item on a separate line). May be placed in any given folder, but will otherwise be created in the working directory of the python interpreter.
'''
fname = outputName
if folder != None:
fname = folder+'/'+fname
f = open(fname, 'w')
for file in inputList:
f.write(file+'\n')
f.close()
This function is defined above and outside of if __name__=='__main__':
I've tried moving InfraPy around in relation to the script. The most baffling situation is that when InfraPy is in the same folder as the script, and I import using from InfraPy import listToText, I receive this error: NameError: name listToText is not defined. Again, the other functions import fine, they are all defined outside of if __name__=='__main__': in InfraPy.
This could happen if the module has __all__ defined
Alternatively there could be another version of the module in your path that is getting imported instead of the one you are expecting
Is the NameError about listToText or is it something inside the function causing the exception?
In addition the __all__ variable gnibbler mentioned you could also have a problem with a InfraPy.pyc file lying around somewhere.
I'd recommend putting a import pdb;pdb.set_trace() first in the InfraPy.py file to make sure you are in the right file, and step through the definition of InfraPy.py to see what is happening. If you don't get a breakpoint, you are importing another file than you think.
You can also dir(InfraPy) after importing it, and check which file you are actually importing with InfraPy.__file__.
Can't think of any more import debugging hints right now. ;-)
This question already has answers here:
Importing installed package from script with the same name raises "AttributeError: module has no attribute" or an ImportError or NameError
(2 answers)
Closed 6 years ago.
If I start python from the command line and type:
import random
print "Random: " + str(random.random())
It prints me a random number (Expected, excellent).
If I include the above-two lines in my django application's models.py and start my django app with runserver I get the output on the command line showing me a random number (Great!)
If I take a custom tag which works perfectly fine otherwise, but I include
import random
print "Random: " + str(random.random())
as the first 2 lines of the custom tag's .py file, I get an error whenever I try to open up a template which uses that custom tag:
TypeError at /help/
'module' object is not callable
Please keep in mind that if I get rid of these two lines, my custom tag behaves as otherwise expected and no error is thrown. Unfortunately, I need some random behavior inside of my template tag.
The problem is if in a custom tag I do:
import random
on a custom template tag, it imports
<module 'django.templatetags.random' from '[snip path]'>
and not
<module 'random' from 'C:\\Program Files\\Python26\\lib\\random.pyc'>
as is normally imported from everywhere else
Django template library has a filter called random, and somehow it is getting priority above the system's random.
Can anyone recommend how to explicitly import the proper python random?
The answer is ... strange.
When I originally wrote my custom tag, I called it random.py. I quickly realized that this name may not be good and renamed it randomchoice.py and deleted my random.py file. Python kept the compiled random.pyc file around, and it was getting loaded whenever I did import random. I removed my random.pyc file, and the problem went away.
Yes, this kind of error is pretty easy. Basically don't name any of your filenames or anything you create with the same names as any likely python modules you are going to use.
Its been a while since I tinkered around with Django, but if random.random is a "module", then try random.random.random(). Or maybe just try random(). You just don't know what kind of hackery goes on behind the scenes.
Edit
Try this:
sys.path = [r"C:\Program Files\Python26\lib\"] + sys.path
import random
sys.path.pop(0)