简体   繁体   English

使用 pyinstaller 导入 external.exe 文件

[英]Import external .exe file with pyinstaller

I'm trying to bundle my Python codebase into an.exe file using pyinstaller.我正在尝试使用 pyinstaller 将我的 Python 代码库捆绑到一个 .exe 文件中。

My code executes a shell command using nmap.我的代码使用 nmap 执行 shell 命令。 This works fine in development because I have nmap installed separately on my dev environment.这在开发中运行良好,因为我在我的开发环境中单独安装了 nmap。

However, if I want run this as an exe on another machine (without installing nmap separately on that machine) I can't seem to do this.但是,如果我想在另一台机器上将其作为 exe 运行(无需在该机器上单独安装 nmap),我似乎无法做到这一点。

I've looked into the --add-binary option of pyinstaller and this is how I'm using it:我查看了 pyinstaller 的 --add-binary 选项,这就是我使用它的方式:

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

The resulting exe file is able to import Python specific dependencies and execute all the other Python code without any difficulties but it cannot run the nmap shell command.生成的 exe 文件能够毫无困难地导入 Python 特定依赖项并执行所有其他 Python 代码,但它无法运行 nmap shell 命令。

This is how I'm running nmap through Python (full command omitted for brevity):这就是我通过 Python 运行 nmap 的方式(为简洁起见,省略了完整的命令):

subprocess.Popen(["powershell.exe", "nmap" )],
        stdout=subprocess.PIPE,
        stderr=subprocess.PIPE)
  1. Is it even possible to bundle nmap with my exe file?甚至可以将 nmap 与我的 exe 文件捆绑在一起吗?
  2. If yes, am I doing it correctly?如果是,我做得对吗?
  3. If not, what are my options?如果没有,我有什么选择? (I can't use any python based nmap libraries like python-nmap etc.) (我不能使用任何基于 python 的 nmap 库,如 python-nmap 等)

[UPDATE] Working Solution: Final pyinstaller statement: [更新] 工作解决方案:最终的 pyinstaller 声明:

pyinstaller --paths=path_to_my_python_file --noconfirm python_file_to_run --noconfirm -n name_of_exe_file 

Used robocopy to copy all the nmap files I needed:使用 robocopy 复制我需要的所有 nmap 文件:

robocopy  path_to_nmap_source_directory path_to_installation_destination_directory /COPYALL /E /NFL /NDL /NJH /NJS /nc /ns /np

Python code ( Bundling data files with PyInstaller (--onefile) ) to get execution path of nmap: Python代码( Bundling data files with PyInstaller (--onefile) )获取nmap的执行路径:

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

Your best bet would be to use a python implementation of nmap, there are many on pip such as python-nmap , I would suggest downloading a pure python implementation and using that instead of an exe.您最好的选择是使用 nmap 的 python 实现,pip 上有很多,例如python-nmap ,我建议下载纯 python 实现并使用它而不是

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM