473,698 Members | 2,192 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

Here is my situation.
To add more functionalities to a standard library(datetim e) 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
Jul 18 '05 #1
5 2150
On 23 Aug 2003 21:52:11 -0700, rumours say that sd****@yahoo.co m
(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.dat etime = datetime
--
TZOTZIOY, I speak England very best,
Microsoft Security Alert: the Matrix began as open source.
Jul 18 '05 #2

"Christos TZOTZIOY Georgiou" <tz**@sil-tec.gr> schrieb im Newsbeitrag
news:gb******** *************** *********@4ax.c om...
On 23 Aug 2003 21:52:11 -0700, rumours say that sd****@yahoo.co m
(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.dat etime = 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
Jul 18 '05 #3
As Michael indicates, the usage of different case is not a good solution.

Daehyok
Christos "TZOTZIOY" Georgiou <tz**@sil-tec.gr> wrote in message news:<gb******* *************** **********@4ax. com>...
On 23 Aug 2003 21:52:11 -0700, rumours say that sd****@yahoo.co m
(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.dat etime = datetime

Jul 18 '05 #4
In addition, you can't import the standard datetime with the following
script in vp/datetime.py.

Daehyok

import datetime
import vp.datetime
vp.datetime.dat etime = datetime

Jul 18 '05 #5
Thanks, Michael.
If I insist the same name,
is there any technical solution for it?

Daehyok

"Michael Peuser" <mp*****@web.de > wrote in message news:<bi******* ******@news.t-online.com>...
"Christos TZOTZIOY Georgiou" <tz**@sil-tec.gr> schrieb im Newsbeitrag
news:gb******** *************** *********@4ax.c om...
On 23 Aug 2003 21:52:11 -0700, rumours say that sd****@yahoo.co m
(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.dat etime = 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

Jul 18 '05 #6

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

Similar topics

0
6907
by: Stian Søiland | last post by:
all examples performed with: Python 2.3+ (#2, Aug 10 2003, 11:09:33) on linux2 (2, 3, 0, 'final', 1) This is a recursive import:
5
2271
by: Petri Savolainen | last post by:
I was trying to roll my own python code importer, but in the end, it seems that no matter what you try, it is always necessary to supply a REAL file object for python lower-level import machinery to work. Is this really true? Or did I miss something? Of course, it is easy to get the module source from somewhere, put it into a tmpfile etc. ... but I'd be nice to be able to skip that extra step. Thanks,
0
1991
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 __import__ hook to trace the actual activity. The source code for the hook is below. There are several examples of the problem in the trace; this is one particularly juicy one. -----------Part 1 ---------------- '0059' Import 'TypeAdapter'...
4
3580
by: Steve Holden | last post by:
I'm trying to load module code from a database, which stores for each module its full name, code, load date and a Boolean indicating whether it's a package or not. The following simple program: import dbimp, sys if __name__ == "__main__": dbimp.install()
5
2472
by: Steve Holden | last post by:
This is even stranger: it makes it if I import the module a second time: import dbimp as dbimp import sys if __name__ == "__main__": dbimp.install() #k = sys.modules.keys() #k.sort() #for kk in k:
2
3091
by: Tian | last post by:
I am writing a python program which needs to support some plug-ins. I have an XML file storing some dynamic structures. XML file records some class names whose instance needs to be created in the run time while parsing the XML file. I wonder what is the best solution for this problem? I also have some problem about the "import". How should I design my packages? Say, I have all code locates at c:\projects\sami, "c:\project" is in my...
79
5248
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
10
3761
by: Ben Finney | last post by:
Howdy all, Question: I have Python modules named without '.py' as the extension, and I'd like to be able to import them. How can I do that? Background: On Unix, I write programs intended to be run as commands to a file with no extension. This allows other programs to use the command as an interface, and I can re-write the program in some other language
49
3927
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 isomorphism between filesystem and namespace, doesn't seem very well suited for big projects. However, I might not really understand the Pythonic way. I'm not sure if I have a specific question here, just a general plea for advice. 1) Namespace....
0
8603
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
9157
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...
0
9026
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8893
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
8861
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
5860
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();...
0
4619
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3045
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
3
2001
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.