pyinstaller-hooks-contrib icon indicating copy to clipboard operation
pyinstaller-hooks-contrib copied to clipboard

Need hook for pypcap on windows

Open jackadam1981 opened this issue 7 years ago • 11 comments

i try build exe with pyinstaller.

the test.py is print('hello')

the warntest.txt

missing module named resource - imported by posix, D:\PythonTest\\test.py
missing module named posix - imported by os, D:\PythonTest\\test.py
missing module named _posixsubprocess - imported by subprocess, D:\PythonTest\\test.py
missing module named 'org.python' - imported by pickle, D:\PythonTest\\test.py, xml.sax
missing module named readline - imported by cmd, code, pdb, D:\PythonTest\\test.py
excluded module named _frozen_importlib - imported by importlib, importlib.abc, D:\PythonTest\\test.py
missing module named _frozen_importlib_external - imported by importlib._bootstrap, importlib, importlib.abc, D:\PythonTest\\test.py
missing module named _winreg - imported by platform, D:\PythonTest\\test.py
missing module named _scproxy - imported by urllib.request
missing module named java - imported by platform, D:\PythonTest\\test.py
missing module named 'java.lang' - imported by platform, D:\PythonTest\\test.py, xml.sax._exceptions
missing module named vms_lib - imported by platform, D:\PythonTest\\test.py
missing module named termios - imported by tty, D:\PythonTest\\test.py, getpass
missing module named grp - imported by shutil, tarfile, D:\PythonTest\\test.py
missing module named pwd - imported by posixpath, shutil, tarfile, http.server, webbrowser, D:\PythonTest\\test.py, netrc, getpass
missing module named _dummy_threading - imported by dummy_threading, D:\PythonTest\\test.py
missing module named org - imported by copy, D:\PythonTest\\test.py

dist test.exe work on some computer.

then i try add code with pypcap:

import pcap
pc = pcap.pcap()
for d,b in pc:
    print(d,b)

pyinstaller test.py

the warntest.txt same old one. no missing pcap or pypcap.
missing module named resource - imported by posix, D:\PythonTest\\test.py
missing module named posix - imported by os, D:\PythonTest\\test.py
missing module named _posixsubprocess - imported by subprocess, D:\PythonTest\\test.py
missing module named org - imported by pickle, D:\PythonTest\\test.py
missing module named readline - imported by cmd, code, pdb, D:\PythonTest\\test.py
excluded module named _frozen_importlib - imported by importlib, importlib.abc, D:\PythonTest\\test.py
missing module named _frozen_importlib_external - imported by importlib._bootstrap, importlib, importlib.abc, D:\PythonTest\\test.py
missing module named _winreg - imported by platform, D:\PythonTest\\test.py
missing module named _scproxy - imported by urllib.request
missing module named java - imported by platform, D:\PythonTest\\test.py
missing module named 'java.lang' - imported by platform, D:\PythonTest\\test.py, xml.sax._exceptions
missing module named vms_lib - imported by platform, D:\PythonTest\\test.py
missing module named termios - imported by tty, D:\PythonTest\\test.py, getpass
missing module named grp - imported by shutil, tarfile, D:\PythonTest\\test.py
missing module named pwd - imported by posixpath, shutil, tarfile, http.server, webbrowser, D:\PythonTest\\test.py, netrc, getpass
missing module named _dummy_threading - imported by dummy_threading, D:\PythonTest\\test.py
missing module named 'org.python' - imported by copy, D:\PythonTest\\test.py, xml.sax

dist test.exe worked on my build computer. the other computer prompt DLL load failed:

PS C:\Users\Jack\Desktop\dist\test> .\test.exe
hello
Traceback (most recent call last):
  File "test.py", line 10, in <module>
  File "D:\PythonEnvi\lib\site-packages\PyInstaller\loader\pyimod03_importers.py", line 714, in load_module
ImportError: DLL load failed: 找不到指定的模块。
[2360] Failed to execute script test

the line 10 is : import pcap

so i try other way : pyinstaller.exe --hidden-import pcap test.py pyinstaller.exe -p D:\PythonEnvi\Lib\site-packages --hidden-import pcap D:\PythonTest\test.py

