在 Visual Studio Code 中使用 Pybluez 时出现“ImportError: No module named bluetooth”

ASB*_*SB2 5 python visual-studio-code

操作系统:Linux Mint

我正在尝试使用 Pybluez 为 python 项目设置开发环境。我通过包管理器安装了 Visual Studio Code,然后安装了 Microsoft 的 Python 扩展。

然后我按照 pybluez 安装说明通过在终端中运行来安装

sudo apt-get install libbluetooth-dev
sudo apt-get install python-dev
sudo pip install PyBluez
Run Code Online (Sandbox Code Playgroud)

这似乎有效,我能够在命令行上运行一个简单的测试脚本

from bluetooth import *

nearby_devices = discover_devices()

print(nearby_devices)
Run Code Online (Sandbox Code Playgroud)

但是,当尝试在 Visual Studio Code 中运行此文件时,我继续收到 ImportError

Traceback (most recent call last):
  File "/bluesniffer.py", line 1, in <module>
    from bluetooth import *
ImportError: No module named bluetooth
Run Code Online (Sandbox Code Playgroud)

我试图在 Visual Studio 代码终端中运行 pip install pybluez 但是我收到这个错误。

sh-4.4$ pip install pybluez
Collecting pybluez
  Cache entry deserialization failed, entry ignored
  Using cached https://files.pythonhosted.org/packages/c1/98/3149481d508bee174335be6725880f00d297afebe75c15e917af8f6fe169/PyBluez-0.22.zip
Building wheels for collected packages: pybluez
  Running setup.py bdist_wheel for pybluez ... error
  Complete output from command /usr/bin/python2.7 -u -c "import setuptools, tokenize;__file__='/tmp/pip-build-4ccFdE/pybluez/setup.py';f=getattr(tokenize, 'open', open)(__file__);code=f.read().replace('\r\n', '\n');f.close();exec(compile(code, __file__, 'exec'))" bdist_wheel -d /tmp/tmpmLzN2Cpip-wheel- --python-tag cp27:
  running bdist_wheel
  running build
  running build_py
  creating build
  creating build/lib.linux-x86_64-2.7
  creating build/lib.linux-x86_64-2.7/bluetooth
  copying bluetooth/__init__.py -> build/lib.linux-x86_64-2.7/bluetooth
  copying bluetooth/widcomm.py -> build/lib.linux-x86_64-2.7/bluetooth
  copying bluetooth/osx.py -> build/lib.linux-x86_64-2.7/bluetooth
  copying bluetooth/msbt.py -> build/lib.linux-x86_64-2.7/bluetooth
  copying bluetooth/btcommon.py -> build/lib.linux-x86_64-2.7/bluetooth
  copying bluetooth/bluez.py -> build/lib.linux-x86_64-2.7/bluetooth
  copying bluetooth/ble.py -> build/lib.linux-x86_64-2.7/bluetooth
  running build_ext
  building 'bluetooth._bluetooth' extension
  creating build/temp.linux-x86_64-2.7
  creating build/temp.linux-x86_64-2.7/bluez
  x86_64-unknown-linux-gnu-gcc -pthread -fno-strict-aliasing -march=x86-64 -mtune=generic -O2 -g -pipe -Wp,-D_FORTIFY_SOURCE=2 -Wp,-D_GLIBCXX_ASSERTIONS -fexceptions -fstack-protector-strong-grecord-gcc-switches -fasynchronous-unwind-tables -fstack-clash-protection -fcf-protection -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -fPIC -I./port3 -I/usr/include/python2.7 -c bluez/btmodule.c -o build/temp.linux-x86_64-2.7/bluez/btmodule.o
  In file included from bluez/btmodule.c:20:
  bluez/btmodule.h:5:10: fatal error: bluetooth/bluetooth.h: No such file or directory
   #include <bluetooth/bluetooth.h>
            ^~~~~~~~~~~~~~~~~~~~~~~
  compilation terminated.
  error: command 'x86_64-unknown-linux-gnu-gcc' failed with exit status 1

  ----------------------------------------
  Failed building wheel for pybluez
  Running setup.py clean for pybluez
Failed to build pybluez
Installing collected packages: pybluez
  Running setup.py install for pybluez ... error
    Complete output from command /usr/bin/python2.7 -u -c "import setuptools, tokenize;__file__='/tmp/pip-build-4ccFdE/pybluez/setup.py';f=getattr(tokenize, 'open', open)(__file__);code=f.read().replace('\r\n', '\n');f.close();exec(compile(code, __file__, 'exec'))" install --record /tmp/pip-9O19sX-record/install-record.txt --single-version-externally-managed --compile:
    running install
    running build
    running build_py
    creating build
    creating build/lib.linux-x86_64-2.7
    creating build/lib.linux-x86_64-2.7/bluetooth
    copying bluetooth/__init__.py -> build/lib.linux-x86_64-2.7/bluetooth
    copying bluetooth/widcomm.py -> build/lib.linux-x86_64-2.7/bluetooth
    copying bluetooth/osx.py -> build/lib.linux-x86_64-2.7/bluetooth
    copying bluetooth/msbt.py -> build/lib.linux-x86_64-2.7/bluetooth
    copying bluetooth/btcommon.py -> build/lib.linux-x86_64-2.7/bluetooth
    copying bluetooth/bluez.py -> build/lib.linux-x86_64-2.7/bluetooth
    copying bluetooth/ble.py -> build/lib.linux-x86_64-2.7/bluetooth
    running build_ext
    building 'bluetooth._bluetooth' extension
    creating build/temp.linux-x86_64-2.7
    creating build/temp.linux-x86_64-2.7/bluez
    x86_64-unknown-linux-gnu-gcc -pthread -fno-strict-aliasing -march=x86-64 -mtune=generic -O2 -g -pipe -Wp,-D_FORTIFY_SOURCE=2 -Wp,-D_GLIBCXX_ASSERTIONS -fexceptions -fstack-protector-strong -grecord-gcc-switches -fasynchronous-unwind-tables -fstack-clash-protection -fcf-protection -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -fPIC -I./port3 -I/usr/include/python2.7 -c bluez/btmodule.c -o build/temp.linux-x86_64-2.7/bluez/btmodule.o
    In file included from bluez/btmodule.c:20:
    bluez/btmodule.h:5:10: fatal error: bluetooth/bluetooth.h: No such file or directory
     #include <bluetooth/bluetooth.h>
              ^~~~~~~~~~~~~~~~~~~~~~~
    compilation terminated.
    error: command 'x86_64-unknown-linux-gnu-gcc' failed with exit status 1

    ----------------------------------------
Command "/usr/bin/python2.7 -u -c "import setuptools, tokenize;__file__='/tmp/pip-build-4ccFdE/pybluez/setup.py';f=getattr(tokenize, 'open', open)(__file__);code=f.read().replace('\r\n', '\n');f.close();exec(compile(code, __file__, 'exec'))" install --record /tmp/pip-9O19sX-record/install-record.txt --single-version-externally-managed --compile" failed with error code 1 in /tmp/pip-build-4ccFdE/pybluez/
You are using pip version 9.0.3, however version 19.0.1 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
Run Code Online (Sandbox Code Playgroud)

我以前遇到过这个错误,它是通过运行修复的

sudo apt-get install libbluetooth-dev
Run Code Online (Sandbox Code Playgroud)

然而,这已经完成了,当我在普通终端中运行它时,程序编译得非常好。我不确定为什么它不能在 Visual Studio Code 中正确运行