basemap and py2exe

Hello World,

I'm using py2exe to create an executable on windows .
There is a setup.py file that allows me to create this executable under eclipse.
I have a problem with basemap.
Probably, the app needs data basemap directory but I don't know where to put it.
Thanks for your help.
Yagua

The contents of the log file is as follows:
Traceback (most recent call last)
  File "traitement_points.py", line 40, in <module>
  File "mpl_toolkits\basemap\__init__.pyc", line 774, in __init__
  File "mpl_toolkits\basemap\__init__.pyc", line 848, in _readboundarydata
IOError: Unable to open boundary dataset file. Only the 'crude', 'low',
'intermediate' and 'high' resolution datasets are installed by default.
If you are requesting a 'full' resolution dataset, you may need to
download and install those files separately
(see the basemap README for details).

The contents of the setup file is :

from distutils.core import setup
import py2exe

import glob

opts = {
    'py2exe': { "includes" : ["sip", "PyQt4", "matplotlib.backends",
"matplotlib.backends.backend_qt4agg",
                               "matplotlib.figure","pylab", "numpy",
"matplotlib.numerix.fft",
                               "matplotlib.numerix.linear_algebra",
"matplotlib.numerix.random_array",
                               "matplotlib.backends.backend_tkagg"],
                'excludes': ['_gtkagg', '_tkagg', '_agg2', '_cairo',
'_cocoaagg',
                             '_fltkagg', '_gtk', '_gtkcairo', ],
                'dll_excludes': ['libgdk-win32-2.0-0.dll',
                                 'libgobject-2.0-0.dll']
              }
       }

data_files = [(r'mpl-data',
glob.glob(r'C:\Python25\Lib\site-packages\matplotlib\mpl-data\*.*')),
                  (r'mpl-data',
[r'C:\Python25\Lib\site-packages\matplotlib\mpl-data\matplotlibrc']),

(r'mpl-data\images',glob.glob(r'C:\Python25\Lib\site-packages\matplotlib\mpl-data\images\*.*')),

(r'mpl-data\fonts',glob.glob(r'C:\Python25\Lib\site-packages\matplotlib\mpl-data\fonts\*.*'))]

setup(windows=[{"script" : "traitement_points.py", "icon_resources":
[(1, "lambda.ico")]}],
        description="test py2exe.",
                version = "1.0",
        options=opts, data_files=data_files)

I'm not shure whether the following suggestion solves your problem,
but it would simplify your script anyway.

import matplotlib
...
setup(..., data_files = matplotlib.get_py2exe_datafiles())

And maybe don't forget to exclude 'libgdk_pixbuf-2.0-0.dll' (on my
system) in 'dll_excludes'. But I actually don't remember for what
reason I had to exclude it.

hth,
Friedrich

I identify the problem.
There is no basemap data directory and the app search those datas in
[MY_DIR]\dist\library.zip\mpl_toolkits\basemap\data

But I don't know how to add it at the compilation....

2010/3/22 Friedrich Romstedt <friedrichromstedt@...287...>:

···

I'm not shure whether the following suggestion solves your problem,
but it would simplify your script anyway.

import matplotlib
...
setup(..., data_files = matplotlib.get_py2exe_datafiles())

And maybe don't forget to exclude 'libgdk_pixbuf-2.0-0.dll' (on my
system) in 'dll_excludes'. But I actually don't remember for what
reason I had to exclude it.

hth,
Friedrich

Yagua Rovi wrote:

I identify the problem.
There is no basemap data directory and the app search those datas in
[MY_DIR]\dist\library.zip\mpl_toolkits\basemap\data

But I don't know how to add it at the compilation....

You can add those to "data_files", or you may need to copy them with a little custom code at the end of your setup.py file.

-CHB

···

2010/3/22 Friedrich Romstedt <friedrichromstedt@...287...>:

I'm not shure whether the following suggestion solves your problem,
but it would simplify your script anyway.

import matplotlib
...
setup(..., data_files = matplotlib.get_py2exe_datafiles())

And maybe don't forget to exclude 'libgdk_pixbuf-2.0-0.dll' (on my
system) in 'dll_excludes'. But I actually don't remember for what
reason I had to exclude it.

hth,
Friedrich

------------------------------------------------------------------------------
Download Intel&#174; Parallel Studio Eval
Try the new software tools for yourself. Speed compiling, find bugs
proactively, and fine-tune applications for parallel performance.
See why Intel Parallel Studio got high marks during beta.
http://p.sf.net/sfu/intel-sw-dev
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-users

--
Christopher Barker, Ph.D.
Oceanographer

Emergency Response Division
NOAA/NOS/OR&R (206) 526-6959 voice
7600 Sand Point Way NE (206) 526-6329 fax
Seattle, WA 98115 (206) 526-6317 main reception

Chris.Barker@...259...

2010/3/22 Friedrich Romstedt <friedrichromstedt@...287...>:

I'm not shure whether the following suggestion solves your problem,
but it would simplify your script anyway.

import matplotlib
...
setup(..., data_files = matplotlib.get_py2exe_datafiles())

And maybe don't forget to exclude 'libgdk_pixbuf-2.0-0.dll' (on my
system) in 'dll_excludes'. But I actually don't remember for what
reason I had to exclude it.

hth,
Friedrich

Hello,

I wrote the solution implemented in the hope that it serves at someone else.

The Situation

···

