473,714 Members | 4,240 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

package search

I have two directories, lib1 and lib2, that both contain the package
foo, one with the submodule mod1
and the other with the submodule mod2:

$ ls lib*/foo/
lib1/foo/:
__init__.py __init__.pyc mod1.py mod1.pyc

lib2/foo/:
__init__.py __init__.pyc mod2.py mod2.pyc
$

Now this script:

import sys
sys.path.append ("lib1")
sys.path.append ("lib2")
import foo.mod1

will find the module foo.mod1, while the same script with the two
append-lines interchanged will not:

import sys
sys.path.append ("lib2")
sys.path.append ("lib1")
import foo.mod1

The error is:

import foo.mod1
ImportError: No module named mod1

So I guess that when Python finds the package foo, it will not look for
it again using the rest of
the search path, even if at that first place the module could not be
found.

Other languages like e.g. Java would find the module mod1 in the second
case too (if Java doesn't
find it in the first place, it will also look for it in the second
place).

This behaviour of Python is very unfortunate, because you always have
to merge your package
trees. In my cvs I have several packages, each in its own cvs module,
all starting with
de.science_comp uting. Now if I need two of them in a new project, I
will have two package trees
in my cvs sandbox, both starting with de.science_comp uting. Leaving the
sandbox that way, cvs
will work but Python imports will fail while if I merge all the package
trees, Python will work but cvs
won't.

My questions:
- Have I done anything wrong?
- If not, is this behaviour of Python a bug or intentional?
- If it it intentional, what is the intent?

Thank you for your help
Boris

Jun 11 '06 #1
4 1469

Sybren Stuvel wrote:
Simplicity and explicitness. You have two packages 'foo', and it
simply loads the first one it finds.


Yes, here you are right. It is indeed simple.

Boris

Jun 11 '06 #2
boris ha scritto:
I have two directories, lib1 and lib2, that both contain the package
foo, one with the submodule mod1
and the other with the submodule mod2:
[...]
Now this script:

import sys
sys.path.append ("lib1")
sys.path.append ("lib2")
import foo.mod1

will find the module foo.mod1, while the same script with the two
append-lines interchanged will not:

import sys
sys.path.append ("lib2")
sys.path.append ("lib1")
import foo.mod1

The error is:

import foo.mod1
ImportError: No module named mod1
[...]


You just have to put in "__init__.p y" in "lib2" (the package directory
you are "extending" ), the following lines:

from pkgutil import extend_path
__path__ = extend_path(__p ath__, __name__)

"__path__", in each __init__ module, is a list initialized with the
module's path, but you can extend it by appending paths where you want
the interpreter to look for further modules.
pkgutil.extend_ path automatically appends to __path__ all subdirectories
of directories on sys.path named after the package.

HTH :-)

Diego.
Jun 11 '06 #3

imho wrote:

You just have to put in "__init__.p y" in "lib2" (the package directory
you are "extending" ), the following lines:

from pkgutil import extend_path
__path__ = extend_path(__p ath__, __name__)

"__path__", in each __init__ module, is a list initialized with the
module's path, but you can extend it by appending paths where you want
the interpreter to look for further modules.
pkgutil.extend_ path automatically appends to __path__ all subdirectories
of directories on sys.path named after the package.

HTH :-)

Diego.


COOL! You just saved me an awful lot of work.

Thanks, Diego!

Boris

Jun 11 '06 #4
boris ha scritto:

COOL! You just saved me an awful lot of work.

Thanks, Diego!

Boris


;-)

Bye
Jun 11 '06 #5

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

Similar topics

2
7659
by: Bob Walpole | last post by:
I am somewhat a novice with vb6 however I have completed a program which needs to be shared by several people if it is to be of major use. I have used the Package and Deploymnet Wizard so that I can distribute it by CD. I have also loaded the program for CD via set up back on to my computer in different directory successfully. However it fails to load onto other computers using windows XP (as is mine) The Program includes DatePicker,...
7
3275
by: Gez | last post by:
Hi, Does anyone know if there is a wavelet package available for Python? This package should at least have the Daubechy wavelet. Cheers, Gez 75
2
1687
by: Skip Montanaro | last post by:
At work we have a package structure for our homegrown code. Developers each have their own sandboxes where they work on the stuff they are responsible for (mixtures of C++ libraries, SWIG wrappers and pure Python packages). We're running into a problem where we have two versions of the main package, one in the developer's sandbox that contains just the developer's bits and one in the central location which contains all the installed stuff....
4
8182
by: Birahim FALL | last post by:
Hi, I was an oracle dev/admin and I'm quite new to postgresql. Postgresql is great, but something bothers me. It seems that there's no concept of PACKAGE in PL/pgSQL as in Oracle PL/SQL. Is is definitely that? or did I missed something? or is it planned for a future version? I've searched in the mailing lists archives, read many lines about schema / Package support in Postgresql,
5
3029
by: kevin bailey | last post by:
how do i call, run or execute a DTS package from an APS (aspx) page? platform win2k iis (the one that comes with win2k) sql server 2000 - with a test dts package ready to be called ..NET framework 1.1 installed and working correctly web matrix is being used to create the files.
0
2857
by: dashprasannajit | last post by:
package djvusearching; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import org.apache.lucene.analysis.cjk.CJKAnalyzer; //import org.apache.lucene.analysis.standard.StandardAnalyz er; import org.apache.lucene.queryParser.ParseException;
14
6747
by: amitsoni.1984 | last post by:
hi, I have some values(say from -a to a) stored in a vector and I want to plot a histogram for those values. How can I get it done in python. I have installed and imported the Matplotlib package but on executing the code =hist(eig, 10) # make a histogram I am getting an error saying "NameError: name 'hist' is not defined". Is there any other way to plot histograms over a given range?
2
7177
by: Bennett Haselton | last post by:
How do I find the mapping between a PHP package name like HTTP_Request, and the "yum" command to install it on CentOS 4.4? For example when I needed to use LWP::UserAgent to use in Perl, the command was yum install -y perl-libwww-perl but I still don't remember how I found that out, I think someone just told me. I never found how to get the mapping between the package name "LWP::UserAgent" and the yum argument "perl-libwww-perl".
2
2285
by: flickle1 | last post by:
Hi, I created a website in PHP www.poundsback.com and i want to know how to make Friendly URL's if you browse the website you will see that some of the urls are quite long in size. I heard already you can use .htaccess
0
8712
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9316
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...
1
9077
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,...
0
9023
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7954
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5959
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();...
1
3160
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
2
2525
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2113
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.