问题
I am making a little game thing in Python, I'm still fairly new to this. I am trying to access a variable in another file by using import, but it keeps saying
AttributeError: module 'core temp' has no attribute 'ct'
This is my code I am trying to run:
elif cmd == "temp":
if core.ct < 2000:
print(colored("Core temperature: "+core.ct+"°C", "green"))
elif core.ct < 4000:
print(colored("Core temperature: "+core.ct+"°C", "yellow"))
elif core.ct >= 3000:
print(colored("Core temperature: "+core.ct+"°C", "red"))
I am importing coretemp like this: import coretemp as core
This is the code in coretemp.py:
from time import sleep as wait
import coolant as c
ct = 10
while True:
if c.coolactive == False:
ct = ct + 1
wait(.3)
else:
ct = ct - 1
wait(1)
I've been stuck on this problem for ages!
PS: sorry if things aren't formatted right, I'm on mobile and it's hard.
回答1:
The only way I can see you would get this error is if the coolant
module also imports coretemp
. (BTW, I am assuming here the space in core temp
was a copy/paste error)
If coolant
imports coretemp
it will be accessing a copy of the module as it existed when coretemp
imported coolant
. That would mean ct
is not yet defined.
Note that the import in main
will never complete as coretemp.py
contains an infinite loop at the top level: main
will simply wait for ever for the module it is importing to finish executing.
来源:https://stackoverflow.com/questions/50353079/using-variables-from-other-python-file