473,396 Members | 1,784 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,396 software developers and data experts.

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.

dependentpackage
|
+---- __init__.py
+---- somemodule.py
+---- somefolder
|
+---- __init__.py
+---- somesubmodule.py
+---- someotherfolder
etc ....

I simply want the dependentpackage 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

dependentpackage
|
+---- __init__.py

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

I my code I still need to have this work:

from dependentpackage.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 1385
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.

dependentpackage
|
+---- __init__.py
+---- somemodule.py
+---- somefolder
|
+---- __init__.py
+---- somesubmodule.py
+---- someotherfolder
etc ....

I simply want the dependentpackage 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

dependentpackage
|
+---- __init__.py

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

I my code I still need to have this work:

from dependentpackage.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(mypackage.__path__, __name__)

into dependentpackage/__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(mypackage.__path__, __name__)

into dependentpackage/__init__.py might work.


One big caveat: If you are mixing both

import mypackage.somemodule

and

import dependentpackage.somemodule

in the same application, mypackage.somemodule and
dependentpackage.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(mypackage.__path__, __name__)

into dependentpackage/__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 dependentpackage 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
dependentpackage (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
dependentpackage 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(mypackage.__path__, __name__)

into dependentpackage/__init__.py might work.


One big caveat: If you are mixing both

import mypackage.somemodule

and

import dependentpackage.somemodule

in the same application, mypackage.somemodule and
dependentpackage.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
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,...
3
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...
14
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...
4
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
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...
0
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. ...
7
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...
0
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....
0
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,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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...
0
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.