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

redundant imports

hi everybody.

suppose that code-1.py imports code-2.py and code-3.py (because it uses
names from both), and that code-2.py imports code-3.py.

if python were c, code-1.c should only *include* code-2.c, because the
latter in turns includes code-3.c.

inclusion of modules in c is a purely preprocessing textual matter
(compilation is deferred to after the fact), i guess, so that such
things are possible. import of modules in python is a different beast,
so the "redundancy" is (i think) necessary.

any comment/suggestion/idea?

bye

macs
Jul 18 '05 #1
16 2092
max(01)* wrote:
hi everybody.

suppose that code-1.py imports code-2.py and code-3.py (because it uses
names from both), and that code-2.py imports code-3.py.

if python were c, code-1.c should only *include* code-2.c, because the
latter in turns includes code-3.c.

inclusion of modules in c is a purely preprocessing textual matter
(compilation is deferred to after the fact), i guess, so that such
things are possible. import of modules in python is a different beast,
so the "redundancy" is (i think) necessary.

any comment/suggestion/idea?

bye

macs


It's not as redundant as it looks. Once a module has been imported it goes
into sys.modules and any subsequent imports refer to that original import,
so the overhead of reading and parsing the file is only incurred once. As
you're probably aware, Python also caches compilation results in *.pyc
files; it will only compile the imported module if it changed since the
last compilation.

Check out the docs for the full skinny, in particular
http://www.python.org/doc/2.4/ref/import.html

HTH,
Tim J

--
Website: www DOT jarmania FULLSTOP com
Jul 18 '05 #2
max(01)* wrote:
hi everybody.

suppose that code-1.py imports code-2.py and code-3.py (because it uses
names from both), and that code-2.py imports code-3.py.

if python were c, code-1.c should only *include* code-2.c, because the
latter in turns includes code-3.c.

inclusion of modules in c is a purely preprocessing textual matter
(compilation is deferred to after the fact), i guess, so that such
things are possible. import of modules in python is a different beast,
so the "redundancy" is (i think) necessary.

any comment/suggestion/idea?


You're mixed up about this whole idea.

You say first that "[code-1] uses names from both",
which by definition means that it needs to import
both. (I say by definition because import is *how*
a module gets names from another module.)

Then you say that code-1 can choose not to include
code-3 because some other module is including it...
but so what? That doesn't change the fact that
code-1 needs names from code-3, and just because
code-3 is imported elsewhere is no reason to think
that code-1 magically gets its names too. Should
all modules magically see all names in all modules
which are imported, even by other modules? That
would pretty much defeat most of the value of
namespaces.

Anyway, why this concern over so-called redundant
imports? The source code itself is not parsed
and compiled all over again, and even the .pyc file
is not re-read... once any module has imported a
module any other import just retrieves a reference
to that module from the sys.modules dictionary,
which is practically a free operation.

-Peter
Jul 18 '05 #3
Tim Jarman wrote:
max(01)* wrote:

hi everybody.

suppose that code-1.py imports code-2.py and code-3.py (because it uses
names from both), and that code-2.py imports code-3.py.

if python were c, code-1.c should only *include* code-2.c, because the
latter in turns includes code-3.c.

inclusion of modules in c is a purely preprocessing textual matter
(compilation is deferred to after the fact), i guess, so that such
things are possible. import of modules in python is a different beast,
so the "redundancy" is (i think) necessary.

any comment/suggestion/idea?

bye

macs

It's not as redundant as it looks.


that's why i used quotes ;-)
Once a module has been imported it goes
into sys.modules and any subsequent imports refer to that original import,
so the overhead of reading and parsing the file is only incurred once. As
you're probably aware, Python also caches compilation results in *.pyc
files; it will only compile the imported module if it changed since the
last compilation.

