473,692 Members | 2,261 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Basic importing question

Hey,
Suppose I have a Python application consists of many modules (lets say
it is a Django application).
If all the modules files are importing sys module, how many times the
sys module will be compiled and executed?
Only once (the first time the PVM locates, compiles and executes the
sys module)? or once for each module importing sys?
Thanks.
Aug 20 '08 #1
7 1599
Hussein B a écrit :
Hey,
Suppose I have a Python application consists of many modules (lets say
it is a Django application).
If all the modules files are importing sys module, how many times the
sys module will be compiled and executed?
Only once (the first time the PVM locates, compiles and executes the
sys module)? or once for each module importing sys?
Hmmm.... Well, could it be possible that Python's developper were smart
enough to implement some cache mechanism here ?-)

import do (approximately) the following:

check if the module already exists in sys.modules
if not:
locate the module's source in sys.path
if not found
raise an import error
look for a compiled .py file
if there's none, or if there's one and it's older than the .py file:
compile the source and save it as .pyc
execute the .pyc file and build a module object from it
add the module object to sys.modules
bind the relevant names into your current namespace
Aug 20 '08 #2
On Aug 20, 8:08*pm, Hussein B <hubaghd...@gma il.comwrote:
Hey,
Suppose I have a Python application consists of many modules (lets say
it is a Django application).
If all the modules files are importing sys module, how many times the
sys module will be compiled and executed?
Only once (the first time the PVM locates, compiles and executes the
sys module)? or once for each module importing sys?
Thanks.
sys is a built-in module, so the answer is zero times.

For a non-builtin module foo where there exists:
(1) only a foo.py, it will be compiled into foo.pyc
(2) only a foo.pyc, it will be used
(3) both a foo.py and a foo.pyc, Python compiles the foo.py if the pyc
is out of date or (so I believe[*]) was created by a different
version of Python.

Subsequent imports will use the in-memory copy (in sys.modules, IIRC[*]) ...
[*] == Please save me the bother of checking this in the manual :-)

HTH,
John
Aug 20 '08 #3
On Aug 20, 5:43 am, John Machin <sjmac...@lexic on.netwrote:
On Aug 20, 8:08 pm, Hussein B <hubaghd...@gma il.comwrote:
Hey,
Suppose I have a Python application consists of many modules (lets say
it is a Django application).
If all the modules files are importing sys module, how many times the
sys module will be compiled and executed?
Only once (the first time the PVM locates, compiles and executes the
sys module)? or once for each module importing sys?
Thanks.

sys is a built-in module, so the answer is zero times.

For a non-builtin module foo where there exists:
(1) only a foo.py, it will be compiled into foo.pyc
(2) only a foo.pyc, it will be used
(3) both a foo.py and a foo.pyc, Python compiles the foo.py if the pyc
is out of date or (so I believe[*]) was created by a different
version of Python.

Subsequent imports will use the in-memory copy (in sys.modules, IIRC[*]) ...
[*] == Please save me the bother of checking this in the manual :-)

HTH,
John
One more question:
If I have this structure:
orig/com/domain/project/Klass1.py
Klass2.py
__init__.py

Why com, domain, project should have __init__.py also? they don't
contain source code files?
Thanks again.
Aug 20 '08 #4
Hussein B a écrit :
(snip)
One more question:
If I have this structure:
orig/com/domain/project/Klass1.py
Klass2.py
__init__.py

Why com, domain, project should have __init__.py also?
Yes, why should they ? Unless you want to mimic Java's package system so
you do "import com.domain.proj ect.stuff" - which is IMHO a pretty bad
idea -, there's just no reason to turn all your filesystem into a python
package.

Python's philosophy here is that flat is better than nested.
Aug 20 '08 #5
On Aug 20, 6:39 am, Bruno Desthuilliers <bruno.
42.desthuilli.. .@websiteburo.i nvalidwrote:
Hussein B a écrit :
(snip)
One more question:
If I have this structure:
orig/com/domain/project/Klass1.py
Klass2.py
__init__.py
Why com, domain, project should have __init__.py also?

Yes, why should they ? Unless you want to mimic Java's package system so
you do "import com.domain.proj ect.stuff" - which is IMHO a pretty bad
idea -, there's just no reason to turn all your filesystem into a python
package.

Python's philosophy here is that flat is better than nested.
Sorry I don't follow you :(
Module package is also nested.
Aug 20 '08 #6
Hussein B wrote:
On Aug 20, 6:39 am, Bruno Desthuilliers <bruno.
42.desthuilli.. .@websiteburo.i nvalidwrote:
>Hussein B a écrit :
>>One more question:
If I have this structure:
orig/com/domain/project/Klass1.py
Klass2.py
__init__.py
Why com, domain, project should have __init__.py also?
Yes, why should they ? Unless you want to mimic Java's package system so
you do "import com.domain.proj ect.stuff" - which is IMHO a pretty bad
idea -, there's just no reason to turn all your filesystem into a python
package.