=========
I wrote a script using basemap and matplolib. which draw a map.Dread a
file containing a bunch of Genbank accession numbers, and downloaded
the Genbank records:
....
from pylab import *
import os, sys
import os.path
from mpl_toolkits.basemap import Basemap
import matplotlib.pyplot as plt
from matplotlib.font_manager import FontProperties

map = Basemap(projection='cyl', resolution='l')
map.drawcoastlines()
map.drawmapboundary()
...

This worked fine as a script, but when I attempted to turn it into a
Windows executable with py2exe and the setup.py script:

"""
from distutils.core import setup
import py2exe

from distutils.core import setup
import py2exe

import glob
import matplotlib

opts = {
    'py2exe': { "includes" : ["sip", "PyQt4", "matplotlib.backends",
      "matplotlib.backends.backend_qt4agg",
                               "matplotlib.figure","pylab", "numpy",
"matplotlib.numerix.fft","mpl_toolkits.basemap",
                               "matplotlib.numerix.linear_algebra",
"matplotlib.numerix.random_array",
                               "matplotlib.backends.backend_tkagg"],
       # 'excludes': ['_gtkagg', '_tkagg','_agg2','_cairo',
'_cocoaagg', '_fltkagg', '_gtk', '_gtkcairo', ],
                'dll_excludes': ['libgdk-win32-2.0-0.dll',
                                 'libgobject-2.0-0.dll']
              }
       }

data_files = [(r'mpl-data',
glob.glob(r'C:\Python25\Lib\site-packages\matplotlib\mpl-data\*.*')),
                    # Because matplotlibrc does not have an extension,
glob does not find it (at least I think that's why)
                    # So add it manually here:
                  (r'mpl-data',
[r'C:\Python25\Lib\site-packages\matplotlib\mpl-data\matplotlibrc']),

(r'mpl-data\images',glob.glob(r'C:\Python25\Lib\site-packages\matplotlib\mpl-data\images\*.*')),

(r'mpl-data\fonts',glob.glob(r'C:\Python25\Lib\site-packages\matplotlib\mpl-data\fonts\*.*'))]
setup(windows=[{"script" : "myapp.py",
  "icon_resources": [(1, "myapp.ico")]}, ],
                version = "1.0",
    options=opts, data_files=data_files)

with the command python setup.py py2exe, attempting to run the
resulting myapp.exe would throw an error.

The Error

This is the error thrown on running the executable:
Traceback (most recent call last)
  File "myapp.py", line 40, in <module>
  File "mpl_toolkits\basemap\__init__.pyc", line 774, in __init__
  File "mpl_toolkits\basemap\__init__.pyc", line 848, in _readboundarydata
IOError: Unable to open boundary dataset file. Only the 'crude', 'low',
'intermediate' and 'high' resolution datasets are installed by default.
If you are requesting a 'full' resolution dataset, you may need to
download and install those files separately
(see the basemap README for details).

The Problem

Location of basemap data
The problem is identified when basemap is imported, the code of
basemap/__init__.py try to establish the basemap data directory in the
library.zip file.
[install dir]\dist\library.zip\mpl_toolkits\basemap\data

Under normal script-like execution, it works, the path is a string
indicating a location accessible through the filesystem . However with
py2exe, the location of data basemap directory is located within the
shared zip archive that py2exe creates and the above error is thrown.

The Solution

The solution came in three times
1. Add mpl_toolkits.basemap in the option list (setup.py)
#--------------------------------------------
opts = {
    'py2exe': { "includes" : ["sip", "PyQt4", "matplotlib.backends",
"matplotlib.backends.backend_qt4agg",
                               "matplotlib.figure","pylab", "numpy",
"matplotlib.numerix.fft","mpl_toolkits.basemap",
                               "matplotlib.numerix.linear_algebra",
"matplotlib.numerix.random_array",
                               "matplotlib.backends.backend_tkagg"],
#--------------------------------------------

2. Add basemap\data in data_files list
#--------------------------------------------
data_files = [(r'mpl-data',
glob.glob(r'C:\Python25\Lib\site-packages\matplotlib\mpl-data\*.*')),
                  (r'mpl-data',
[r'C:\Python25\Lib\site-packages\matplotlib\mpl-data\matplotlibrc']),

(r'mpl-data\images',glob.glob(r'C:\Python25\Lib\site-packages\matplotlib\mpl-data\images\*.*')),

(r'mpl-data\data',glob.glob(r'C:\Python25\Lib\site-packages\mpl_toolkits\basemap\data\*.*')),
#---------------------------------------------

3. Modification of
C:\Python25\Lib\site-packages\mpl_toolkits\basemap\__init__.py

Near l.50, add the block "check if it turns into a Windows executable
with py2exe"

if 'BASEMAPDATA' in os.environ:
    basemap_datadir = os.environ['BASEMAPDATA']
    if not os.path.isdir(basemap_datadir):
        raise RuntimeError('Path in environment BASEMAPDATA not a directory')
else:
    basemap_datadir = os.sep.join([os.path.dirname(__file__), 'data'])

#-----------------------------------------------
# Check if it turns into a Windows executable with py2exe
    import re
    if (basemap_datadir.find("library.zip") > -1):
       basemap_datadir=re.sub("library.zip","",basemap_datadir)
       basemap_datadir=re.sub("mpl_toolkits","mpl-data",basemap_datadir)
       basemap_datadir=re.sub("basemap","",basemap_datadir)
#-----------------------------------------------

This works under normal script execution and in py2exe mode.