模块未加载,同时从终端运行python脚本

2024-06-16 10:41:27 发布

您现在位置:Python中文网/ 问答频道 /正文

我写了一个剧本,完全符合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')

当我在下面运行这个scit时:

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中运行这个代码时,我想从中捕获键盘中断。但它不起作用。即使是我也能控制

有人能告诉我谁也可以在pycharm中做键盘中断吗。你知道吗


Tags: ofthe代码inpyinfoscanmain