this leads me to another question. since *.pyc files are automatically
created the first time an import statement in executed on a given
module, i guess that if i ship a program with modules for use in a
directory where the user has no write privileges then i must ship the
*.pyc files along too. right?
Check out the docs for the full skinny, in particular
http://www.python.org/doc/2.4/ref/import.html

HTH,
Tim J


thanks a lot

bye

macs
Jul 18 '05 #4
Peter Hansen wrote:
max(01)* wrote:
hi everybody.

suppose that code-1.py imports code-2.py and code-3.py (because it
uses names from both), and that code-2.py imports code-3.py.

if python were c, code-1.c should only *include* code-2.c, because the
latter in turns includes code-3.c.

inclusion of modules in c is a purely preprocessing textual matter
(compilation is deferred to after the fact), i guess, so that such
things are possible. import of modules in python is a different beast,
so the "redundancy" is (i think) necessary.

any comment/suggestion/idea?

You're mixed up about this whole idea.


that's why i am asking for advice here.
You say first that "[code-1] uses names from both",
which by definition means that it needs to import
both. (I say by definition because import is *how*
a module gets names from another module.)

Then you say that code-1 can choose not to include
code-3 because some other module is including it...
but so what? That doesn't change the fact that
code-1 needs names from code-3, and just because
code-3 is imported elsewhere is no reason to think
that code-1 magically gets its names too. Should
all modules magically see all names in all modules
which are imported, even by other modules?
that's how inclusion works in c, which is why i asked for clarification
about the differences between c-style includes and python-style imports.

now i can see that python-style import is more like the inclusion of
declarations in c (the "names"), usually shipped as *.h header files.

the real *definition* of names is python (contents of variables,
definition of functions, and so on) i guess is inserted later in the
process of execution.
That
would pretty much defeat most of the value of
namespaces.

Anyway, why this concern over so-called redundant
imports? The source code itself is not parsed
and compiled all over again, and even the .pyc file
is not re-read... once any module has imported a
module any other import just retrieves a reference
to that module from the sys.modules dictionary,
which is practically a free operation.

-Peter


my concern was motivated by a (clumsy) attempt to understand the
difference of mechanism between the approach to modular programming in a
more "traditional" language (c) and python.

thanks again for your couseling.

bye

macs
Jul 18 '05 #5
max(01)* wrote:
this leads me to another question. since *.pyc files are automatically
created the first time an import statement in executed on a given
module, i guess that if i ship a program with modules for use in a
directory where the user has no write privileges then i must ship the
*.pyc files along too. right?


Not required except for performance reasons. If the .pyc
files don't exist, the .py files are recompiled and the
resulting bytecode is simply held in memory and not cached
and the next startup will recompile all over again.

Note also that the main file (the one invoke from the
command line) is never cached in a .pyc...

-Peter
Jul 18 '05 #6
Peter Hansen wrote:
max(01)* wrote:
this leads me to another question. since *.pyc files are automatically
created the first time an import statement in executed on a given
module, i guess that if i ship a program with modules for use in a
directory where the user has no write privileges then i must ship the
*.pyc files along too. right?

Not required except for performance reasons. If the .pyc
files don't exist, the .py files are recompiled and the
resulting bytecode is simply held in memory and not cached
and the next startup will recompile all over again.

Note also that the main file (the one invoke from the
command line) is never cached in a .pyc...

-Peter

Also, beware that you ship the right .pyc files - they are
version-dependent, so shipping 2.3 binaries to a 2.4 user will actually
cause a small slow-down, since the interpreter will have to check the
..pyc's for the correct magic number before ignoring them.

regards
Steve
--
Steve Holden +1 703 861 4237 +1 800 494 3119
Holden Web LLC http://www.holdenweb.com/
Python Web Programming http://pydish.holdenweb.com/

Jul 18 '05 #7
Peter Hansen wrote:
max(01)* wrote:
this leads me to another question. since *.pyc files are automatically
created the first time an import statement in executed on a given
module, i guess that if i ship a program with modules for use in a
directory where the user has no write privileges then i must ship the
*.pyc files along too. right?

