【问题标题】:pyinstaller not working with pyvis networkpyinstaller 不适用于 pyvis 网络
【发布时间】:2022-06-20 20:59:38
【问题描述】:

我正在尝试使用 Pyinstaller 将 Python 中的 pyvis 库打包到一个可用的应用程序中。但是,我想在虚拟环境中进行。创建虚拟环境并安装 pyvis 和所需的库后,我已经运行 pyinstaller 来创建应用程序的目录。以下是我要测试其打包的示例 python 脚本:

from pyvis.network import Network
import pandas as pd

got_net = Network(height='750px', width='100%', bgcolor='#222222', font_color='white')

# set the physics layout of the network
got_net.barnes_hut()
got_data = pd.read_csv('https://www.macalester.edu/~abeverid/data/stormofswords.csv')

sources = got_data['Source']
targets = got_data['Target']
weights = got_data['Weight']

edge_data = zip(sources, targets, weights)

for e in edge_data:
    src = e[0]
    dst = e[1]
    w = e[2]

    got_net.add_node(src, src, title=src)
    got_net.add_node(dst, dst, title=dst)
    got_net.add_edge(src, dst, value=w)

neighbor_map = got_net.get_adj_list()

# add neighbor data to node hover data
for node in got_net.nodes:
    node['title'] += ' Neighbors:<br>' + '<br>'.join(neighbor_map[node['id']])
    node['value'] = len(neighbor_map[node['id']])

got_net.show('gameofthrones.html')

我在 Anaconda Prompt 中使用了以下命令来构建虚拟环境和应用程序:

virtualenv venv
venv\Scripts\activate.bat
pip install pandas pyvis pyinstaller
pyinstaller -D -w samplepyvis.py

在创建的目录中运行 samplepyvis.exe 时,出现以下错误:

Traceback (most recent call last):
  File "samplepyvis.py", line 9, in <module>
  File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
  File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
  File "PyInstaller\loader\pyimod03_importers.py", line 495, in exec_module
  File "pandas\__init__.py", line 22, in <module>
  File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
  File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
  File "PyInstaller\loader\pyimod03_importers.py", line 495, in exec_module
  File "pandas\compat\__init__.py", line 14, in <module>
  File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
  File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
  File "PyInstaller\loader\pyimod03_importers.py", line 495, in exec_module
  File "pandas\_typing.py", line 78, in <module>
AttributeError: module 'numpy' has no attribute 'ndarray'

我也尝试过在没有虚拟环境的情况下创建应用程序。但是,即使在这种情况下,我也会收到错误消息:

Traceback (most recent call last):
  File "samplepyvis.py", line 39, in <module>
  File "pyvis\network.py", line 495, in show
  File "pyvis\network.py", line 476, in write_html
  File "pyvis\network.py", line 434, in generate_html
FileNotFoundError: [Errno 2] No such file or directory: 'D:\\Python Workspace\\pyvis1\\dist\\samplepyvis\\pyvis/templates/template.html'

有人可以帮我解决这个问题吗?虽然在 Spyder Editor 中运行的脚本似乎产生了正确的结果,但使用 Pyinstaller 将其打包到应用程序中却没有。

任何帮助将不胜感激:)

【问题讨论】:

    标签: python pandas numpy pyinstaller pyvis


    【解决方案1】:

    运行安装程序,将模块文件收集到可执行文件中,如下所示:

    pyinstaller -D -w --collect-all pyvis ./samplepyvis.py
    

    这将导致模块的模板与可执行文件捆绑在一起。

    【讨论】:

      猜你喜欢
      • 2023-02-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-04
      • 2012-04-29
      相关资源
      最近更新 更多