Frequently Asked Questions

Version: 16.2

Table of Contents

1.0 MineSight Grail and Python

1.1 Q: What Version of Python Can We Use with MineSight Grail?

This MineSight Grail will work with Python 2.7.6 .

1.2 Q: How does Python search for MSGRAIL?

The search is done via a set of steps described in Section 6.1.1 of the Python Tutorial for details.

It is a bit complicated, so it might be helpful to explain this via an example. When python parses the line "import grail", it does the following search for a "grail module",

  • Starting in the Local Working Directory,
    1. Is there a grail.pyc (or *.pyo) that is younger than a grail.py?
    2. Is there a grail.py
    3. Is there a grail directory that contains an __init__.py or __init__.pyc.
    4. Is there a grail.pth file (old way of specifying modules).
  • For each path specified on PYTHONPATH,
    1. Is there a grail.pyc (or *.pyo) that is younger than a grail.py?
    2. Is there a grail.py
    3. Is there a grail directory that contains an __init__.py or __init__.pyc.
    4. Is there a grail.pth file (old way of specifying modules).
  • It then repeats the steps again through the Python system directories.

There is a trick to allow you to see the directory search order that python takes, if you type the following from the command line (assuming python.exe is on your path),

c:\>python -c "import pprint, sys; pprint.pprint(sys.path)"

For example, with PYTHONPATH equal to c:\winexe, I get,

['',
 
'C:\\WINEXE',
 
'C:\\Python22\\lib\\site-packages\\Pythonwin',
 
'C:\\Python22\\lib\\site-packages\\win32',
 
'C:\\Python22\\lib\\site-packages\\win32\\lib',
 
'C:\\Python22\\lib\\site-packages',
 
'c:\\Python22\\DLLs',
 
'c:\\Python22\\lib',
 
'c:\\Python22\\lib\\lib-tk',
 
'C:\\Python22',
 
'c:\\Python22\\lib\\site-packages\\Numeric',
 
'c:\\Python22\\lib\\site-packages\\PIL']

Note that in that list, the '' is referring to the local working directory, which in this example, would be the c:/ drive.

1.3. Q: I am unable to import zipfile, what can I do?

If the following python import statement,

import zipfile

is generating the following error,

ImportError: dynamic module does not define init function (initzlib)

then you are running into a conflict of "zlibs" (zip compression library) which can be resolved by replaceing import zlib with the following,

# Workaround to load the zlib module, which ends up conflicting with
# our MEDEXE directory.
import os, sys
sys.path.insert(0, os.path.join(sys.prefix, "DLLs") )
import zlib
import zipfile

This effectively tells the python interpreter to make sure it always looks in the python "DLLs" directory for zlib.pyd before it starts looking elsewhere. You may have a zlib.dll that is "found" before the real zlib.pyd is found.

2.0 Common Scripting Questions

2.1 Q: How can I determine the Script's Directory?

Sometimes you want to know the name of the directory that contains your script. The following little snippet will help you determine that:

# Defines the location of the script directory.
import os
import sys
if __name__=="__main__":
    
SCRIPT_DIR = os.path.dirname(sys.argv[0])
else:
    
SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__))

this will show the location of the script directory when you import the script or if you just run the script. See also "Dive Into Python: 16.2 Finding the path" [1] for more details.

Starting in version 4.60, you can also listen for messages from '''gmain'''.

def main():
    
# The TRUE script path, may be unpacked elsewhere.
    
print gmain.actual_path
    
# The location a user selected the data from.
    
print gmain.logical_path
gmain = gsys.GMAIN(main, __name__)
gmain.run()

for PY/PYC files the logical and actual script paths will be the same. For a PYZ file the actual script path will be where the script is begin executed from, and the logical script path will be where script was picked.

In earlier versions you would have to listen to the messages sent to gmain, as in,

def gmain(m,d):
   
global script_path
   
global log_script_path
   
if m == messages.gACTUAL_SCRIPT_PATH:
      
# The TRUE script path, may be unpacked elsewhere.
      
script_path = d
   
elif m == message.gLOGICAL_SCRIPT_PATH:
      
# The location a user selected the data from.
      
log_script_path = d

if your script_path and log_script_path are globals, make sure to use the "global" directive in your function.

3.0 Trouble Shooting

3.1 Q: How can I determine what version of MSGRAIL I am using?

There are two ways to do it: one from MS3D and one from the command line.

  • From MineSight(r) 3D (MS3D):

    1. Run MS3D

    2. Select File->Scripts->Run Script...

    3. Pick the em-info.py script found in the $(medexe)\scripts directory.

      This should display information on the MSGRAIL installation and python within the MS3D message window. Including the version number of MSGRAIL library that you are using.

  • From the Command Line

    1. You can type the following from the command line, assuming that you have grail installed, and python is available on the path,

      python -c "import grail.info; print grail.info.DESCRIPTION"
      

3.2 Q: How can I determine the location of my MSGRAIL installation?

There are two ways to do it: one from MS3D and one from the command line.

  • From MineSight(r) 3D (MS3D):

    1. Run MS3D

    2. Select File->Scripts->Run Script...

    3. Pick the em-info.py script found in the $(medexe)\scripts directory.

      This should display information on the MSGRAIL installation and python within the MS3D message window. Including the location of the MSGRAIL library that you are using.

  • From the Command Line

    1. You can type the following from the command line, assuming that you have grail installed, and python is available on the path,

      python -c "import grail.info; print grail.info.DESCRIPTION"
      

4.0 MineSight Grail and Compass

4.1 Q: How can I determine the available files in a Compass Project?

See the HowTo for Determining Available Files for the Current Compass Project. If you want to find all files, regardless of availablity, see the next question.

References

[1]"16.2 Finding the path". Dive Into Python: Python from novice to pro. 28 June 2007. <http://www.diveintopython.org/functional_programming/finding_the_path.html>