how to EXCLUDE a module

2,483 views
Skip to first unread message

Gelonida G

unread,
Sep 20, 2014, 11:00:27 AM9/20/14
to pyins...@googlegroups.com
Sometimes I have python code with conditional imports.

Sometimes I know, that the version, that I want to ship with ipython
must not (will never) import certain dependencies.

Thus my question of how to tell pyinstaller to not 'autodiscover'
certain conditional imports and its dependencies



Two examples:


1.) Alternative bindings
---------------------------
I have code, that uses try except to import either PyQt or PySide.
Obviously I don't want to ship with PySide and PyQT.



2.) Exclude optional modules
-------------------------------

There seems to be a bug in ipython, that causes my build to fail.

The problem is related to an import of Ipython.terminal.embed


For the time being (until I know the real solution for this issue)
I could live with the fact, that only my local release uses ipython and
the version created with pyinstaller would not use ipython. (it's mostly
for debugging anyway.)

Thus I had to know how to tell pyinstaller to NOT consider my
conditional import of
Ipython.terminal.embed


To show you how my code would look like:

#!/usr/bin/env python
import sys

args = sys.argv[1:]
if len(args) >= 1 and args[0] == 'use_ipython':
print("special functionality never being used with a "
"pyinstaller version")
from IPython.terminal.embed import InteractiveShellEmbed

print("this should work with pyinstaller")
print("ARGS %r" % args)
# ####### end of code

Hartmut Goebel

unread,
Sep 20, 2014, 12:30:26 PM9/20/14
to pyins...@googlegroups.com
Am 20.09.2014 06:09, schrieb Gelonida G:
Thus my question of how to tell pyinstaller to not 'autodiscover' certain conditional imports and its dependencies


One way is to use a virtual env containing onle the required modules.

Or you can manipulate result of the Analysis() call in the .spec-file, see <http://pythonhosted.org/PyInstaller/#id38>

--
Schönen Gruß
Hartmut Goebel
Dipl.-Informatiker (univ), CISSP, CSSLP
Information Security Management, Security Governance, Secure Software Development

Goebel Consult, Landshut
http://www.goebel-consult.de

Blog: http://www.goebel-consult.de/blog/einladung-von-der-friedrich-ebert-stiftung
Kolumne: http://www.cissp-gefluester.de/2011-08-horrorszenario-bring-your-own-device

Goebel Consult ist Mitglied bei http://www.7-it.de/

Gelonida N

unread,
Sep 23, 2014, 5:02:59 PM9/23/14
to pyins...@googlegroups.com
Hi Hartmut,

On 09/20/2014 06:30 PM, Hartmut Goebel wrote:
> Am 20.09.2014 06:09, schrieb Gelonida G:
>> Thus my question of how to tell pyinstaller to not 'autodiscover'
>> certain conditional imports and its dependencies
>
>
> One way is to use a virtual env containing onle the required modules.

Sounds like a good solution:
Howver I have one small problem:
a virtualenv can be created with or without system-site-packages

What I would need is to include some system-site-packages (e.g. pywin32,
but to exclude others PyQT)

Is this possible with virtualenv?


>
> Or you can manipulate result of the Analysis() call in the .spec-file,
> see <http://pythonhosted.org/PyInstaller/#id38>

Also an option:
Does somebody have a plugin / code to exclude all PyQT dependencies?

I'll probably manage to implement this somehow, but wonder whether this
would be as elegant as some existing solutions.

Hartmut Goebel

unread,
Sep 23, 2014, 5:19:52 PM9/23/14
to pyins...@googlegroups.com
Am 23.09.2014 22:51, schrieb Gelonida N:
What I would need is to include some system-site-packages (e.g. pywin32, but to exclude others PyQT)

The most simple way should be to remove the links from <virtualenv>/lib/python/site-packages.

But to repeat it: For what you want to get, you should better build a proper deb-package.


--
Schönen Gruß
Hartmut Goebel
Dipl.-Informatiker (univ), CISSP, CSSLP
Information Security Management, Security Governance, Secure Software Development

Goebel Consult, Landshut
http://www.goebel-consult.de

Gelonida N

unread,
Sep 23, 2014, 5:21:58 PM9/23/14
to pyins...@googlegroups.com
Hi Hartmut,


I tried to follow your suggestion and setup a virtualenv containing just
the modules, that are allowed to be auto-discovered.

First error message, that I get is:
Error: PyInstaller for Python 2.6+ on Windows needs pywin32.
Please install from http://sourceforge.net/projects/pywin32/

How can I install pywin32 in a virtualenv?

PLease see below a few success less attempts:
> (mypy) d:\>pip install pywin32
> Downloading/unpacking pywin32
> Could not find any downloads that satisfy the requirement pywin32
> Some externally hosted files were ignored (use --allow-external pywin32 to allow).
> Cleaning up...
> No distributions at all found for pywin32
> Storing debug log for failure in C:\Users\gelonida\pip\pip.log

> (mypy) d:\>pip install --allow-external pywin32 pywin32
> Downloading/unpacking pywin32
> Could not find any downloads that satisfy the requirement pywin32
> Some insecure and unverifiable files were ignored (use --allow-unverified pywin32 to allow).
> Cleaning up...
> No distributions at all found for pywin32
> Storing debug log for failure in C:\Users\gelonida\pip\pip.log


> (mypy) d:\>pip install --allow-external pywin32 --allow-unverified pywin32 pywin32
> Downloading/unpacking pywin32
> Could not find any downloads that satisfy the requirement pywin32
> Cleaning up...
> No distributions at all found for pywin32
> Storing debug log for failure in C:\Users\gelonida\pip\pip.log




On 09/20/2014 06:30 PM, Hartmut Goebel wrote:

Bryan A. Jones

unread,
Sep 23, 2014, 5:26:07 PM9/23/14
to pyins...@googlegroups.com
I *think* I've manipuled the TOC in a spec file to remove unwanted imports -- would that work? Here's an example (from long ago) of excluding Tk:

# -*- mode: python -*-

a = Analysis(['code_chat.py'],
             pathex=['C:\\Users\\bjones\\Documents\\documentation'],
             hiddenimports=[],
             hookspath=['pyinstaller_hooks'],
             excludes=['_tkinter'])   # ********************* THIS LINE **********************
pyz = PYZ(a.pure)
exe = EXE(pyz,
          a.scripts, # + [('v', '', 'OPTION')], # for extra run-time debug
          exclude_binaries=1,
          name=os.path.join('build\\pyi.win32\\code_chat', 'code_chat.exe'),
          debug=False,
          strip=None,
          upx=False,
          console=True )
coll = COLLECT(exe,
               a.binaries,
               a.zipfiles,
               a.datas,
               strip=None,
               upx=False,
               name=os.path.join('dist', 'code_chat'))

Bryan



--
You received this message because you are subscribed to the Google Groups "PyInstaller" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pyinstaller+unsubscribe@googlegroups.com.
To post to this group, send email to pyins...@googlegroups.com.
Visit this group at http://groups.google.com/group/pyinstaller.
For more options, visit https://groups.google.com/d/optout.



--
Bryan A. Jones, Ph.D.
Associate Professor
Department of Electrical and Computer Engineering
231 Simrall / PO Box 9571
Mississippi State University
Mississippi state, MS 39762
http://www.ece.msstate.edu/~bjones
bjones AT ece DOT msstate DOT edu
voice 662-325-3149
fax 662-325-2298

Our Master, Jesus Christ, is on his way. He'll show up right on
time, his arrival guaranteed by the Blessed and Undisputed Ruler,
High King, High God.
- 1 Tim. 6:14b-15 (The Message)

Gelonida N

unread,
Sep 23, 2014, 8:00:00 PM9/23/14
to pyins...@googlegroups.com
On 09/23/2014 11:19 PM, Hartmut Goebel wrote:
> Am 23.09.2014 22:51, schrieb Gelonida N:
>> What I would need is to include some system-site-packages (e.g.
>> pywin32, but to exclude others PyQT)
>
> The most simple way should be to remove the links from
> <virtualenv>/lib/python/site-packages.
>
> But to repeat it: For what you want to get, you should better build a
> proper deb-package.
>

>
I'm running under Windows. That's why I have the dependency on win32.
Not sure, but I thought you suggested to somebody else to create a
proper deb-package.

Pyinstaller seems in my opinion quite appropriate for my use case.

I try to create a self contained version of a Python application for
Windows, that should still allow some degrees of simple debugging /
patching and that should not be bigger than necessary and that should
not contain PyQt and PySide as one binding is enough.

However some of the modules I'm using do try to pull in both of them.
that's why I try to exclude PyQT.



Hartmut Goebel

unread,
Sep 24, 2014, 3:43:31 AM9/24/14
to pyins...@googlegroups.com
Am 24.09.2014 01:59, schrieb Gelonida N:
Not sure, but I thought you suggested to somebody else to create a proper deb-package.

Yes, seams as if I mixed the cases :-)


--
Schönen Gruß
Hartmut Goebel
Dipl.-Informatiker (univ), CISSP, CSSLP
Information Security Management, Security Governance, Secure Software Development

Goebel Consult, Landshut
http://www.goebel-consult.de

Hartmut Goebel

unread,
Sep 24, 2014, 3:45:39 AM9/24/14
to pyins...@googlegroups.com
Am 23.09.2014 23:18, schrieb Gelonida N:
How can I install pywin32 in a virtualenv?

Searching the web for exactly this question gives several hits. The first one looks okay for me and fitting exactly your case:  <http://stackoverflow.com/questions/1830304/how-can-i-use-pywin32-with-a-virtualenv-without-having-to-include-the-host-envir>

--
Schönen Gruß
Hartmut Goebel
Dipl.-Informatiker (univ), CISSP, CSSLP
Information Security Management, Security Governance, Secure Software Development

Goebel Consult, Landshut
http://www.goebel-consult.de

Hartmut Goebel

unread,
Sep 24, 2014, 4:03:04 AM9/24/14
to pyins...@googlegroups.com
Am 24.09.2014 01:59, schrieb Gelonida N:
I try to create a self contained version of a Python application for Windows, that should still allow some degrees of simple debugging / patching and that should not be bigger than necessary and that should not contain PyQt and PySide as one binding is enough.

However some of the modules I'm using do try to pull in both of them. that's why I try to exclude PyQT.

For this case, the solution Brian suggested, sounds good. ``excludes`` is documented in the code as:

                An optional list of module or package names (their Python names,
                not path names) that will be ignored (as though they were not found).

I just found that this is not documented in the manual :-(


--
Schönen Gruß
Hartmut Goebel
Dipl.-Informatiker (univ), CISSP, CSSLP
Information Security Management, Security Governance, Secure Software Development

Goebel Consult, Landshut
http://www.goebel-consult.de

Gelonida N

unread,
Sep 24, 2014, 6:53:24 AM9/24/14
to public-pyinstaller-/...@plane.gmane.org


On 09/24/2014 09:45 AM, Hartmut Goebel wrote:
> Am 23.09.2014 23:18, schrieb Gelonida N:
>> How can I install pywin32 in a virtualenv?
>
> Searching the web for exactly this question gives several hits. The
> first one looks okay for me and fitting exactly your case:
> <http://stackoverflow.com/questions/1830304/how-can-i-use-pywin32-with-a-virtualenv-without-having-to-include-the-host-envir>
>

Thanks a lot.
Yes following command (from your url) works.

easy_install pywin32-291.win32-py2.7.exe

works fine. I always forget, that pip is a little limited for installing
binary packages.



Reply all
Reply to author
Forward
0 new messages