从模块调用方法
Calling method from module
我是 python 的新手,在使用我的代码时遇到问题。
我有两个不同的模块,在模块 a.py 中,我放入了所有方法,在模块 b.py 中,我放入了逻辑。
我使用的开发环境是Sypder
# module a
import serial
ser = serial.Serial()
def serWrite ( str ):
ser.write (str + "\x0D")
print "Write data :", str + "\x0D"
time.sleep(0.1)
return
def configuration():
flagAT = 0
while (flagAT == 0):
serWrite("at")
while True:
ok = ser.readline()
if (ok[0:2] == "OK"):
print ("AT OK, DEVICE CONNECTED" + "\x0D")
flagAT = 1
break
else:
print "DEVICE NOT CONNECTED OR NOT WORKING"
break
print("Starting with configuration")
模块b.py:
#module b
import serial
import a
ser = serial.Serial()
ser.port = "/dev/ttyS1"
ser.baudrate = 115200
### more serial configuration here###
try:
ser.open()
except Exception, e:
print "error open serial port: " + str(e)
exit()
configuration()
现在进入正题:
当我 运行 模块 b.py AFTER 运行ning a.py INSIDE Spyder 一切都按预期工作。
但是:Spyder 代码分析告诉我
'a' imported but unused
当我尝试调用模块 b.py OUTSIDE Spyder 时出现以下错误
pi@(none) ~/WorkingDirectory $ python b.py
Traceback (most recent call last):
File "b.py", line 83, in <module>
configuration()
NameError: name 'configuration' is not defined
正在导入配置():
import a
a.configuration()
给出以下错误:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.7/dist-packages/spyderlib/widgets/externalshell/sitecustomize.py", line 540, in runfile
execfile(filename, namespace)
File "/home/workingDirectory/test.py", line 85, in <module>
a.configuration()
File "a.py", line 336, in configuration
serWrite("at")
File "a.py", line 16, in serWrite
ser.write (str + "\x0D")
File "build/bdist.linux-i686/egg/serial/serialposix.py", line 490, in write
serial.serialutil.SerialException: Attempting to use a port that is not open
我不明白为什么我的程序 运行在 spyder 中没有问题,但在外部却没有。
有人可以帮忙吗?
您需要将模块 b 更改为:
#module b
import serial
from a import ser, configuration
#ser = serial.Serial()
ser.port = "/dev/ttyS1"
ser.baudrate = 115200
### more serial configuration here###
try:
ser.open()
except Exception, e:
print "error open serial port: " + str(e)
exit()
configuration()
我是 python 的新手,在使用我的代码时遇到问题。
我有两个不同的模块,在模块 a.py 中,我放入了所有方法,在模块 b.py 中,我放入了逻辑。
我使用的开发环境是Sypder
# module a
import serial
ser = serial.Serial()
def serWrite ( str ):
ser.write (str + "\x0D")
print "Write data :", str + "\x0D"
time.sleep(0.1)
return
def configuration():
flagAT = 0
while (flagAT == 0):
serWrite("at")
while True:
ok = ser.readline()
if (ok[0:2] == "OK"):
print ("AT OK, DEVICE CONNECTED" + "\x0D")
flagAT = 1
break
else:
print "DEVICE NOT CONNECTED OR NOT WORKING"
break
print("Starting with configuration")
模块b.py:
#module b
import serial
import a
ser = serial.Serial()
ser.port = "/dev/ttyS1"
ser.baudrate = 115200
### more serial configuration here###
try:
ser.open()
except Exception, e:
print "error open serial port: " + str(e)
exit()
configuration()
现在进入正题: 当我 运行 模块 b.py AFTER 运行ning a.py INSIDE Spyder 一切都按预期工作。
但是:Spyder 代码分析告诉我
'a' imported but unused
当我尝试调用模块 b.py OUTSIDE Spyder 时出现以下错误
pi@(none) ~/WorkingDirectory $ python b.py
Traceback (most recent call last):
File "b.py", line 83, in <module>
configuration()
NameError: name 'configuration' is not defined
正在导入配置():
import a
a.configuration()
给出以下错误:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.7/dist-packages/spyderlib/widgets/externalshell/sitecustomize.py", line 540, in runfile
execfile(filename, namespace)
File "/home/workingDirectory/test.py", line 85, in <module>
a.configuration()
File "a.py", line 336, in configuration
serWrite("at")
File "a.py", line 16, in serWrite
ser.write (str + "\x0D")
File "build/bdist.linux-i686/egg/serial/serialposix.py", line 490, in write
serial.serialutil.SerialException: Attempting to use a port that is not open
我不明白为什么我的程序 运行在 spyder 中没有问题,但在外部却没有。 有人可以帮忙吗?
您需要将模块 b 更改为:
#module b
import serial
from a import ser, configuration
#ser = serial.Serial()
ser.port = "/dev/ttyS1"
ser.baudrate = 115200
### more serial configuration here###
try:
ser.open()
except Exception, e:
print "error open serial port: " + str(e)
exit()
configuration()