title: Python - How to import module from .so file from egg file without using an absolute file path
body: <p>I built the <a href="https://pypi.python.org/pypi/pyahocorasick/" rel="nofollow noreferrer">pyahocorasick</a> library with <code>python setup.py bdist_egg</code> command and uploaded it onto Spark for my PySpark job.</p>
  
  <p>However, the .so file inside pyahocorasick can't be imported through <code>pkg_resources.resource_filename()</code> method on Spark cluster for security reasons.</p>
  
  <pre><code>Traceback (most recent call last):
    File "spark_datawash.py", line 251, in &lt;module&gt;
      import ahocorasick
    File "build/bdist.linux-x86_64/egg/ahocorasick.py", line 7, in &lt;module&gt;
    File "build/bdist.linux-x86_64/egg/ahocorasick.py", line 4, in __bootstrap__
    File "build/bdist.linux-x86_64/egg/pkg_resources/__init__.py", line 1152, in resource_filename
    File "build/bdist.linux-x86_64/egg/pkg_resources/__init__.py", line 1696, in get_resource_filename
    File "build/bdist.linux-x86_64/egg/pkg_resources/__init__.py", line 1726, in _extract_resource
    File "build/bdist.linux-x86_64/egg/pkg_resources/__init__.py", line 1219, in get_cache_path
    File "build/bdist.linux-x86_64/egg/pkg_resources/__init__.py", line 1199, in extraction_error
  pkg_resources.ExtractionError: Can't extract file(s) to egg cache
  
  The following error occurred while trying to extract file(s) to the Python egg
  cache:
  
    [Errno 13] Permission denied: '/home/.python-eggs'
  
  The Python egg cache directory is currently set to:
  
    /home/.python-eggs
  
  Perhaps your account does not have write access to this directory?  You can
  change the cache directory by setting the PYTHON_EGG_CACHE environment
  variable to point to an accessible directory.
  </code></pre>
  
  <p>This is how pyahocorasick import the .so:</p>
  
  <pre><code>def __bootstrap__():
      global __bootstrap__, __loader__, __file__
      import sys, pkg_resources, imp 
      __file__ = pkg_resources.resource_filename(__name__, 'ahocorasick.so')
      __loader__ = None; del __bootstrap__, __loader__
      imp.load_dynamic(__name__,__file__)
  __bootstrap__()
  </code></pre>
  
  <p>Can I import the .so by <code>resource_stream()</code> instead of <code>resource_filename()</code> or some other way without the need to read from an absolute file path? 
  Thanks all.</p>
  
  <p>BTW, I can't install pyahocorasick on every node on Spark cluster for other reasons. So I have to upload an egg-zipped distribution for later use.</p>