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

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_computing. 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_computing. 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 1452

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__.py" in "lib2" (the package directory
you are "extending"), the following lines:

from pkgutil import extend_path
__path__ = extend_path(__path__, __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__.py" in "lib2" (the package directory
you are "extending"), the following lines:

from pkgutil import extend_path
__path__ = extend_path(__path__, __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
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...
7
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
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...
4
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...
5
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...
0
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...
14
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...
2
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...
2
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...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
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,...

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.