473,772 Members | 2,414 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2132
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
(compilatio n 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 "traditiona l" 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 misunderstandin g 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.2 01:~/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.2 01:~/tmp/import-enigma$ cat importer.py
import imported
imported.fun_1( )
ma**@172.17.1.2 01:~/tmp/import-enigma$ cat imported.py
def fun_1():
print "I am fun_1()"
ma**@172.17.1.2 01:~/tmp/import-enigma$ python importer.py
I am fun_1()
ma**@172.17.1.2 01:~/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.2 01:~/tmp/import-enigma$

see?

bye

macs
Jul 18 '05 #10

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

Similar topics

14
11981
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 can be accessed as an atomic entity, even in the presence of asynchronous interrupts." Does this mean that the use of "volatile" in the above declaration is redundant? (It sure sounds that way to me.)
40
3056
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 I mean, please look at this feedback my me: http://lab.msdn.microsoft.com/productfeedback/viewfeedback.aspx?feedbackid=3074c204-04e4-4383-9dd2-d266472a84ac If you think I am right, please vote for this feedback.
1
1901
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 System Imports System.Web Imports System.Web.UI Imports System.Web.UI.WebControls Imports System.Data
19
1851
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 Dim X as New System.XXX ....
1
1892
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 objects/functions really come from. For the same reason I don't like "from foo import *". The downside is that you have a rather verbose import section at the
5
13369
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" statement? How will i know which "imports" will my project need? From books or how can i guess? Thanks...
3
1827
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 imports ) Take Example :- in A.py :- import B print dir() # no problems we can see B which contain re module and C module
3
1689
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 subroutines. I am sick of typing this verbose redundant declaration. Is there an easy way to define this once at the top of a module and then reuse this definition in all the other ByRefs? Such as:
0
1511
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 i want to know the country_name for some thing_id, i will have to make two subqueries. because i have the location organized this way: Table cities: city_id | country_id | city_name
0
9619
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9454
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
10261
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
10103
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...
0
9911
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
6713
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
5482
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4007
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
2850
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.