我是Python新手。我想做的是在安装电子工具包(pip install esptool)之后用我的应用程序中的一些参数调用它的主要方法。类似于:
esptool.py -p /dev/ttyUSB0 write_flash -fm qio 0x0000我遇到了一个问题。esptool不在python中要导入的包列表中(它已经安装了pip )。我该如何使用import并调用主方法?
发布于 2018-06-09 14:22:22
解决进口问题
您不能简单地调用import esptool,因为esptool.py是一个可执行脚本,因此不打算像普通模块那样导入。但是,从可执行脚本导入代码有一些解决办法;以下是我所知道的两种方法:
扩展sys.path
您可以扩展sys.path以包括包含esptool.py脚本的bindir。从命令行进行简单检查:
$ PYTHONPATH=$(which esptool.py)/.. python -c "import esptool; esptool.main()"应该打印“使用帮助”文本。
在代码中扩展sys.path:
import os
import sys
try:
from shutil import which
except ImportError:
from distutils.spawn import find_executable as which
bindir = os.path.dirname(which('esptool.py'))
sys.path.append(bindir) # after this line, esptool becomes importable
import esptool
if __name__ == '__main__':
esptool.main()使用进口机械
您可以通过使用从任意文件导入Python代码的机制来避免扩展sys.path。我更喜欢这个解决方案,而不是摆弄sys.path,但不幸的是,它不能在Python2和3之间移植。
Python 3.5+
import importlib.machinery
import importlib.util
from shutil import which
if __name__ == '__main__':
loader = importlib.machinery.SourceFileLoader('esptool', which('esptool.py'))
spec = importlib.util.spec_from_loader(loader.name, loader)
esptool = importlib.util.module_from_spec(spec)
loader.exec_module(esptool) # after this line, esptool is imported
esptool.main()Python 2.7
import imp
from distutils.spawn import find_executable as which
if __name__ == '__main__':
esptool = imp.load_source('esptool', which('esptool.py'))
esptool.main()传递命令行参数
命令行参数存储在sys.argv列表中,因此您必须暂时覆盖它,以便将参数传递给主函数:
# assuming esptool is imported
import sys
if __name__ == '__main__':
# save the current arguments
argv_original = sys.argv[:]
# overwrite arguments to be passed to esptool argparser
sys.argv[:] = ['', '-p', '/dev/ttyUSB0', 'write_flash', '-fm', 'qio', '0x0000']
try:
esptool.main()
except Exception:
# TODO deal with errors here
pass
finally: # restore original arguments
sys.argv[:] = argv_originalhttps://stackoverflow.com/questions/50772342
复制相似问题