簡體   English   中英

與 pyinstaller 捆綁 - ModuleNotFoundError: No module named 'bs4

[英]Bundling with pyinstaller - ModuleNotFoundError: No module named 'bs4

在我完成 Python 腳本之后,我想捆綁我的腳本和使用的庫,這樣我可以更輕松地傳播我的腳本。

我嘗試使用 PiInstaller,但我運行在 dist 文件夾上生成的腳本,我得到模塊 bs4(找不到 beautifullSoup)

我進行如下:

我從我使用命令“python3 -m venv env » 創建的虛擬環境目錄中運行 piInstaller,如下所示:

$ /home/kfocal/.local/bin/pyinstaller podcast_apple.py                                 
31 INFO: PyInstaller: 4.2 
31 INFO: Python: 3.8.5 
39 INFO: Platform: Linux-5.4.0-65-generic-x86_64-with-glibc2.29 
42 INFO: wrote /home/focal/Documents/python/Kdata/env/podcast_apple.spec 
45 INFO: UPX is not available. 
47 INFO: Extending PYTHONPATH with paths 
['/home/focal/Documents/python/Kdata/env', 
'/home/focal/Documents/python/Kdata/env'] 
54 INFO: checking Analysis 
54 INFO: Building Analysis because Analysis-00.toc is non existent 
54 INFO: Initializing module dependency graph... 
55 INFO: Caching module graph hooks... 
58 WARNING: Several hooks defined for module 'win32ctypes.core'. Please take care they do not conflict. 
60 INFO: Analyzing base_library.zip ... 
2102 INFO: Processing pre-find module path hook distutils from '/home/kfocal/.local/lib/python3.8/site-packages/PyInstaller/hooks/pre_find_module_path/hook-distutils.py'. 
2103 INFO: distutils: retargeting to non-venv dir '/usr/lib/python3.8' 
4053 INFO: Caching module dependency graph... 
4137 INFO: running Analysis Analysis-00.toc 
4160 INFO: Analyzing /home/focal/Documents/python/Kdata/env/podcast_apple.py 
4235 INFO: Processing pre-safe import module hook six.moves from '/home/kfocal/.local/lib/python3.8/site-packages/PyInstaller/hooks/pre_safe_import_module/hook-six.moves.py'. 
5794 INFO: Processing module hooks... 
5794 INFO: Loading module hook 'hook-certifi.py' from '/home/kfocal/.local/lib/python3.8/site-packages/_pyinstaller_hooks_contrib/hooks/stdhooks'... 
5796 INFO: Loading module hook 'hook-cryptography.py' from '/home/kfocal/.local/lib/python3.8/site-packages/_pyinstaller_hooks_contrib/hooks/stdhooks'... 
5949 INFO: Loading module hook 'hook-distutils.util.py' from '/home/kfocal/.local/lib/python3.8/site-packages/PyInstaller/hooks'... 
5952 INFO: Excluding import of lib2to3.refactor from module distutils.util 
5952 INFO: Loading module hook 'hook-distutils.py' from '/home/kfocal/.local/lib/python3.8/site-packages/PyInstaller/hooks'... 
5954 INFO: Loading module hook 'hook-multiprocessing.util.py' from '/home/kfocal/.local/lib/python3.8/site-packages/PyInstaller/hooks'... 
5955 INFO: Excluding import of test from module multiprocessing.util 
5955 INFO: Excluding import of test.support from module multiprocessing.util 
5955 INFO: Loading module hook 'hook-xml.py' from '/home/kfocal/.local/lib/python3.8/site-packages/PyInstaller/hooks'... 
6004 INFO: Loading module hook 'hook-lib2to3.py' from '/home/kfocal/.local/lib/python3.8/site-packages/PyInstaller/hooks'... 
6011 INFO: Loading module hook 'hook-sysconfig.py' from '/home/kfocal/.local/lib/python3.8/site-packages/PyInstaller/hooks'... 
6021 INFO: Loading module hook 'hook-difflib.py' from '/home/kfocal/.local/lib/python3.8/site-packages/PyInstaller/hooks'... 
6024 INFO: Excluding import of doctest from module difflib 
6024 INFO: Loading module hook 'hook-pickle.py' from '/home/kfocal/.local/lib/python3.8/site-packages/PyInstaller/hooks'... 
6025 INFO: Excluding import of argparse from module pickle 
6025 INFO: Loading module hook 'hook-heapq.py' from '/home/kfocal/.local/lib/python3.8/site-packages/PyInstaller/hooks'... 
6027 INFO: Excluding import of doctest from module heapq 
6027 INFO: Loading module hook 'hook-encodings.py' from '/home/kfocal/.local/lib/python3.8/site-packages/PyInstaller/hooks'... 
6064 INFO: Loading module hook 'hook-xml.etree.cElementTree.py' from '/home/kfocal/.local/lib/python3.8/site-packages/PyInstaller/hooks'... 
6080 INFO: Looking for ctypes DLLs 
6104 INFO: Analyzing run-time hooks ... 
6107 INFO: Including run-time hook '/home/kfocal/.local/lib/python3.8/site-packages/PyInstaller/hooks/rthooks/pyi_rth_multiprocessing.py' 
6111 INFO: Including run-time hook '/home/kfocal/.local/lib/python3.8/site-packages/_pyinstaller_hooks_contrib/hooks/rthooks/pyi_rth_certifi.py' 
6121 INFO: Looking for dynamic libraries 
6500 INFO: Looking for eggs 
6500 INFO: Python library not in binary dependencies. Doing additional searching... 
6546 INFO: Using Python library /lib/x86_64-linux-gnu/libpython3.8.so.1.0 
6554 INFO: Warnings written to /home/focal/Documents/python/Kdata/env/build/podcast_apple/warn-podcast_apple.txt 
6582 INFO: Graph cross-reference written to /home/focal/Documents/python/Kdata/env/build/podcast_apple/xref-podcast_apple.html 
6591 INFO: checking PYZ 
6591 INFO: Building PYZ because PYZ-00.toc is non existent 
6591 INFO: Building PYZ (ZlibArchive) /home/focal/Documents/python/Kdata/env/build/podcast_apple/PYZ-00.pyz 
7080 INFO: Building PYZ (ZlibArchive) /home/focal/Documents/python/Kdata/env/build/podcast_apple/PYZ-00.pyz completed successfully. 
7084 INFO: checking PKG 
7084 INFO: Building PKG because PKG-00.toc is non existent 
7084 INFO: Building PKG (CArchive) PKG-00.pkg 
7126 INFO: Building PKG (CArchive) PKG-00.pkg completed successfully. 
7127 INFO: Bootloader /home/kfocal/.local/lib/python3.8/site-packages/PyInstaller/bootloader/Linux-64bit/run 
7127 INFO: checking EXE 
7127 INFO: Building EXE because EXE-00.toc is non existent 
7127 INFO: Building EXE from EXE-00.toc 
7128 INFO: Appending archive to ELF section in EXE /home/focal/Documents/python/Kdata/env/build/podcast_apple/podcast_apple 
7138 INFO: Building EXE from EXE-00.toc completed successfully. 
7139 INFO: checking COLLECT 
7139 INFO: Building COLLECT because COLLECT-00.toc is non existent 
7140 INFO: Building COLLECT COLLECT-00.toc 
7158 INFO: Building COLLECT COLLECT-00.toc completed successfully.

