473,327 Members | 2,090 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

Import mechanism to support multiple Python versions

Hi,
I'm trying to support two Python versions at the same time and I'm
trying to find effective mechanisms to support modules compiled in C++
transparently.

All my code in under a single package. Is it possible to override the
import mechanism only for modules under that package and sub-packages so
that?:

import cppmymodule

would be equivalent to:

if sys.version == "2.4":
import cppmymodule24 as cppmymodule
elif sys.version == "2.3":
import cppmymodule23 as cppmymodule

for all modules under the package and all modules with names beginning
with cpp (or another way to identify them).
I have also third party packages. Is it possible to make a package
point to another folder? For example:

psyco23/...
psyco24/...
psyco/__init__.py => points to psyco23 or psyco24 depending on Python
version used.

Note that I cannot use .pth files or symbolic links, since I would want
the exact same code hierarchy to work with both Python 2.3 and 2.4.

Any help appreciated.
Thx and regards,
Nicolas
Jul 18 '05 #1
4 1413
Nicolas Fleury <ni******@yahoo.com_remove_the_> writes:
I have also third party packages. Is it possible to make a package
point to another folder? For example:

psyco23/...
psyco24/...
psyco/__init__.py => points to psyco23 or psyco24 depending on Python
version used.


You may manipulate the package path in the __init__.py file.
pkgutil may also be useful.

Thomas
Jul 18 '05 #2
Nicolas Fleury wrote:
import cppmymodule

would be equivalent to:

if sys.version == "2.4":
import cppmymodule24 as cppmymodule
elif sys.version == "2.3":
import cppmymodule23 as cppmymodule

for all modules under the package and all modules with names beginning
with cpp (or another way to identify them).


Since all my imports are absolute, my code looks more like:

import root.subpackage.cppmymodule

So I guess, I can add this in root/__init__.py and everything would be fine:

def myimport(name, globals=None, locals=None, fromlist=None,
__import__=__import__):
names = name.split('.')
if names[0] == 'root' and names[-1][0:3] == 'cpp':
name += '%s%s' % sys.version_info[0:2]
return __import__(name, globals, locals, fromlist)

__builtins__['__import__'] = myimport

It seems to work, is that right?

Thx and regards,
Nicolas
Jul 18 '05 #3
Nicolas Fleury wrote:
Hi,
I'm trying to support two Python versions at the same time and I'm
trying to find effective mechanisms to support modules compiled in
C++ transparently.

All my code in under a single package. Is it possible to override
the import mechanism only for modules under that package and
sub-packages so that?:

import cppmymodule

would be equivalent to:

if sys.version == "2.4":
import cppmymodule24 as cppmymodule
elif sys.version == "2.3":
import cppmymodule23 as cppmymodule

for all modules under the package and all modules with names
beginning with cpp (or another way to identify them).


I used the following approach application-wide:
===== The very start of main file ===
resolve_package_dependencies()
import package

def main():
...

# boilerplate at the end of main file
def resolve_package_dependencies():
if sys.version_info[0:2] == (2,5):
import package1
sys.modules["package"] = sys.modules["package1"]
else:
import package2
sys.modules["package"] = sys.modules["package2"]

=====================================

I've never needed that for packages like you, but as far as I
remember package specific modules are stored like
"package.module" so aliasing "package45" with "package" in your
case will look like
sys.modules[__name__+".package"] = sys.modules[__name__+".package45"]

Serge.

Jul 18 '05 #4
Nicolas Fleury wrote:
All my code in under a single package. Is it possible to override the
import mechanism only for modules under that package and sub-packages so
that?:


Yes. A package module has a __path__ attribute to which
you can add additional directories to be searched for
submodules of that package. So in your package's
__init__.py you can do something like

if sys.version == "2.4":
subdir = "python24"
elif sys.version == "2.3":
subdir = "python23"

__path__.append(os.path.join(os.path.basename(__fi le__, subdir)))

The directory structure is then

yourpackage/
__init__.py
python23/
cppmymodule.pyd (2.3 version)
python24/
cppmymodule.pyd (2.4 version)

but at run time it will appear as though one version or
the other of cppmymodule is a direct submodule of
yourpackage.

--
Greg Ewing, Computer Science Dept,
University of Canterbury,
Christchurch, New Zealand
http://www.cosc.canterbury.ac.nz/~greg
Jul 18 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
by: Manlio Perillo | last post by:
Hi. I'm a new user of Python but I have noted a little problem. Python is a very good language but it is evolving, in particular its library is evolving. This can be a problem when, ad example,...
43
by: Dan Perl | last post by:
Here is a python feature that I would like: to be able to import modules from an archive like the jar files in Java. Maybe a regular tar file? Maybe a python specific file type, let's call it a...
0
by: Carlos Ribeiro | last post by:
I am looking for information on packages & import hooks, including simple examples on how to implement a simple import hook. Quick googling turns out some documents, such as: -- PEP 302:...
4
by: Andrew James | last post by:
Hi, I've been looking around on Google for the answer to this question, and it's beginning to really bug me. I'm making some design decisions for some code I'm writing, and I'm wondering whether...
4
by: gsocks | last post by:
hello, i have a problem. i would like to import python files above and below my current directory. i'm working on /home/foo/bar/jar.py i would like to import /home/foo/car.py and...
5
by: diffuser78 | last post by:
I have installed wx and everything looks fine. I have written a small app that uses wx. When I run my program from the console like ubuntu $ python PROGRAM_NAME.py it gives error Traceback...
49
by: Martin Unsal | last post by:
I'm using Python for what is becoming a sizeable project and I'm already running into problems organizing code and importing packages. I feel like the Python package system, in particular the...
9
by: rsoh.woodhouse | last post by:
Hi, I'm trying to work out some strange (to me) behaviour that I see when running a python script in two different ways (I've inherited some code that needs to be maintained and integrated with...
5
by: John Ladasky | last post by:
Hi folks, Running Python 2.5 on both a Windows XP laptop, and an Ubuntu Linux 7.04 desktop. I've gotten tired of maintaining multiple copies of my personal modules that I use over and over. ...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.