473,594 Members | 2,806 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Package that imports with name of dependent package

Hi. I have code that currently depends on a particular package of a
framework. I have decided I want to create my own package because I have
made many changes and it is getting too difficult to maintain each time
I retrieve an updated version of the framework from svn.

The problem is, that there are all sorts of imports to the dependent
package throughout my code and I just want to replace this module with
something that will provide a reference to my own package without
changing the original imports. So it just needs to point to the new
package so that the original imports in my code will continue to work.

For example, here is a package structure.

dependentpackag e
|
+---- __init__.py
+---- somemodule.py
+---- somefolder
|
+---- __init__.py
+---- somesubmodule.p y
+---- someotherfolder
etc ....

I simply want the dependentpackag e to point to the new package leaving
no more than an init file or whatever would have to be minimally
required to make this work

dependentpackag e
|
+---- __init__.py

mypackage
|
+---- __init__.py
+---- somemodule.py
+---- somefolder
|
+---- __init__.py
+---- somesubmodule.p y
+---- someotherfolder
etc ....

I my code I still need to have this work:

from dependentpackag e.somemodule import something

- but I want the package to be getting the code from the new module.

I'd appreciate hearing of what I can do in an __init__ file or what
other strategy could make this work. Many thanks.

Regards,
David

May 13 '06 #1
4 1391
David Pratt wrote:
Hi. I have code that currently depends on a particular package of a
framework. I have decided I want to create my own package because I have
made many changes and it is getting too difficult to maintain each time
I retrieve an updated version of the framework from svn.

The problem is, that there are all sorts of imports to the dependent
package throughout my code and I just want to replace this module with
something that will provide a reference to my own package without
changing the original imports. So it just needs to point to the new
package so that the original imports in my code will continue to work.

For example, here is a package structure.

dependentpackag e
|
+---- __init__.py
+---- somemodule.py
+---- somefolder
|
+---- __init__.py
+---- somesubmodule.p y
+---- someotherfolder
etc ....

I simply want the dependentpackag e to point to the new package leaving
no more than an init file or whatever would have to be minimally
required to make this work

dependentpackag e
|
+---- __init__.py

mypackage
|
+---- __init__.py
+---- somemodule.py
+---- somefolder
|
+---- __init__.py
+---- somesubmodule.p y
+---- someotherfolder
etc ....

I my code I still need to have this work:

from dependentpackag e.somemodule import something

- but I want the package to be getting the code from the new module.

I'd appreciate hearing of what I can do in an __init__ file or what
other strategy could make this work. Many thanks.


I think fixing the imports is the better long-term approach. But putting

from pkgutil import extend_path
import mypackage
__path__ = extend_path(myp ackage.__path__ , __name__)

into dependentpackag e/__init__.py might work.

Peter

May 13 '06 #2
Peter Otten wrote:
I'd appreciate hearing of what I can do in an __init__ file or what
other strategy could make this work. Many thanks.


I think fixing the imports is the better long-term approach. But putting

from pkgutil import extend_path
import mypackage
__path__ = extend_path(myp ackage.__path__ , __name__)

into dependentpackag e/__init__.py might work.


One big caveat: If you are mixing both

import mypackage.somem odule

and

import dependentpackag e.somemodule

in the same application, mypackage.somem odule and
dependentpackag e.somemodule are *not* the same module instance. This may
have surprising effects when global variables in somemodule.py are
updated...

Peter
May 13 '06 #3
Hi Peter. I'd like to fix the imports, but this would impact the
portability of portions of the code that currently work with the
existing package from the framework.

This solution does the trick and allows me to create the package I want
using a good amount of new material. I don't have to worry about adding
to the original package each time a release comes out. I'll only have to
monitor code changes for an impact on my classes, subclasses, etc. Still
a pain, but a smaller one :-) Many thanks.

Regards
David
Peter Otten wrote:
from pkgutil import extend_path
import mypackage
__path__ = extend_path(myp ackage.__path__ , __name__)

into dependentpackag e/__init__.py might work.

Peter

May 13 '06 #4
Hi Peter. Thank you for this warning. I'll document this in the code. I
plan on importing only from dependentpackag e for portability. Also, much
in the framework relies upon it. This approach is primarily for
maintenance purposes and would also allow me to package the modified
dependentpackag e (with just the __init__.py ) along with mypackage if I
plan to distribute it. It allows it to be a drop in replacement.

I am hoping with packaging utilities, I can easily remove the
dependentpackag e if it is encountered in site-packages to replace it
with my own.

Regards,
David
Peter Otten wrote:
Peter Otten wrote:
I'd appreciate hearing of what I can do in an __init__ file or what
other strategy could make this work. Many thanks.

I think fixing the imports is the better long-term approach. But putting

from pkgutil import extend_path
import mypackage
__path__ = extend_path(myp ackage.__path__ , __name__)

into dependentpackag e/__init__.py might work.


One big caveat: If you are mixing both

import mypackage.somem odule

and

import dependentpackag e.somemodule

in the same application, mypackage.somem odule and
dependentpackag e.somemodule are *not* the same module instance. This may
have surprising effects when global variables in somemodule.py are
updated...

Peter

May 13 '06 #5

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

Similar topics

4
4184
by: Robert Ferrell | last post by:
I have a question about how to organize modules in a package. I think there is something fundamental that I am missing. Say I'm creating a package, GreatPackage, which has three sub-packages, myUtilities, GoodStuff, MoreGoodStuff. The directories look like: GreatPackage |
3
3808
by: George P | last post by:
I've run into a strange package related namespace problem. Follow these steps (on UNIX system) to illustrate: ------------------------- mkdir /tmp/mypkg cd /tmp/mypkg touch __init__.py echo 'import os\ndef test():\n print os.getcwd()' > os.py cd /tmp
14
1561
by: ToddLMorgan | last post by:
Summary: How should multiple (related) projects be arranged (structured) and configured so that the following is possible: o Sharing common code (one of the projects would be a "common" project referenced by all others and likely the others would share at least the common project and possibly more as times goes on) o Clear separation of "production" code and "test" code (ie to readily ship source and test as separate components. Usually...
4
1826
by: Martin Blais | last post by:
Hi I'm a tad confused over a problem involving cycles between packages. Assume the following sets of files:: driver.py a/__init__.py a/alice.py
0
1098
by: Gabriel Genellina | last post by:
En Fri, 13 Jun 2008 20:01:56 -0300, Dan Yamins <dyamins@gmail.com> escribió: Note that if you execute dir() at this point, you'll see the Operations name, *not* Operations.archive. The statement "import Operations.archive" first tries to locate and load a module named Operations - and *that* name is added to the current namespace, not Operations.archive (which is an invalid name by itself).
0
1059
by: Roger Ineichen | last post by:
Hi Tim For a usecase like this, I personaly recommend to defina all interfaces in one module which probably is a namespace if you need alot of interfaces to define. e.g. openehr.interfaces.foobar.IFooBar
7
2180
by: alito | last post by:
Hi all, I am new to using packages to group my modules. I can't figure out how to run a module that uses relative imports without writing a wrapper that imports that module. Everything I try it complains that I am attempting a relative import in a non-package. eg ~/python/testpackage$ ls config.py importer.py __init__.py
0
751
by: David Pratt | last post by:
Hi. I am in the midst of preparing a package to convert between various schemas including orms. The issue is I don't want django, slqalchemy, storm, rdflib etc. as hard dependencies of the package. Each module is a schema to schema conversion. As an example, I have imports for sqlalchemy with classes and methods that use them. from sqlalchemy.util import OrderedDict from sqlalchemy import types as rdbtype import sqlalchemy as sa
0
864
by: Chris Rebert | last post by:
On Sun, Sep 28, 2008 at 7:10 AM, David Pratt <fairwinds.dp@gmail.comwrote: Why not just catch the ImportError-s and set some variables depending on whether you were able to import the modules, e.g.: # repeat for each soft-depended module try: import django_module except ImportError: imported_django = False
0
8253
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8374
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8009
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
5739
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5411
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
3867
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
3903
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2389
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1482
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.