3

I have in my python workspace two Modules which need sip.pyd
Module1.pyd needs sip.pyd (which implements v 8.0-8.1)
Module2.pyd needs sip.pyd (another file, that implements v6.0)

So I can't just choose the newer one, it doesn't work: I have to keep them both!

(RuntimeError: the sip module implements API v6.0 but the fbx module requires API v8.1)

How can I import a module in .pyd extension (a python dll, not editable), and specify which sip.pyd to source?

As for a workaround, I manage to do that:

  1. One sip.pyd is in my root site-packages location.
  2. If I have to import the module that need the other sip.pyd, I remove root path form sys.path, and I append the precise folder path where the other sip.pyd are.
  3. I can import my Module and restore previous sys.path.
Shreyos Adikari
  • 12,348
  • 19
  • 73
  • 82
gon2024
  • 33
  • 4

3 Answers3

1

VirtualEnv is done to handle those case.

virtualenv is a tool to create isolated Python environments.

Using virtualenv, you will be able to create 2 environements, one with the sip.pyd in version 8.x another in version 6.0

Cédric Julien
  • 78,516
  • 15
  • 127
  • 132
  • Thank you for this tips ! as I have used the other solution, i found VirtualEnv very interesting! I'm sure I will use it somewhere else, sometime. – gon2024 Nov 29 '11 at 09:42
1

Assuming you don't have a piece of code needing both files at once. I'd recommend the following:

  • install both files in 2 separate directories (call them e.g. sip-6.0 and sip-8.0), that you'll place in site-packages/

  • write a sip_helper.py file with code looking like

sip_helper.py contents:

import sys
import re
from os.path import join, dirname
def install_sip(version='6.0'):
    assert version in ('6.0', '8.0'), "unsupported version"
    keep = []
    if 'sip' in sys.modules:
       del sys.modules['sip']
    for path in sys.path:
        if not re.match('.*sip\d\.\d', path):
            keep.append(path)
    sys.path[:] = keep # remove other paths
    sys.path.append(join(dirname(__file__), 'sip-%s' % version))
  • put sip_helper.py in site_packages (the parent directory of the sip-6.0 and sip-8.0 directories)
  • call sip_helper.install_sip at the startup of your programs
gurney alex
  • 13,247
  • 4
  • 43
  • 57
  • Thank you, it works very well! And if I have, in the same code, to toggle between sip i can do: try: import ModuleX except: sip_helper.install_sip() import ModuleX – gon2024 Nov 29 '11 at 09:31
0

I don't know if that works (if a module's name has to match its contents), but can't you just rename them to sip6.pyd resp. sip8.pyd and then do

if need6:
    import sip6 as sip
else:
    import sip8 as sip

?

glglgl
  • 89,107
  • 13
  • 149
  • 217