2

I have a Python program which adds something dynamically to sys.path. It works fine, but I'd like to distribute my Python program as a .zip file where __main__.py runs.

Can I add entries to sys.path from the zip file? After searching around a bit, I found the pkgutil documentation but I can't figure out what will work.

note: the stuff I need to add in sys.path includes .pyd and .dll files for Windows.

Jason S
  • 184,598
  • 164
  • 608
  • 970

1 Answers1

3

I'm not entirely sure I understand your question, but I'll have a crack at answering it anyway ...

Unfortunately, while you can import from a zipfile by adding it to sys.path:

sys.path contains a list of strings providing search locations for modules and packages. It is initialized from the PYTHONPATH environment variable and various other installation- and implementation-specific defaults. Entries in sys.path can name directories on the file system, zip files, and potentially other “locations” (see the site module) that should be searched for modules, such as URLs, or database queries. Only strings and bytes should be present on sys.path; all other data types are ignored. The encoding of bytes entries is determined by the individual path entry finders.

source: http://docs.python.org/3/reference/import.html#path-entry-finders

... that doesn't extend to .pyd and .dll files:

Any files may be present in the ZIP archive, but only files .py and .py[co] are available for import. ZIP import of dynamic modules (.pyd, .so) is disallowed.

source: http://docs.python.org/3/library/zipimport.html

I'm not aware of any way of working around this restriction.

Zero Piraeus
  • 56,143
  • 27
  • 150
  • 160
  • is there a good workaround? e.g. if I unzip the .pyd and .dll files somewhere and add them to sys.path, where is a good place to do this? I don't want to modify the default Python installation, and if I run the .zip file 100 times I don't want it to generate 100 temporary directories. – Jason S Jul 24 '13 at 13:34
  • 1
    @JasonS I've never developed specifically for Windows (nor am I a Windows user), but it looks like [winpaths](http://ginstrom.com/code/winpaths.html) might be helpful - presumably you'd want one of the "App Data" variants. Found [here](http://stackoverflow.com/q/626796/1014938), by the way. – Zero Piraeus Jul 24 '13 at 19:36