Yes, this question has been asked before. No, it did not answer my question satisfactorily.
So, I'm creating my Giraffe Program in Python first (don't ask) and I'm trying to get the user to name their giraffe.
My files are all in one package called code. In the file Main_Code, the function createGiraffe is called from code.Various_Functions. Here is my code.
import code
print("Welcome to The Animal Kingdom.")
userGiraffe = code.Various_Functions.createGiraffe()
And the code in code.Giraffes:
import code
def createGiraffe():
print("Name your giraffe.")
GiraffeName = input()
return GiraffeName
However, when I run Main_Code, it gives me this error:
Traceback (most recent call last):
File "C:\Users\Jonathan\Documents\Aptana Studio 3 Workspace\The Animal Kingdom\src\code\Main_Code.py", line 3, in <module>
userGiraffe = code.Giraffes.Giraffes.createGiraffe()
AttributeError: 'module' object has no attribute 'Giraffes'
How do I fix this? I believe that I've done everything by the book. It's all using the same package so I can call the function, I'm calling it from the right place, and the function has no syntax errors. Can anyone help with this?
Do
import code.Giraffes
before executing the offending line:
userGiraffe = code.Giraffes.Giraffes.createGiraffe()
When you call function like:
userGiraffe = code.Giraffes.Giraffes.createGiraffe()
it means you have a project in dir code with internal dir Giraffes and module Giraffes with function createGiraffe. Is this your exception?
Related
I cannot run the selected block of the code in VS Code.
Given the code that works well if I run it as a whole
import numpy as np
x = np.arange(5)
print(x)
if I select the line print(x) and press Shift+Enter, it yields
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'x' is not defined
It looks like the objects are erased from the memory as soon as the compilation is over.
Could somebody explain what is the reason and how to tackle this problem?
Thank you!
As you already know, the previous objects are erased with every execution of the code from the memory.
When you run just the print statement, it is like you would just run print(x) in a new file without defining it.
To my knowledge, this can't be changed, because the python interpreter works that way, and it creates a temporary file with the selected code and runs that. In that file are the objects not defined, and thus it raises an exception.
I'm trying to get the ODB library working. In the documentation at https://python-obd.readthedocs.io/en/latest/ it lists the following code:
import obd
connection = obd.OBD("/dev/ttyUSB0") # connects to USB or RF port
cmd = obd.commands.SPEED # select an OBD command (sensor)
response = connection.query(cmd) # send the command, and parse the response
print(response.value) # returns unit-bearing values thanks to Pint
print(response.value.to("mph")) # user-friendly unit conversions
When I put this in a file called test.py and I run it:
python2 test.py
I get the following error message:
Traceback (most recent call last):
File "test.py", line 1, in <module>
import obd
File "/home/ubuntu/obd.py", line 3, in <module>
AttributeError: 'module' object has no attribute 'OBD'
Stackoverflow comes up with several iterations of this error message, but none clearly explain the problem, only giving specific solutions to those libraries.
I guess it's obvious that I'm new to Python, and I'm having trouble interpreting this error message, even after several hours of writing several small Python programs. I am of course also interested in why the error message is so un-intuitive to a newcomer and where I can gain the common knowledge I might be missing, and I guess this is as good a case to discover that through as any.
So far I have figured out the following:
<module> refers to the name of my python script that I am trying to run.
The mistake I made at first, was to name my test.py file, initially obd.py. This conflicted with the import obd as it was trying to import the file itself - even after I deleted it! The reason was that when I tried to run it the first time, it created a file called obd.pyc - and even though obd.py was no longer there, import OBD found the initially created obd.pyc and tried to import that - which of course does not contain the OBD object from the library I was trying to use.
This is not the detailed answer I'm looking for, so please add a more detailed explanation - or a link to how Python compiling works, if you can.
I am wondering if it is possible to edit/customize the behavior and printout of built-in errors in Python. For example, if I type:
>>> a = 1
>>> print A
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'A' is not defined
I want the output to instead be:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'A' is not defined. Check capitalization.
Moreover, I want this to occur at a global level, for ALL FUTURE CODE, without having to explicitly include an exception in my code. If such a change is possible, I would assume this needs to be done at the very source or library-file level of Python. However, I am not sure where exactly to look to know if this is even possible.
I am using Python 2.7 on both Ubuntu and OSX, so help on either system would be appreciated.
(My apologies in advance if this is covered elsewhere, but searching for threads on "changing Python error messages" generally gave me topics on Exceptions, which is not necessarily my interest here. If anyone can point me to a page on this though, I'd greatly appreciate it.)
YES! There is a way to exactly what you want!
traceback.py is the program that detects errors in your code. It then gives you an explanation of what happened (creates the error message that you see.)
You can find this file in your library folder for python.
When in that file you can change the messages that it outputs when you come across an error!
Please tell me if this helped you!
Hello everyone i am new to the python language and i have chosen learn python the hard way to learn it and to better my understanding... I am stumped on exercise 25 , When we import the code directly into the terminal
>>> import ex25
>>> sentence = "All good things come to those who wait."
>>> words = ex25.break_words(sentence)
And then I get an attribute error
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'break_words'
I am using python 2.7 on
windows 7 please help..... http://learnpythonthehardway.org/book/ex25.html
It appears to me that the exercise does not instruct the learner to save the file prior to the import. In order for this to work, you've got to save the code that defines the break_words function in a file called ex25.py using your text editor. Then, from the same directory open the python interpreter by typing:
python
and you should be able to import ex25 and run the break_words function which the ex25.py module has defined.
The code in your link for ex25.py does include that function - that yours doesn't suggests that you've somehow missed it when you transcribed the code into your file. Check that your ex25.py includes all the code from the page, and in particular contains this function (it's the very top one):
def break_words(stuff):
"""This function will break up words for us."""
words = stuff.split(' ')
return words
Consider pasting the code into your editor in preference to transcribing it in order to avoid errors like this.
I have a program that runs in Python, without the console present (which is to say, I've compiled it using py2exe). I want people to be able to quit from the main menu, or by a particular key-press in-game (say, Shift+Q). I'm running it, for now, in Windows, though I am working on compiling Linux/Mac versions. Pressing the X button works if there's no 'while' loop running, it seems, and that closes it correctly, otherwise it seems to 'store' the close command wait until the current loop is closed.
As for menu options, I've looked thoroughly through documentation and Stackoverflow and tried quit(), exit(), sys.exit() and every combination I can find, but every time I get:
Traceback (most recent call last):
File "alphatime.pyw", line 61177, in <module>
File "alphatime.pyw", line 53970, in place_menu
NameError: global name 'sys' is not defined
if I try sys.exit, and then:
Traceback (most recent call last):
File "alphatime.pyw", line 61177, in <module>
File "alphatime.pyw", line 53970, in place_menu
NameError: global name 'quit' is not defined
if I try just "quit()". I've heard about 'Raising' things like a need to close the program, but I'm not clear what that means (I'm new to Python) and how I would go about doing that.
So, my question is two-fold.
Firstly, is there something I can put in loops for recognizing keypresses something that will recognize the 'X' being clicked, and close?
Secondly, is there an appropriate command that will just close the program? I cannot figure out why these commands don't work, and I've had quite a few complaints from people using the program that it crashes, or they have to ctrl-alt-del it, or whatever. I believe
import os
try:
os._exit(return_code)
except:
pass
would work, but at this point, I'm not sure I'm competent enough at python to deploy it appropriately. Thanks in advance!
did you by any chance
import sys
because that should work!
NameError: global name 'sys' is not defined
Before you can use sys.exit(), you must import sys.
That's the best way to exit the program. Function names that begin with _ are considered internal, and should not be used unless you are really trying to do something weird.