如您所見,一切似乎都很好,但我知道 bs4 模塊丟失了

~/Documents/python/Kdata/env/dist/podcast_apple$ ./podcast_apple  
Traceback (most recent call last): 
 File "podcast_apple.py", line 1, in <module> 
ModuleNotFoundError: No module named 'bs4' 
[81848] Failed to execute script podcast_apple

而當我搜索“bs4”時,我只能在我的開發人員的庫中找到。

~/Documents/python/Kdata/env$ find . -name "*bs4*" 
./lib/python3.8/site-packages/bs4 
./lib/python3.8/site-packages/bs4-0.0.1.egg-info

我錯過了什么嗎? 根據文檔,piInstaller 應該復制所有使用的模塊。 為什么 pyInstaller 錯過了 bs4 ? Mon腳本很簡單。

為了找出答案,PyInstaller 會在您的腳本中找到所有導入語句。 它找到導入的模塊並在其中查找導入語句,以此類推,直到它擁有您的腳本可能使用的模塊的完整列表。

我知道這個線程已經很老了,但是在我的虛擬環境中運行 PyInstaller 時我遇到了同樣的問題。 Beautifulsoup4 已安裝在我的 virtualenv 中,但 PyInstaller 似乎仍然沒有添加 package。 在運行 PyInstaller 時添加--hidden-import beautifulsoup4也沒有解決問題。

經過一番搜索,我終於找到了這個鏈接: https://groups.google.com/g/PyInstaller/c/0IOY5Xoevsg

我看到的建議:

您可以將它(PyInstaller)作為python -m PyInstaller [rest of the command here]運行(而不是運行pyinstaller.exe [args] )嗎?

令人驚訝的是,這對我有用。 我想我現在明白發生了什么:我認為雖然我在一個安裝了 package 的 virtualenv 中,但被調用的 pyinstaller 是在錯誤的環境中(在我的 virtualenv 之外?) - 也許是因為我的順序激活了virtualenv。

使用python -m...強制它使用正確的 PyInstaller

更:

如果python -m PyInstaller給出 No module named PyInstaller 錯誤,那么您的 venv 是一團糟,並且 PyInstaller 不在正確的環境中 使用python -m...強制它使用正確的 PyInstaller(它不存在)。 您的 PATH 中的 PyInstaller 可能是從其他一些舊環境(使用where PyInstaller找出哪個)或您的根環境中遺留下來的。

修復只是使用以下方法在您的 venv 中安裝 PyInstaller:

venv\Scripts\activate pip install PyInstaller

驗證第一個(或唯一一個)output PyInstaller 現在指向您的 venv/Scripts 中的位置。

tl;博士:總而言之-

  1. 嘗試將 pyinstaller 運行為python -m PyInstaller [args]而不是pyinstaller.exe [args] ,以嘗試強制它使用正確的 pyinstaller 上下文

  2. 如果這仍然不起作用,請從虛擬環境本身安裝 PyInstaller,方法是運行pip install PyInstaller

希望這可以幫助某人。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM