Search code examples
pythonpandasnumpypyinstallerpyvis

pyinstaller not working with pyvis network


I am trying to test the packaging of pyvis library in Python using Pyinstaller into a usable application. However, I want to do it in a virtual environment. After creating the virtual environment and installing pyvis and the required libraries, I have run pyinstaller to create a directory of the app. The following is the sample python script whose packaging I want to test:

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')

and I used the following commands in Anaconda Prompt to build the virtual environment and the application:

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

Upon running the samplepyvis.exe in the directory created, I get the following error:

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'

I have also tried creating the application without the virtual environment. However, I get an error even in that case:

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'

Could someone please help me on this issue? While the script running in Spyder Editor seems to produce the correct results, packaging it into an application using Pyinstaller does not.

Any help would be really appreciated :)


Solution

  • Run the installer collecting the module files into the executable like this:

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

    This will cause the module's templates to be bundled with the executable.