Not required except for performance reasons. If the .pyc
files don't exist, the .py files are recompiled and the
resulting bytecode is simply held in memory and not cached
and the next startup will recompile all over again.

Note also that the main file (the one invoke from the
command line) is never cached in a .pyc...


but the other files *are* compiled, right? so the initial question
remains unanswered: *if* they are compiled, where are they put, if the
corresponding *.py files are on a non-writeable directory?
Jul 18 '05 #8
max(01)* wrote:
Peter Hansen wrote:
Not required except for performance reasons. If the .pyc
files don't exist, the .py files are recompiled and the
resulting bytecode is simply held in memory and not cached
and the next startup will recompile all over again.
but the other files *are* compiled, right?


Yes, definitely. I did say that.
so the initial question remains unanswered:
No it doesn't. I thought I was clear, but I can reword
it for you: the files are compiled *in-memory* and the
results are never written to disk.
*if* they are compiled, where are they put, if the
corresponding *.py files are on a non-writeable directory?


They are not put anywhere. Compilation is a process
by which the source is converted to executable bytecodes.
This says nothing about where either the source or the
compiled result resides. The actual compilation works
on a long series of bytes in memory, and the result is
just another long series of bytes. Nothing requires that
either of these things are even stored in a file.

Not sure what else I could say to make it clearer...