https://github.com/pynetwork/pypcap the pcap homepage.

windows10 Home 64bit python 3.63 pyinstaller 3.3.1 pypcap 1.20 npcap 0.98 installer npcap sdd 0.1 Microsoft Visual C++ Build Tools 2015

jackadam1981 avatar Feb 08 '18 05:02 jackadam1981

This looks like not all required files are packages. Please have a look at Make sure everything is packaged correctly for tips to find out what is missing. Esp use NTstrace to find which dll is missing.

We would appreciate if you'd submit a hook for this. See https://pyinstaller.readthedocs.org/en/latest/hooks.html for more information about how to implement hooks.

htgoebel avatar Feb 08 '18 22:02 htgoebel

my englist is bad. can't understand hooks. so try get some help.

jackadam1981 avatar Feb 09 '18 08:02 jackadam1981

Did you install the Windows 10 SDK?

I'm pretty sure I had the same issue with an executable not running on a different computer when I hadn't included the ucrt files:

Update the pathex line in your spec file to include the ucrt dlls.

x86: (32-bit windows) pathex=['C:\\Program Files (x86)\\Windows Kits\\10\\Redist\\ucrt\\DLLs\\x86']

x64: (64-bit windows) pathex=['C:\\Program Files (x86)\\Windows Kits\\10\\Redist\\ucrt\\DLLs\\x64']

josephalway avatar Feb 09 '18 21:02 josephalway

Accidentally ,I solved the problem. pypcap need npcap and winpcap API. different computer install npcap with winpcapAPI。 then build exe work on different computer。 my script use pyqt pypcap. i want start gui first. then check npcap.

but,build time, import pcap. start exe ,check import ,so DLL load failed. mybe some ,i try included the DLL in exe.

jackadam1981 avatar Feb 10 '18 02:02 jackadam1981

Can anyone tell me if this is possible? I'm trying to bundle a winpcap app ut cannot understand how to include the dlls.

arossert avatar Aug 11 '19 16:08 arossert

@arossert See above: You need to create a hook.

htgoebel avatar Aug 12 '19 08:08 htgoebel

@htgoebel thanks for the response but can you please point me to how can I achieve this with a hook?

From this conversation, it is hard to understand what needs to be done. I have created some hooks to some modules but all of them was to include some modules or source files and never external DLLs, so I'm a bit confused about how to begin.

arossert avatar Aug 12 '19 09:08 arossert

Just read hte manual an look at other hooks. You need to add the dll as binaries.

htgoebel avatar Aug 12 '19 14:08 htgoebel

@htgoebel I have tried to manually copy the files that are installed from Npcap to the target PyInstaller directory but still getting the DLL error exception. The files are Packet.lib & wpcap.lib, do you have any idea how to make it work? This is the simple example that i'm trying to build:

import pcapy

if the target machine has no Npcao/winpcap installed it is throwing the DLL exception.

arossert avatar Aug 12 '19 15:08 arossert

You don't need .lib files (static libraries used to compile an application), but the .dll files.

bjones1 avatar Aug 12 '19 16:08 bjones1

@bjones1 thanks, this was my mistake. I have successfully created a hook that takes the Packet.dll & wpcap.dll from the system32 lib and includes them in the final directory but when I copied it to another machine it is not working, I'm getting this error:

pcapy.PcapError: No valid interfaces to open

If I install the winpcap/npcap library and remove the DLLs from my application directory it is working so I'm not sure that it is even possible.

It might be because I have built it on win7 and tested it on win10, is there a way to make it portable?

UPDATE: I have tried to build on Win10 and run on another Win10 and it is still not working, I believe that the DLLs are not the only thing that is needed in order to "sniff" the network interfaces and that the winpcap/npcap libraries are doing something to make it work.

I wanted a way to include everything and not instruct the users of my application to install another library in order to use it.

If there is anything else that I can do to make it work I'm willing to spend the time testing it but currently I'm stuck.

arossert avatar Aug 13 '19 14:08 arossert