Joan Venge
Joan Venge

Reputation: 330972

Transferring Python modules

Basically for this case, I am using the _winreg module in Python v2.6 but the python package I have to use is v2.5. When I try to use:

_winreg.ExpandEnvironmentStrings

it complains about not having this attribute in this module. I have successfully transferred other modules like comtypes from site-packages folder.

But the problem is I don't know which files to copy/replace. Is there a way to do this? Also is site-packages the main places for 3rd party modules?

Upvotes: 0

Views: 240

Answers (1)

bobince
bobince

Reputation: 536379

It's a compiled C extension, not pure Python, so you generally can't simply copy the DLL/so file across from one installation to another: the Python binary interface changes on 0.1 version number updates (but not 0.0.1 updates). In any case, _winreg seems to be statically build into Python.exe on the current official Windows builds rather than being dropped into the ‘DLLs’ folder.

_winreg.ExpandEnvironmentStrings is not available pre-2.6, but you could usefully fall back to os.path.expandvars, which does more or less the same thing. (It also supports $VAR variables, which under Windows you might not want, but this may not be a practical problem.) You're right: %-syntax for expandvars under Windows was only introduced in 2.6, how useless. Looks like you'll need the below.

If the worst comes to the worst it's fairly simple to write by hand:

import re, os

def expandEnvironmentStrings(s):
    r= re.compile('%([^%]+)%')
    return r.sub(lambda m: os.environ.get(m.group(1), m.group(0)), s)

Though either way there is always Python 2.x's inability to read Unicode envvars to worry about.

Upvotes: 2

Related Questions