简体   繁体   English

没有名为pcapy pyinstaller的模块

[英]No module named pcapy pyinstaller

This is my first time using pyinstaller and after I build my executable and try to run my executable I get ImportError: No module named pcapy . 这是我第一次使用pyinstaller并且在构建可执行文件并尝试运行可执行文件后,我得到ImportError: No module named pcapy Can someone tell me why the module might be missing? 有人可以告诉我为什么可能缺少该模块吗? Not sure if it helps but my script is 1 file and here are all my imports: 不确定是否有帮助,但是我的脚本是1个文件,这是我所有的导入文件:

from tornado.websocket import WebSocketHandler
from tornado.httpserver import HTTPServer
from tornado.web import Application
from tornado.ioloop import IOLoop

import subprocess
import threading
import os

import sys
from scapy.all import *
import logging

I'm running OS X 10.10.5, scapy 2.3.1 and python 2.7.9 我正在运行OS X 10.10.5,Scapy 2.3.1和python 2.7.9

Create a file called hook-pcap.py with the following content: 创建一个名为hook-pcap.py的文件,其内容如下:

import os
import glob
import itertools

try:
    # PY_EXTENSION_SUFFIXES is unavailable in older versions
    from PyInstaller.hooks.hookutils import PY_EXTENSION_SUFFIXES
except ImportError:
    try:
        from importlib.machinery import EXTENSION_SUFFIXES as PY_EXTENSION_SUFFIXES
    except ImportError:
        import imp
        PY_EXTENSION_SUFFIXES = set([f[0] for f in imp.get_suffixes()
                                     if f[2] == imp.C_EXTENSION])

def hook(mod):
    module_directory = os.path.dirname(mod.__file__)
    bundled = []

    for libname, ext in itertools.product(('pcap', ),
                                          PY_EXTENSION_SUFFIXES):
        bundled.extend(glob.glob(os.path.join(module_directory, libname + ext)))

    for f in bundled:
        name = os.path.join('pcap', os.path.basename(f))
        if hasattr(mod, 'pyinstaller_binaries'):
            mod.pyinstaller_binaries.append((name, f, 'BINARY'))
        else: # mod.pyinstaller_binaries is unavailable in older versions
            mod.binaries.append((name, f, 'BINARY'))

    return mod

Repeat the process with hook-dnet.py : 使用hook-dnet.py重复该过程:

import os
import glob
import itertools

try:
    # PY_EXTENSION_SUFFIXES is unavailable in older versions
    from PyInstaller.hooks.hookutils import PY_EXTENSION_SUFFIXES
except ImportError:
    try:
        from importlib.machinery import EXTENSION_SUFFIXES as PY_EXTENSION_SUFFIXES
    except ImportError:
        import imp
        PY_EXTENSION_SUFFIXES = set([f[0] for f in imp.get_suffixes()
                                     if f[2] == imp.C_EXTENSION])

def hook(mod):
    module_directory = os.path.dirname(mod.__file__)
    bundled = []

    for libname, ext in itertools.product(('dnet', ),
                                          PY_EXTENSION_SUFFIXES):
        bundled.extend(glob.glob(os.path.join(module_directory, libname + ext)))

    for f in bundled:
        name = os.path.join('dnet', os.path.basename(f))
        if hasattr(mod, 'pyinstaller_binaries'):
            mod.pyinstaller_binaries.append((name, f, 'BINARY'))
        else: # mod.pyinstaller_binaries is unavailable in older versions
            mod.binaries.append((name, f, 'BINARY'))

    return mod

When building, specify pcap and dnet as hidden imports and supply the path to the directory in which you placed the hook files as a value to the --additional-hooks-dir argument, as follows: 生成时,将pcapdnet指定为隐藏的导入,并提供--additional-hooks-dir参数的值,将放置钩子文件的目录的路径提供给它,如下所示:

--hidden-import=pcap --hidden-import=dnet --additional-hooks-dir=<path_to_directory_of_hook_files>

I also encounter this problem, when I want to use pyinstaller to convert a python script, which use pcapy module directly, to a executable. 当我想使用pyinstaller将直接使用pcapy模块的python脚本转换为可执行文件时,也会遇到此问题。 And below is my solution. 下面是我的解决方案。

Enter the python module directory, where is Lib/site-packages in Windows. 输入python模块目录,在Windows中为Lib / site-packages。 And I found that the pcapy module existed as a single file, named 'pcapy-0.10.9-py2.7-win-amd64.egg'. 我发现pcapy模块作为一个文件存在,名为“ pcapy-0.10.9-py2.7-win-amd64.egg”。 I thought it may be some compress technique, and pyinstaller was unable to analysis this compressed file in that time. 我认为这可能是某种压缩技术,而pyinstaller当时无法分析此压缩文件。 So I extract the 'pcapy-0.10.9-py2.7-win-amd64.egg', and delete the original 'pcapy-0.10.9-py2.7-win-amd64.egg' file, rename the extracted directory to 'pcapy-0.10.9-py2.7-win-amd64.egg'. 因此,我提取了'pcapy-0.10.9-py2.7-win-amd64.egg',并删除了原始的'pcapy-0.10.9-py2.7-win-amd64.egg',将提取的目录重命名为'pcapy-0.10.9-py2.7-win-amd64.egg'。

Then, I converted the script again, and this time, the executable worked right, no any import error. 然后,我再次转换了脚本,这次,可执行文件运行正常,没有任何导入错误。

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

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