我写了一个可以从Pycharm完美运行的脚本。但是,如果我从终端运行此代码,则说找不到模块。
代码如下:
from rplidar import RPLidar, RPLidarException
lidar = RPLidar('/dev/ttyUSB0')
info = lidar.get_info()
print(info)
health = lidar.get_health()
print(health)
try:
for i, scan in enumerate(lidar.iter_scans()):
scan_data = []
for d in scan: #d[0] : Quality of the measurement
'''if 0< d[1] <15: #d[1] : Angle of the measurement
print(d[2]/10) #d[2] : Distance of the measurement '''
if False:
lidar.stop()
lidar.stop_motor()
lidar.disconnect()
break
except KeyboardInterrupt as err:
print('key board interupt')
lidar.stop()
lidar.stop_motor()
lidar.disconnect()
except RPLidarException as err:
print(err)
lidar.stop()
lidar.stop_motor()
lidar.disconnect()
except AttributeError:
print('hi attribute error')
当我以下面的方式运行此程序时:
python -m main.py
我收到此错误:
Traceback (most recent call last):
File "/usr/lib/python2.7/runpy.py", line 174, in _run_module_as_main
"__main__", fname, loader, pkg_name)
File "/usr/lib/python2.7/runpy.py", line 72, in _run_code
exec code in run_globals
File "/home/shahin/PycharmProjects/RpLidar/main.py", line 1, in <module>
from rplidar import RPLidar, RPLidarException
ImportError: No module named rplidar
shahin@shahin-UX360UAK:~/PycharmProjec
如果您告诉我如何克服这个问题,我将感到非常高兴。
同时,当我在pycharm中运行此代码时,我想从中捕获KeyboardInterrupt。但是它不起作用。即使我也做Contro + c。
有人可以告诉我谁也可以在pycharm中执行KeyboardInterrupt。
答案 0 :(得分:1)
如果显示的是python 3.7,请转到终端类型python3,然后键入python3 -m main.py
或python3 main.py