Skip to content

Instantly share code, notes, and snippets.

@htgoebel
Last active August 29, 2015 14:11
Show Gist options
  • Save htgoebel/6f65c40a0a7ef4c92d5b to your computer and use it in GitHub Desktop.
Save htgoebel/6f65c40a0a7ef4c92d5b to your computer and use it in GitHub Desktop.
PyInstaller #653: Importing nested modules from eggs/*.egg
/bin
/lib
/lib64
/include
build/
dist/
*.egg-info
*.py[co]

From <http://article.gmane.org/gmane.comp.python.pyinstaller/5416>, 2012-12-23 10:58:

I am attaching source for two packages, nnsA (nested namespace A) and nnsA.B.C, where nnsA.B is declared as a namespace-package; nnsA contains console script (runNnsA) which tries to import nnsA.B and prints whether it succeeded or not. I tested that setup with your development branch (git://github.com/matysek/pyinstaller.git).

Preperation un-flatten the files in the gist:

for i in *---* ; do
  j=$(echo $i | sed 's!---!/!g')
  mkdir -p $(dirname $j)
  mv $i $j
done

Non-frozen installation:

virtualenv .
. bin/activate
cd nnsA
python setup.py install
runNnsA
# should give: "nnsA.B: ImportError"

cd ../nnsA.B.C
python setup.py install
runNnsA
# should give: "nnsA.B: imported"

cd ../nanspZ
python setup.py install
runNnsA
# should give: "nanspZ: imported"

works just fine: nnsA.B is not imported when nnsA.B.C is not installed, and it imported when nnsA.B.C is installed.

Frozen installation:

cd ../nnsA.B.C
python setup.py bdist_egg
cd ../nanspZ/
python setup.py bdist_egg

cd ../nnsA
python setup.py install
python /path/to/pyinstaller/pyinstaller.py -y pyinstaller.spec
dist/nnsA-frozen/runNnsA.exe
# should give: "nnsA.B: ImportError"
# should give: "nanspZ: ImportError"

cd ../nnsA
mkdir dist/nnsA-frozen/eggs
cp ../{nnsA.B.C,nanspZ}/dist/*.egg dist/nnsA-frozen/eggs/

does not import nnsA.B when nnsA.B.C's egg is not installed, but it fails importing nnsA.B when nnsA.B.C-0.0.0-py2.7.egg is dropped into nnsA/dist/nnsA-frozen/eggs dir.

Flatten the files:

for i in $(lf * | grep / ) ; do
  j=$(echo $i | sed 's!/!---!g')
  echo git mv $i ./$j
done | sh
print 'Importing nanspZ'
from setuptools import setup
setup(
name='nanspZ',
packages=['nanspZ'],
# not sure about this warning, since nnsA is NOT a namespace package, have to ask on the distutils-sig
# WARNING: 'nnsA.B' is declared as a package namespace, but 'nnsA' is not: please correct this in setup.py
# anyway, it has no influence whether the egg is importable by pyinstaller
zip_safe=True
)
print 'Importing nnsA'
import sys
def main():
print sys.path
try:
import nnsA.B
print 'nnsA.B: imported'
except ImportError:
print 'nnsA.B: ImportError'
def Entrypoint(dist, group, name, scripts=None, pathex=None, **kw):
import pkg_resources
scripts = scripts or []
pathex = pathex or []
# get the entry point
ep = pkg_resources.get_entry_info(dist, group, name)
# insert path of the egg at the verify front of the search path
pathex = [ep.dist.location] + pathex
# script name must not be a valid module name to avoid name clashes on import
script_path = os.path.join(WORKPATH, name+'-script.py')
print "writing script for entry point", dist, group, name
fp = open(script_path, 'w')
try:
print >>fp, "import", ep.module_name
print >>fp, "%s.%s()" % (ep.module_name, '.'.join(ep.attrs))
finally:
fp.close()
return Analysis(scripts=scripts+[script_path], pathex=pathex, **kw)
main=Entrypoint(dist='nnsA',group='console_scripts',name='runNnsA',
scripts=[os.path.join(HOMEPATH, 'PyInstaller/loader/_pyi_egg_install.py')])
pyz=PYZ(main.pure)
exeMain=EXE(pyz,
main.scripts,
exclude_binaries=1,
name=os.path.join('build\\pyi.win32\\nnsA', 'runNnsA.exe'),
debug=False,
strip=None,
upx=False,
console=True
)
coll=COLLECT(
exeMain,
main.binaries,
main.zipfiles,
main.datas,
strip=None,
upx=False,
name=os.path.join('dist', 'nnsA-frozen')
)
from setuptools import setup
setup(
name='nnsA',
zip_safe=False,
packages=['nnsA'],
entry_points={
'console_scripts':['runNnsA=nnsA:main']
}
)
__import__('pkg_resources').declare_namespace(__name__)
from setuptools import setup
setup(
name='nnsA.B.C',
packages=['nnsA','nnsA.B','nnsA.B.C'],
# not sure about this warning, since nnsA is NOT a namespace package, have to ask on the distutils-sig
# WARNING: 'nnsA.B' is declared as a package namespace, but 'nnsA' is not: please correct this in setup.py
# anyway, it has no influence whether the egg is importable by pyinstaller
namespace_packages=['nnsA.B'],
zip_safe=True
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment