[Proposal] Give a chance to import cElementTree

John Arbash Meinel john at arbash-meinel.com
Thu Sep 22 16:25:03 BST 2005

Alexander Belchenko wrote:
> There is no notice in README about in which directory must be installed
> cElementTree, but I'm found in bzrlib/xml.py that bzr try import
> cElementTree and ElementTree itself *only* from bzrlib/util directory.
> I think we can give bzr a chance to import cElementTree from standard
> site-packages directory. I propose next solution: temporary add to
> sys.path path to bzrlib/util and try to import cElementTree. After that
> remove added item from sys.path. Patch attached.
> BTW, README claims:
> "If you use the setup.py script then bzr will be installed into the
> specified path.  In this case you must install ElementTree and
> urlgrabber separately."
> But this is not true: there is package "bzrlib.elementtree" listed in
> your setup.py, so user do not need to install ElementTree separately.
> And what is more: bzr do not try to import ElementTree from another
> location that bzrlib/util/elementtree.
> Alexander.

Is this the best way? Or would it be better to try and do:

  from cElementTree import ElementTree, SubElement, Element
except ImportError:
    from util.cElementTree import ElementTree, ...
  except ImportError:
      from elementtree.ElementTree import ElementTree, ...
      from util.elementtree.ElementTree import ElementTree, ...

that way you don't modify sys.path.

Alternatively, I would suggest that we load the local cElementTree or
elementtree before we load the system one. Because we know that the
local one works, and was specifically chosen for us. We don't know if
the system one is the right version, etc.

But thanks for bringing this up. We changed where packages were located,
and probably broke how we were finding cElementTree.

> ------------------------------------------------------------------------
> === modified file 'bzrlib\\xml.py'
> --- bzrlib\xml.py
> +++ bzrlib\xml.py
> @@ -22,10 +22,18 @@
>  # importing this module is fairly slow because it has to load several
>  # ElementTree bits
> +import os.path
> +import sys
> +
> +# give a chance to find cElementTree if one installed in lib\site-packages
> +# add path .../bzrlib/util to sys.path
> +sys.path.append(os.path.join(os.path.split(__file__)[0], "util"))
>  try:
> -    from util.cElementTree import ElementTree, SubElement, Element
> +    from cElementTree import ElementTree, SubElement, Element
>  except ImportError:
> -    from util.elementtree.ElementTree import ElementTree, SubElement, Element
> +    from elementtree.ElementTree import ElementTree, SubElement, Element
> +# remove added path
> +sys.path.pop()
>  from bzrlib.inventory import ROOT_ID, Inventory, InventoryEntry
>  from bzrlib.revision import Revision, RevisionReference        

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 256 bytes
Desc: OpenPGP digital signature
Url : https://lists.ubuntu.com/archives/bazaar/attachments/20050922/432cab34/attachment.pgp 

More information about the bazaar mailing list