Python 使用pyinstaller导入外部.exe文件
我正在尝试使用pyinstaller将Python代码库绑定到一个.exe文件中 我的代码使用nmap执行shell命令。这在开发中效果很好,因为我在开发环境中单独安装了nmap 但是,如果我想在另一台机器上以exe的形式运行它(而不在该机器上单独安装nmap),我似乎无法做到这一点 我已经研究了pyinstaller的--add binary选项,下面是我如何使用它的:Python 使用pyinstaller导入外部.exe文件,python,windows,shell,pyinstaller,nmap,Python,Windows,Shell,Pyinstaller,Nmap,我正在尝试使用pyinstaller将Python代码库绑定到一个.exe文件中 我的代码使用nmap执行shell命令。这在开发中效果很好,因为我在开发环境中单独安装了nmap 但是,如果我想在另一台机器上以exe的形式运行它(而不在该机器上单独安装nmap),我似乎无法做到这一点 我已经研究了pyinstaller的--add binary选项,下面是我如何使用它的: pyinstaller --onefile --add-binary "C:\Program Files (x86
pyinstaller --onefile --add-binary "C:\Program Files (x86)\Nmap\nmap.exe";. --paths=path_to_my_python_file python_file_to_run -n name_of_my_exe_file
生成的exe文件能够导入特定于Python的依赖项并执行所有其他Python代码,没有任何困难,但它无法运行nmap shell命令
这就是我通过Python运行nmap的方式(为了简洁起见,省略了完整命令):
pyinstaller --paths=path_to_my_python_file --noconfirm python_file_to_run --noconfirm -n name_of_exe_file
使用robocopy复制我需要的所有nmap文件:
robocopy path_to_nmap_source_directory path_to_installation_destination_directory /COPYALL /E /NFL /NDL /NJH /NJS /nc /ns /np
获取nmap执行路径的Python代码():
def get_base_path():
base_path = ""
if getattr(sys, 'frozen', False):
base_path = sys._MEIPASS
return base_path
def get_exe_path(file):
#file is the name of your exe file e.g nmap.exe
base_path = get_base_path()
exe_path = os.path.join(base_path, file)
return exe_path
您最好的选择是使用nmap的python实现,在pip上有很多实现,例如,我建议下载纯python实现并使用它而不是exe。为什么要通过powershell运行它,如果nmap.exe不在路径上,它无论如何都不会工作,为什么不直接执行它呢?不管怎样,我需要将nmap与exe文件捆绑在一起,对吗?除非您这样做,否则您将无法运行它-从未尝试过捆绑exe,但您可以捆绑二进制文件,所以为什么不试试呢?好的,我不太确定您的建议是什么。您能详细说明一下吗?根据@barnyYes的建议,更新了问题和工作解决方案,我知道有python实现。但是,我似乎无法获得python nmap所需的相同级别的详细信息。i、 e shell命令返回更全面的信息。
def get_base_path():
base_path = ""
if getattr(sys, 'frozen', False):
base_path = sys._MEIPASS
return base_path
def get_exe_path(file):
#file is the name of your exe file e.g nmap.exe
base_path = get_base_path()
exe_path = os.path.join(base_path, file)
return exe_path