473,549 Members | 2,243 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

indirect import of standard module

I know this must have been answered a hundred times, but I must be
searching on the wrong terminology.

Let's say I have a module foo.py that imports os.

I make another script called bar.py that imports foo.py and now I want
to use, say, os.walk in bar.py.

Which is faster or more correct or whatever: Do I import os at the top
of bar.py and use foo's functions?

Or do I call os.walk by writing foo.os.walk?

Sorry for lack of proper terminology. I read the module parts of the
Tutorial but couldn't find it there.

Rick

Apr 18 '06 #1
9 2850
Unless you override some of os.* functions in foo, you want to import
os into foo and bar separately.

Python does not reimport the module a second time (create a second
instance of os), it only creates a pointer to the first instance that's
loaded.

Apr 18 '06 #2
You're right!

When running bar.py, id(os) and id(foo.os) give the same number!

Cool. I'll go read about INSTANCES and pointers.

Thank you very much,

rick

Apr 18 '06 #3
"alisonken1 " <al********@gma il.com> writes:
Unless you override some of os.* functions in foo, you want to
import os into foo and bar separately.

Python does not reimport the module a second time (create a second
instance of os)


More accurately, it *does* import it twice, into two separate
namespaces; but loads and executes the code only the first time.

--
\ "God forbid that any book should be banned. The practice is as |
`\ indefensible as infanticide." -- Dame Rebecca West |
_o__) |
Ben Finney

Apr 18 '06 #4
Actually, it does not "execute the code only the first time", more
accurately, it "initialize s the code only the first time".

But you are correct, it exposes the os.* module into the current
namespace so you don't have to go to convoluted lengths to get to it.

Apr 18 '06 #5
>> More accurately, it *does* import it twice, into two separate
namespaces;


If it's in two different namespaces, how can they have the same id
number?

rick

Apr 18 '06 #6

Although 'namespace' may be a misnomer, module interfaces are 'exposed'
to the module that imports it - it's not imported a second time into
the new 'namespace'. The confusion comes about thinking that modules
and classes are related.

When a module is first imported, an instance is created for the module
and the public interfaces/variables are exposed to the module that did
the import. When another call to import the same module from somewhere
else, Python recognizes that the module has already been imported, so
it only creates a reference link to the new module that called import.
That is why you get the same ID number for the module function calls.

It's similar to classes in that Python keeps track of 'instances' of
classes as well as 'instances' of modules, the 'class' instance is
based upon variables which can be either the same class (think of a
call to a function with a class instance as the input) or a new
instance (where the same class is reused, but with different values),
whereas modules are based upon executable bytecode where there is only
one instance at all times during the program execution.

Apr 18 '06 #7
Thank you for the elucidation. I'm just getting into Classes, but I
wanted to get modules down first.

Much appreciated.

rick

Apr 19 '06 #8
"BartlebyScrive ner" <rp*******@gmai l.com> writes:
More accurately, it *does* import it twice, into two separate
namespaces;


If it's in two different namespaces, how can they have the same id
number?


That "two different namespaces" might be a bit inaccurate.

There is only one object representing the module, which is created the
first time it is imported. That object has a unique id.

There can be any number of names referring to that module object, in
different parts of the namespace.

--
\ "I used to work in a fire hydrant factory. You couldn't park |
`\ anywhere near the place." -- Steven Wright |
_o__) |
Ben Finney

Apr 19 '06 #9
BartlebyScriven er wrote:
More accurately, it *does* import it twice, into two separate
namespaces;


If it's in two different namespaces, how can they have the same id
number?


they're mixing up the terminology. each loaded module has exactly one
namespace, no matter how many times you import it.

the modules you're importing to have their own namespaces, of course.

this link may help:

http://effbot.org/zone/import-confus...does-python-do

</F>

Apr 19 '06 #10

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

Similar topics

5
2147
by: sdhyok | last post by:
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...
5
2265
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....
2
3531
by: Marc Ederis | last post by:
Hello, I'm trying to create an executable with py2exe, and it uses the odbc module. The script runs fine until I use py2exe on it and run the ..exe. Then I get: -- Traceback (most recent call last): File "dbmod.py", line 2, in ? File "odbc.pyo", line 9, in ?
6
1706
by: Sean Berry | last post by:
I don't know that this partilarily bad programming, but I was interested in doing the following. def functionname( module, var1, var2 ): import module I would like to be able to pass the module name as an argument and have the module imported as a result. For instance... functionname( os, "1", 234)
0
1219
by: vincent Salaun | last post by:
hi all, i'd like to know if it's possible to import a module from an archive (zip, rar, tar, jar, ...) I've tried using different formats but it still doesn't work ..... i make: >>>sys.path.append('d:/myModules.zip') >>>import aModuleFromTheArchive
14
10365
by: Jay O'Connor | last post by:
Is there a good way to import python files without executing their content? I'm trying some relfection based stuff and I want to be able to import a module dynamically to check it's contents (class ad functions defined) but without having any of the content executed. For example: ---------------------- def test(var): print var
3
1655
by: Phd | last post by:
Hi, I'm using python 2.2, I want to import a module by referring to its relative location. The reason for this is that there is another module with the same name that's already in pythonpath( not my decision, but I got to work around it, bummer). So is there any easy way to do it? something like import module "from this location"
3
1323
by: Andy Leszczynski | last post by:
I have a program which is going to dynamicly load components from some arbitrary defined paths. How to do that? A.
10
2101
by: Jia Lu | last post by:
Hi all: I try to do things below: import i Traceback (most recent call last): File "<pyshell#67>", line 2, in <module> import i ImportError: No module named i But it seems that import donot know what is i ? why?
6
4436
by: Werner | last post by:
Hi, I try to read (and extract) some "self extracting" zipefiles on a Windows system. The standard module zipefile seems not to be able to handle this. False Is there a wrapper or has some one experience with other libaries to
0
7986
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...
1
7504
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...
0
7832
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...
0
6074
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...
1
5391
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5114
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...
0
3518
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1083
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
786
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...

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.