How to import a standard module in source file with same name?

S

sdhyok

Here is my situation.
To add more functionalities to a standard library(datetime) in python,
I am implementing my own code called vp.datetime
(directory structure, vp/datetime.py).
To make it clear the file extends functions of the standard datetime,
I like to use the same name with its standard library.

The problem is that I have to use the standard library inside
my extended code. But, whenever I try "import datetime",
it assumes my extended module, not the standard module
because of python's default path searching order.

Under the condition that the absolute path to the standard module
is variable in different machines
(so, imp.find_module may not be a solution),
is there an elegant way to solve this problem?

Shin, Daehyok
 
C

Christos TZOTZIOY Georgiou

On 23 Aug 2003 21:52:11 -0700, rumours say that (e-mail address removed)
(sdhyok) might have written:

[snip of problem description as per the subject]
Under the condition that the absolute path to the standard module
is variable in different machines
(so, imp.find_module may not be a solution),
is there an elegant way to solve this problem?

You might try changing the case... call it DateTime for example; there
must be some magic in the C code (if you work on Windows) that matches
in a case-sensitive way. Or you can do (in the importing module,
possibly your main program):

import datetime
import vp.datetime
vp.datetime.datetime = datetime
 
M

Michael Peuser

Christos TZOTZIOY Georgiou said:
On 23 Aug 2003 21:52:11 -0700, rumours say that (e-mail address removed)
(sdhyok) might have written:

[snip of problem description as per the subject]
Under the condition that the absolute path to the standard module
is variable in different machines
(so, imp.find_module may not be a solution),
is there an elegant way to solve this problem?

You might try changing the case... call it DateTime for example; there
must be some magic in the C code (if you work on Windows) that matches
in a case-sensitive way. Or you can do (in the importing module,
possibly your main program):

import datetime
import vp.datetime
vp.datetime.datetime = datetime
--

It i sgeneraqlly not a good idea to call a package _exactly_ as an already
existing one, even if you want to superseed it. Because of Windows' case
insensitivity for files for folders such a distinction should also be
considered bad style. So the solution could be:

User of package:

import newPackage as Package
or
from newPackage import *


Implementation of package:

import Package as oldPackage

and qulifying in case of use of oldPackage accordingly.


Kindly
Michael P
 
S

sdhyok

As Michael indicates, the usage of different case is not a good solution.

Daehyok


Christos "TZOTZIOY" Georgiou said:
On 23 Aug 2003 21:52:11 -0700, rumours say that (e-mail address removed)
(sdhyok) might have written:

[snip of problem description as per the subject]
Under the condition that the absolute path to the standard module
is variable in different machines
(so, imp.find_module may not be a solution),
is there an elegant way to solve this problem?

You might try changing the case... call it DateTime for example; there
must be some magic in the C code (if you work on Windows) that matches
in a case-sensitive way. Or you can do (in the importing module,
possibly your main program):

import datetime
import vp.datetime
vp.datetime.datetime = datetime
 
S

sdhyok

In addition, you can't import the standard datetime with the following
script in vp/datetime.py.

Daehyok
 
S

sdhyok

Thanks, Michael.
If I insist the same name,
is there any technical solution for it?

Daehyok

Michael Peuser said:
Christos TZOTZIOY Georgiou said:
On 23 Aug 2003 21:52:11 -0700, rumours say that (e-mail address removed)
(sdhyok) might have written:

[snip of problem description as per the subject]
Under the condition that the absolute path to the standard module
is variable in different machines
(so, imp.find_module may not be a solution),
is there an elegant way to solve this problem?

You might try changing the case... call it DateTime for example; there
must be some magic in the C code (if you work on Windows) that matches
in a case-sensitive way. Or you can do (in the importing module,
possibly your main program):

import datetime
import vp.datetime
vp.datetime.datetime = datetime
--

It i sgeneraqlly not a good idea to call a package _exactly_ as an already
existing one, even if you want to superseed it. Because of Windows' case
insensitivity for files for folders such a distinction should also be
considered bad style. So the solution could be:

User of package:

import newPackage as Package
or
from newPackage import *


Implementation of package:

import Package as oldPackage

and qulifying in case of use of oldPackage accordingly.


Kindly
Michael P
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top