(I'm not misunderstanding you am I? I get the feeling
we're not quite on the same page here.)
-Peter
Jul 18 '05 #9
Peter Hansen wrote:
max(01)* wrote:
Peter Hansen wrote:
Not required except for performance reasons. If the .pyc
files don't exist, the .py files are recompiled and the
resulting bytecode is simply held in memory and not cached
and the next startup will recompile all over again.

but the other files *are* compiled, right?

Yes, definitely. I did say that.
so the initial question remains unanswered:

No it doesn't. I thought I was clear, but I can reword
it for you: the files are compiled *in-memory* and the
results are never written to disk.
> *if* they are compiled, where are they put, if the

corresponding *.py files are on a non-writeable directory?

They are not put anywhere. Compilation is a process
by which the source is converted to executable bytecodes.
This says nothing about where either the source or the
compiled result resides. The actual compilation works
on a long series of bytes in memory, and the result is
just another long series of bytes. Nothing requires that
either of these things are even stored in a file.


ok, maybe it is an implementation-dependent issue after all.

i am working on a debian woody platform with the standard python2.3 package.

consider this:

ma**@172.17.1.201:~/tmp/import-enigma$ ll
total 8
-rw-r--r-- 1 max2 max2 36 2005-04-02 17:44 imported.py
-rw-r--r-- 1 max2 max2 33 2005-04-02 17:44 importer.py
ma**@172.17.1.201:~/tmp/import-enigma$ cat importer.py
import imported
imported.fun_1()
ma**@172.17.1.201:~/tmp/import-enigma$ cat imported.py
def fun_1():
print "I am fun_1()"
ma**@172.17.1.201:~/tmp/import-enigma$ python importer.py
I am fun_1()
ma**@172.17.1.201:~/tmp/import-enigma$ ll
total 12
-rw-r--r-- 1 max2 max2 36 2005-04-02 17:44 imported.py
-rw-r--r-- 1 max2 max2 307 2005-04-02 17:45 imported.pyc
-rw-r--r-- 1 max2 max2 33 2005-04-02 17:44 importer.py
ma**@172.17.1.201:~/tmp/import-enigma$

see?

bye

macs
Jul 18 '05 #10
"max(01)*" <ma**@fisso.casa> writes:
Peter Hansen wrote:
max(01)* wrote:
hi everybody.

suppose that code-1.py imports code-2.py and code-3.py (because it
uses names from both), and that code-2.py imports code-3.py.

if python were c, code-1.c should only *include* code-2.c, because
the latter in turns includes code-3.c.

inclusion of modules in c is a purely preprocessing textual matter
(compilation is deferred to after the fact), i guess, so that such
things are possible. import of modules in python is a different
beast, so the "redundancy" is (i think) necessary.

any comment/suggestion/idea?

You're mixed up about this whole idea.


that's why i am asking for advice here.
my concern was motivated by a (clumsy) attempt to understand the
difference of mechanism between the approach to modular programming in
a more "traditional" language (c) and python.


[Names changed to be valid python module names.]

I feel I ought to point out that you don't really *have* to import the
code_3.py in code_1.py. You can get to things code_3.py in code_1.py
as code_2.code_3.<thing>.

The semantic behavior of "include" in C is the same as "from module
import *" in python. Both cases add all the names in the included
namespace directly to the including namespace. This usage is
depreciated in Python, because it leads to problems figuring out where
a specific variable came from. In C, it creates a problem called "name
space pollution". This is the case when a file1.c gets all the symbols
for some_header.h, even though it doesn't include/need those symbols,
because some header file1.c included needed a symbol from
some_header.h. This is especially galling if the pollution collides
with some_header2.h that file1.c actually needs.

<mike
--
Mike Meyer <mw*@mired.org> http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
Jul 18 '05 #11
On Sat, 02 Apr 2005 16:44:29 -0600, Mike Meyer <mw*@mired.org> wrote:
"max(01)*" <ma**@fisso.casa> writes:
Peter Hansen wrote:
max(01)* wrote:

hi everybody.

suppose that code-1.py imports code-2.py and code-3.py (because it
uses names from both), and that code-2.py imports code-3.py.

if python were c, code-1.c should only *include* code-2.c, because
the latter in turns includes code-3.c.

inclusion of modules in c is a purely preprocessing textual matter
(compilation is deferred to after the fact), i guess, so that such
things are possible. import of modules in python is a different
beast, so the "redundancy" is (i think) necessary.

any comment/suggestion/idea?
You're mixed up about this whole idea.

that's why i am asking for advice here.
my concern was motivated by a (clumsy) attempt to understand the
difference of mechanism between the approach to modular programming in
a more "traditional" language (c) and python.


[Names changed to be valid python module names.]

I feel I ought to point out that you don't really *have* to import the
code_3.py in code_1.py. You can get to things code_3.py in code_1.py
as code_2.code_3.<thing>.

The semantic behavior of "include" in C is the same as "from module

Bzzt ;-) It's not the same semantics!import *" in python. Both cases add all the names in the included
namespace directly to the including namespace. This usage is But a C #include results in processing as if the _source_ were substituted
into the including source in place of the #include line. That's not what
from module import * does, because when import executes the module's source
(happening only the first time BTW, unlike in-place #include)
it creates a module global that is distinct from the includer's global.
The import * creates local bindings to the objects visible as bindings
in the global space of the module, but the objects retain their module
global references (if any, since there doesn't have to be any).

IMO execfile('module.py') is closer to C's #include effect. Note:
print '----\n%s----'%open('impex.py').read() ----
g = 'g in global space of impex.py'
def showg(): print g

---- from impex import *
g 'g in global space of impex.py' showg() g in global space of impex.py g = 'g in global space of importer'
g 'g in global space of importer' showg() g in global space of impex.py

Note that showg insisted on looking for g in it's idea of global.
Now we'll bring in g and showg via execfile:
execfile('impex.py')
g 'g in global space of impex.py' showg() g in global space of impex.py g = 'g in global space of importer'
showg()

g in global space of importer

Note that because execfile executed the definition of showg in the
interactive global space, it sees the interactive change of g's binding
(in what it now also showg's global space).

(Execfile inside a function or class definition needs careful control, but can be done).
depreciated in Python, because it leads to problems figuring out where
a specific variable came from. In C, it creates a problem called "name
space pollution". This is the case when a file1.c gets all the symbols
for some_header.h, even though it doesn't include/need those symbols,
because some header file1.c included needed a symbol from
some_header.h. This is especially galling if the pollution collides
with some_header2.h that file1.c actually needs.

Of course in C you could write some #ifxxx kludges to control inclusion of named things
from a given header file somewhat. But name space pollution is a pox, to be sure ;-)

Regards,
Bengt Richter
Jul 18 '05 #12
Mike Meyer wrote:
The semantic behavior of "include" in C is the same as "from module
import *" in python. Both cases add all the names in the included
namespace directly to the including namespace. This usage is
depreciated in Python ...


Did you mean discouraged? Or it's really slated for deprecation?

Serge.
Jul 18 '05 #13
max(01)* wrote:
Peter Hansen wrote:
No it doesn't. I thought I was clear, but I can reword
it for you: the files are compiled *in-memory* and the
results are never written to disk.
> *if* they are compiled, where are they put, if the
corresponding *.py files are on a non-writeable directory?
They are not put anywhere.


ok, maybe it is an implementation-dependent issue after all.


Not really.
consider this: [snip] -rw-r--r-- 1 max2 max2 307 2005-04-02 17:45 imported.pyc

see?


Yes, but you don't, yet. :-)

Obviously the .pyc file is being written, so my comments
above, out of context, is wrong.

Now please go put them back in context. You asked what
would happen if the directory was not writable. That's
the context in which to interpret my claims that the
bytecode (the *result* of the compilation) is not
written to disk.

I'll try one last time, before giving up in abject
failure and letting someone else take a stab at this:
the compilation will occur every time if a .pyc file
does not exist. The interpreter will attempt to write
the results of the compilation process to disk in a
..pyc file to cache it for the next time, to avoid
having to recompile. *If* this is not possible, then
no caching takes place, no .pyc file is written, and
the next time you run the code, the compilation step
will occur all over again (note: with the results being
held in memory only while the program runs, then
discarded).

Please tell me it's clear now. :-)

-Peter
Jul 18 '05 #14
Serge Orlov wrote:
Mike Meyer wrote:

The semantic behavior of "include" in C is the same as "from module
import *" in python. Both cases add all the names in the included
namespace directly to the including namespace. This usage is
depreciated in Python ...

Did you mean discouraged? Or it's really slated for deprecation?


Deprecated basically means "the use of this is discouraged",
though because it is often followed by a comment like
"and it will be removed in a future release", people
sometimes are misled into thinking "deprecate" refers
to the pending act of removal rather than the discouragement
itself.

So, yes, its use is deprecated (though I'm not sure if that's
by any official statement, or simply by widespread convention),
but no, that doesn't mean it is going to go away any time soon.

-Peter
Jul 18 '05 #15
Peter Hansen wrote:
max(01)* wrote:
Peter Hansen wrote:
No it doesn't. I thought I was clear, but I can reword
it for you: the files are compiled *in-memory* and the
results are never written to disk.

> *if* they are compiled, where are they put, if the

corresponding *.py files are on a non-writeable directory?
They are not put anywhere.

ok, maybe it is an implementation-dependent issue after all.

Not really.
consider this:


[snip]
-rw-r--r-- 1 max2 max2 307 2005-04-02 17:45 imported.pyc

see?

Yes, but you don't, yet. :-)

Obviously the .pyc file is being written, so my comments
above, out of context, is wrong.


oops!
Now please go put them back in context. You asked what
would happen if the directory was not writable. That's
the context in which to interpret my claims that the
bytecode (the *result* of the compilation) is not
written to disk.

I'll try one last time, before giving up in abject
failure and letting someone else take a stab at this:
the compilation will occur every time if a .pyc file
does not exist. The interpreter will attempt to write
the results of the compilation process to disk in a
..pyc file to cache it for the next time, to avoid
having to recompile. *If* this is not possible, then
no caching takes place, no .pyc file is written, and
the next time you run the code, the compilation step
will occur all over again (note: with the results being
held in memory only while the program runs, then
discarded).

Please tell me it's clear now. :-)


it's not clear. it's crystalline. :-)

thanks for your patience and help.

best regards

macs
Jul 18 '05 #16
Mike Meyer wrote:
"max(01)*" <ma**@fisso.casa> writes:

Peter Hansen wrote:
max(01)* wrote:
hi everybody.

suppose that code-1.py imports code-2.py and code-3.py (because it
uses names from both), and that code-2.py imports code-3.py.

if python were c, code-1.c should only *include* code-2.c, because
the latter in turns includes code-3.c.

inclusion of modules in c is a purely preprocessing textual matter
(compilation is deferred to after the fact), i guess, so that such
things are possible. import of modules in python is a different
beast, so the "redundancy" is (i think) necessary.

any comment/suggestion/idea?

You're mixed up about this whole idea.

that's why i am asking for advice here.
my concern was motivated by a (clumsy) attempt to understand the
difference of mechanism between the approach to modular programming in
a more "traditional" language (c) and python.

[Names changed to be valid python module names.]

I feel I ought to point out that you don't really *have* to import the
code_3.py in code_1.py. You can get to things code_3.py in code_1.py
as code_2.code_3.<thing>.


oh. it never occured to me. interesting i must say...
The semantic behavior of "include" in C is the same as "from module
import *" in python. Both cases add all the names in the included
namespace directly to the including namespace. This usage is
depreciated in Python, because it leads to problems figuring out where
a specific variable came from.
so 'import module' is to be preferred, right?
In C, it creates a problem called "name
space pollution". This is the case when a file1.c gets all the symbols
for some_header.h, even though it doesn't include/need those symbols,
because some header file1.c included needed a symbol from
some_header.h. This is especially galling if the pollution collides
with some_header2.h that file1.c actually needs.

<mike


thanks a lot, mike!
Jul 18 '05 #17

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

Similar topics

14
by: Ian Pilcher | last post by:
It's pretty common to see declarations such as: static volatile sig_atomic_t caught_signal = 0; C99 defines sig_atomic_t as a "... (possibly volatile-qualified) integer type of an object that...
40
by: Neo The One | last post by:
I think C# is forcing us to write more code by enforcing a rule that can be summarized as 'A local variable must be assgined *explicitly* before reading its value.' If you are interested in what...
1
by: Steven K | last post by:
Hello, Is there a way to declare imports so that they don't have to be declared both in Master Page.vb and the Content Page.vb? For example, I have the following in the Master Page.vb Imports...
19
by: Tiraman | last post by:
Hi , I have an assembly that hold few class and few imports which are not used in all of the class's . so my question is when to use the "Imports XXX" And when to use this kind of statement...
1
by: Thomas Wittek | last post by:
Hi! Is there any possibility/tool to automatically organize the imports at the beginning of a module? I don't mean automatic imports like autoimp does as I like seeing where my...
5
by: kimiraikkonen | last post by:
Hello, I want to ask about "imports" statement. Some projects must be inserted with "imports xxxx" statements before beginning coding. But how do i know when to use or do i have to use "imports"...
3
by: Mohamed Yousef | last post by:
Hello , The problem I'm asking about is how can imported modules be aware of other imported modules so they don't have to re-import them (avoiding importing problems and Consicing code and...
3
by: Robert | last post by:
A typical declaration in a current project: Dim Graph As New QuickGraph.UndirectedGraph(Of PointVertexType, TaggedEdge(Of PointVertexType, Integer)) This gets passed as a parameter to several...
0
bilibytes
by: bilibytes | last post by:
hi, I was wondering how to determine whether an information is redundant or not. if you have in a table "Main", the basic information for a thing: thing_id | name | image_id | city_id ok if...
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: 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:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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...
0
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...
0
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,...

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.