简体   繁体   中英

Using plot.ly with py2exe

I've written a graphing program using the PyQT4 and plot.ly libraries. I've tried packaging the program using py2exe, and everything compiled, but when the program reaches the point where the plot.ly library is being used, an error occurs.

File "Efficio.py", line 154, in generateGraph
File "plotly\offline\offline.pyc", line 283, in plot
File "plotly\offline\offline.pyc", line 47, in get_plotlyjs
File "pkg_resources\__init__.pyc", line 1173, in resource_string
File "pkg_resources\__init__.pyc", line 1605, in get_resource_string
File "pkg_resources\__init__.pyc", line 1683, in _get
IOError: [Errno 0] Error: 'plotly\\offline\\plotly.min.js'

Here's my imports:

import sys 
from PyQt4 import QtGui 
import plotly
import plotly.graph_objs as go 
import webbrowser

And my setup file:

from distutils.core import setup
import py2exe
setup(windows=['Efficio.py'],
  options = {
      'py2exe' : {
          'packages' : ['plotly'],
          'dll_excludes' : ["MSVCP90.dll", "HID.DLL", "w9xpopen.exe"],
          'includes' : ["sip", "requests", "six", "pytz"]
          }
      }
  )

What did I do wrong?

The issue is that the plotly.min.js file is not being included in the Library.zip file which is created by py2exe .

I've not found a way to include the plotly.min.js file in the Library.zip in an elegant way (ie: using the data_files argument in the setup.py file).

However! ... there is a dirty-ish hack. This is ONLY a hack; if anyone else has a more elegant solution, PLEASE post.

You can append a routine to the end of your setup.py file which manually adds the plotly.min.js file to the Library.zip.

Here is a version of my setup.py file:

import sys
import py2exe
import matplotlib
import zipfile
import shutil
from distutils.core import setup

#INITIALISE PATH VARIABLES
path_lib  = 'c:\\path\\to\\library'
path_temp = 'c:\\temp\\extract'
path_js   = 'C:\\path\\to\\plotly.min.js'

#EXTEND RECURSION LIMIT
sys.setrecursionlimit(10000)

#SETUP
setup(name='myfile.py',
      version='1.0.0',
      description='program description',
      author='73dev',
      author_email='',
      console=['myfile.py'],
      data_files=matplotlib.get_py2exe_datafiles(),
      options=
      {
      'py2exe' : 
          {
          'packages' : ['sqlalchemy.dialects.sqlite', 'plotly'],
          'includes' : ['plotly', 'requests', 'six', 'pytz'],
          }
      })


''' -- ADD PLOTLY.MIN.JS TO THE LIBRARY ZIP-- '''    

#USER NOTIFICATION
print '\nadding plotly.min.js to Library.zip ...'

#EXTRACT LIBRARY.ZIP TO TEMP DIRECTORY
with zipfile.ZipFile(path_lib + '.zip', 'r') as z: z.extractall(path_temp)

#ADD PLOTLY.MIN.JS TO EXTRACT
shutil.copyfile(path_js, path_temp + '\\plotly\\offline\\plotly.min.js')

#REZIP FILE
shutil.make_archive(path_lib, 'zip', path_temp)

#KILL TEMP FILE
shutil.rmtree(path_temp, ignore_errors=True)

#USER NOTIFICATION
print 'processing complete.'

Here is a semi-related fix (only using the option argument for Cx_Freeze).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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