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

Problematic behavior of the import statement when several module files have the same name.

Xif
Hello Everyone!

Here's a problem with relative imports:

Suppose I have a package called some_package (in a separate directory
included in the PYTHONPATH, with an __init__.py file etc.)

This package has a module inside the directory, called "database", and
therefore residing in the file some_package/database.py.

Now, what if there's another module, for example inside the
site-packages directory, with the same file name (i.e. database.py)?

We have a problem. Since although these modules have different absolute
names ("some_package.database" for the first module, and just
"database" for the second), when I try to do

import database

from inside some_package, it first of all tries to find the matching
file in the some_package directory (i.e. do a relative import). Since
it first checks the some_package directory, and finds database.py
there,

import database

in fact imports the module with the absolute name
some_package.database.

This is problemat on two levels:

1) It is surprising and undesirable that "import database" in effect
may do two completely different things ("import some_package.database"
or "import database") depending on an external, unpredictable factor:
the existence of a database.py file inside the some_package directory.

2) It effectively prevents you from naming a module inside a package
with the same name of any module in the "root" PYTHONPATH directories.
In my example, there's no sane way I can see of having
some_package.database if there's already a database module (database.py
file) in any PYTHONPATH directory.

Are my observations correct? Is there something I ignored? Should
this be posted somewhere else?

Your comments would be appreciated.

Xif

Jul 18 '05 #1
1 1437
Xif wrote:
Hello Everyone!

Here's a problem with relative imports:

Suppose I have a package called some_package (in a separate directory
included in the PYTHONPATH, with an __init__.py file etc.)

This package has a module inside the directory, called "database", and
therefore residing in the file some_package/database.py.

Now, what if there's another module, for example inside the
site-packages directory, with the same file name (i.e. database.py)?

We have a problem. Since although these modules have different absolute
names ("some_package.database" for the first module, and just
"database" for the second), when I try to do

import database

from inside some_package, it first of all tries to find the matching
file in the some_package directory (i.e. do a relative import). Since
it first checks the some_package directory, and finds database.py
there,

import database

in fact imports the module with the absolute name
some_package.database.


You've just re-discovered the reason you should always use absolute
imports. Check out:

http://www.python.org/doc/faq/progra...rt-in-a-module

STeVe
Jul 18 '05 #2

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

Similar topics

9
by: Frantisek Fuka | last post by:
This thing keeps bugging me. It's probably some basic misunderstanding on my part but I am stumped. Let's say I have two Python files: file.py and file2.py. Their contents is as follows: ...
0
by: John Roth | last post by:
I've found a case where it seems that Python is importing two copies of a module without any reason or indication. It took me a while to verify that this is what is occuring: I had to write a...
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...
79
by: pinkfloydhomer | last post by:
I want to scan a file byte for byte for occurences of the the four byte pattern 0x00000100. I've tried with this: # start import sys numChars = 0 startCode = 0 count = 0
1
by: mirandacascade | last post by:
O/S: Windows 2K Vsn of Python: 2.4 Currently: 1) Folder structure: \workarea\ <- ElementTree files reside here \xml\ \dom\
2
by: | last post by:
I have a breakpoint in an aspx page that I'm using to try to trap some code to see what's going on. I'm translating a page that is working in a traditional ASP page, which takes several session...
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...
3
by: kwatch | last post by:
What is the condition of module name which is available in 'from .. import ..' statement ? ---------------------------------------- import os print os.path # <module 'posixpath'...
3
by: J. Cliff Dyer | last post by:
On Thu, 2008-05-08 at 12:00 -0700, Eric Hanchrow wrote: It's the same reason as this: 5 5 6 5 Python "variables" are just names that point at objects. When you
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: 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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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,...
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...

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.