Python's philosophy here is that flat is better than nested.

Sorry I don't follow you :(
Module package is also nested.
Essentially, "flatter is better than more nested" -- in other words
the things you see after "import this" are principles to trend towards,
not commandments to never be violated. Some structure is better
than no structure, but that doesn't mean more structure is better
ad infinitum. Lists of length 1, modules containing a single class,
dictionaries with a single entry, functions or methods that simply
return a variable or instance are "code smells" for over-exercise of
structuring for its own sake. Each layer should have enough structure
that it is "worth" replacing the more straightforward access with a
name.
Similarly, the name should be associated with few enough things
that you might reasonably expect to hold its scope in your head.
A layer with fifty elements "smells" as bad as one with a single
element, just in a different way.

--Scott David Daniels
Sc***********@A cm.Org
Aug 20 '08 #7
Bruno Desthuilliers wrote:
As the name imply, built-in modules are built in the interpreter - IOW,
they are part of the interpreter *exposed* as modules[1]. Unless you
have a taste for gory implementation details, just don't worry about this.

Other "ordinary" modules need of course to be executed once when first
loaded - IOW, the first time they are imported. All statements[2] at the
top-level of the module are then sequentially executed, so that any
relevant object (functions, classes, whatever) are created and bound in
the module's namespace.
Builtin modules like thread, signal etc. as well as C extensions like
socket have an initialization function. The initialization function is
called during the first import of a module. It creates the module object
and assigns objects to its __dict__ attribute.

Builtin modules are statically linked into the Python executable /
library while normal C extensions are shared libraries. Some modules are
builtins because they are required during boot strapping. It's possible
to embed most C modules into the core.

Christian

Aug 20 '08 #8

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

Similar topics

1
2163
by: Al Murphy | last post by:
Hi, I hope that you can help me wit this one please and apologies if i'm in the wrong forum! I recently bought a book that contaiend a CD-ROM of very useful Visual Basic algoritms. Now these algoritms are written in visual basic 4 I'm having difficulty importing the projects into Visual Studio .NET. I get an error message stating: "The Visual Basic 6.0 project file is corrupted or cannot be read". Can anyone tell me what I should do to...
9
1769
by: Frantisek Fuka | last post by:
This thing keeps bugging me. It's probably some basic misunderstanding on my part but I am stumped. Let's say I have two Python files: file.py and file2.py. Their contents is as follows: file.py: --------------------- import file2 def hello(): print "Hello" file2.hello2()
3
1521
by: Player | last post by:
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Hello I am teaching myself python, and I have gotten a long way, it's quite a decent language and the syntax is great :) However I am having a few, "problems" shall we say with certain conventions in python.
2
2029
by: Awah Teh | last post by:
I am working on a project that involves importing IIS Log files into a SQL Server database (and these logfiles are big --> Some up to 2GB in size). Up until now I thought that DTS or the BULK INSERT command was the fastest method out there, but still proves to be long (taking an average of 45mns to an hour to process each log file). Because I have to import the log files from three web heads in my cluster (therefore 2GB log files per...
2
2312
by: Sara | last post by:
Hi - I've been reading the posts for a solution to my query, and realize that I should ask an "approch" question as well. We receive our production data from a third party, so my uers import the data from Excel into the appropriate tables. There are 6 different databases that receive data, though 4 of them only get one table each. I have learned how to automate the data import through
1
1441
by: ANSWER | last post by:
Is there any way I could disable user to create a new database and import all of my tables and queries into his database. I make .mde, disable shift+Enter, make logon users module (with your help sometimes before) but still my tables and queries are free to take. If you know couple solutions please let me know so I can decide. Thanks
7
3302
by: Timothy Shih | last post by:
Hi, I am trying to figure out how to use unmanaged code using P/Invoke. I wrote a simple function which takes in 2 buffers (one a byte buffer, one a char buffer) and copies the contents of the byte buffer into the character pointer. The code looks like the following: #include <stdio.h> #include <stdlib.h> #include "stdafx.h" BOOL APIENTRY DllMain( HANDLE hModule, DWORD ul_reason_for_call,
0
955
by: Rufus A | last post by:
I've been asked to extract some data from tables in Oracle for importing in to Lotus Notes, and am struggling over the SQL which is probably very simple (if you know what you are doing). There's a table that contains an employee's past present jobs. Each row will have the employee id, job title, and effective date (that they started / will start that job). All I need to find is the employee's *current* job title.
0
843
by: TG | last post by:
hi! I am trying to import a text file into a table created on the fly in SQL Server 2005 since the number of columns can vary depending on what the user has checked or not. Here is the code I am using: Dim myConnectionString As String = "Data Source=dr-ny-sql003;Initial Catalog= TEST_TAMMY;Integrated Security=YES"
0
8547
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,...
1
8812
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
8810
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
7639
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6462
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5822
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
4329
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4563
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2242
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.