Zipimport with packages

I am trying to pack a pychess package into a zip file and import it using zipimport, but running into some problems.

I packed it into a zip file with the following script that works:

#!/usr/bin/env python
import zipfile
zf = zipfile.PyZipFile('../pychess.zip.mod', mode='w')
try:
    zf.writepy('.')
finally:
    zf.close()
for name in zf.namelist():
    print name

However, I cannot perform complex imports in my code:

z = zipimport.zipimporter('./pychess.zip.mod')
#z.load_module('pychess') # zipimport.ZipImportError: can't find module 'pychess'
#z.load_module('Utils.lutils') # zipimport.ZipImportError: can't find module 'Utils.lutils'
Utils = z.load_module('Utils') # seems to work, but...
from Utils import lutils
#from Utils.lutils import LBoard  # ImportError: No module named pychess.Utils.const


How to import, for example. pychess.Utils.lutils.LBoard from zip file?

Here is the complete list of modules I need to import:

import pychess
from pychess.Utils.lutils import LBoard
from pychess.Utils.const import *
from pychess.Utils.lutils import lmovegen
from pychess.Utils.lutils import lmove

Thank!

+5
source share
1 answer

Assuming you have unpacked pychess leading to the pychess-0.10.1 directory in your current directory and that pychess-0.10.1 / lib / pychess exists (I got this directory from the deployment of pychess-0.10.1.tar. Gz) .

First start:

#!/usr/bin/env python

import os
import zipfile

os.chdir('pychess-0.10.1/lib')
zf = zipfile.PyZipFile('../../pychess.zip', mode='w')
try:
    zf.writepy('pychess')
finally:
    zf.close()
for name in zf.namelist():
    print name

after that, it works:

#!/usr/bin/env python

import sys
sys.path.insert(0, 'pychess.zip')

from pychess.Utils.lutils import LBoard
+2
source

All Articles