473,320 Members | 2,092 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,320 software developers and data experts.

Imports visibility in imported modules problem

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
B.C.W() # our problem here we get an empty list

in B.py :-

import re
import C

in C.py :-

def W():
print dir() # when called from A we get [] though other imports
has been made to re and others

my goal is basically making W() aware of the re module when called from A
----
why am i doing this in the first place I'm in the process of a medium
project where imports of modules start to make a jungle and i wanted
all needed imports to be in a single file (namely __init__.py) and
then all imports are made once and other modules feel it

another reason to do this that my project is offering 2 interfaces
(Console and GUI through Qt) and i needed a general state class (
whether i'm in Console or GUI mode) to be available for all , for
determining state and some public functions ,and just injecting
Imports everywhere seems a bad technique in many ways (debugging ,
modifying ...etc )

in PHP "Require" would do the trick neatly ... so is there is
something I'm missing here or the whole technique is bad in which case
what do you suggest ?

Thanks,

Regards,
Mohamed Yousef
Aug 24 '08 #1
3 1800
On Aug 23, 7:27*pm, "Mohamed Yousef" <harrr...@gmail.comwrote:
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 )
You could import sys and look at sys.modules
why am i doing this in the first place I'm in the process of a medium
project where imports of modules start to make a jungle and i wanted
all needed imports to be in a single file (namely __init__.py) and
then all imports are made once and other modules feel it
This doesn't sound like a good idea. If A imports a module which is
automatically imported into B's namespace, that sounds like a
maintenance nightmare.
>
another reason to do this that my project is offering 2 interfaces
(Console and GUI through Qt) and i needed a general state class (
whether i'm in Console or GUI mode) to be available for all , for
determining state and some public functions ,and just injecting
Imports everywhere seems a bad technique in many ways (debugging ,
modifying ...etc )
I still don't understand.
in PHP "Require" would do the trick neatly ... so is there is
something I'm missing here or the whole technique is bad in which case
what do you suggest ?
I don't know what to suggest, in that you haven't yet stated anything
that appears to be a problem with how Python works. If two different
modules import the same third module, there is no big performance
penalty. The initialization code for the third module is only
executed on the first import, and the cost of having the import
statement find the already imported module is trivial.
Aug 24 '08 #2
On Sun, Aug 24, 2008 at 5:54 AM, Patrick Maupin <pm*****@gmail.comwrote:
On Aug 23, 7:27 pm, "Mohamed Yousef" <harrr...@gmail.comwrote:
>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 )

You could import sys and look at sys.modules
no even if you imported sys or used dir you will see no re in W() from
A . and you can even try to use re and will see
>why am i doing this in the first place I'm in the process of a medium
project where imports of modules start to make a jungle and i wanted
all needed imports to be in a single file (namely __init__.py) and
then all imports are made once and other modules feel it

This doesn't sound like a good idea. If A imports a module which is
automatically imported into B's namespace, that sounds like a
maintenance nightmare.
why ?
this saves time and consices whole package imports in one file
and maintaining it is easier (eg. you will never have circuular imports)
>>
another reason to do this that my project is offering 2 interfaces
(Console and GUI through Qt) and i needed a general state class (
whether i'm in Console or GUI mode) to be available for all , for
determining state and some public functions ,and just injecting
Imports everywhere seems a bad technique in many ways (debugging ,
modifying ...etc )

I still don't understand.
put it another way a general variable in all modules
and some functions visible in all modules
>in PHP "Require" would do the trick neatly ... so is there is
something I'm missing here or the whole technique is bad in which case
what do you suggest ?

I don't know what to suggest, in that you haven't yet stated anything
that appears to be a problem with how Python works. If two different
modules import the same third module, there is no big performance
penalty. The initialization code for the third module is only
executed on the first import, and the cost of having the import
statement find the already imported module is trivial.
it's not performance it's the jungle resulting
many imports in every file and suppose i changed the module used in
all modules name
guess what , it will have to be renamed in every import to it in all modules
Aug 24 '08 #3
Le Sunday 24 August 2008 12:11:03 Mohamed Yousef, vous avez écrit*:
On Sun, Aug 24, 2008 at 5:54 AM, Patrick Maupin <pm*****@gmail.comwrote:
On Aug 23, 7:27 pm, "Mohamed Yousef" <harrr...@gmail.comwrote:
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 )
You could import sys and look at sys.modules

no even if you imported sys or used dir you will see no re in W() from
A . and you can even try to use re and will see
why am i doing this in the first place I'm in the process of a medium
project where imports of modules start to make a jungle and i wanted
all needed imports to be in a single file (namely __init__.py) and
then all imports are made once and other modules feel it
This doesn't sound like a good idea. If A imports a module which is
automatically imported into B's namespace, that sounds like a
maintenance nightmare.

why ?
this saves time and consices whole package imports in one file
and maintaining it is easier
Maintaining is easier ? Not at all, refactoring maybe but it's not a big deal,
you must be convinced that module names are exactly as APIs, they are not
intended to change between versions.

BTW you could do that by importing all you need in one module, say libs.py,
and from libs import * in all you r package modules.
But htis considered a very bad practice by python developpers as PEP 8 states,
it break on of the zen's rule : "explicit is better than implicit".

Really, it is very good for reading and *maintaining* purpose to have all the
names used in one's module be easily found in the few import statments at the
top of the file.

In case you really need to make available a name to an arbitrary piece of code
(not a common scheme though), you could easily add it to the __builtin__
module, but if you know exactly what you do.
(eg. you will never have circuular imports)
No, circular imports are another problem that wouldn't be resolved by your
suggestion.
another reason to do this that my project is offering 2 interfaces
(Console and GUI through Qt) and i needed a general state class (
whether i'm in Console or GUI mode) to be available for all , for
determining state and some public functions ,and just injecting
Imports everywhere seems a bad technique in many ways (debugging ,
modifying ...etc )
I still don't understand.

put it another way a general variable in all modules
and some functions visible in all modules
This has no use case in python, even builtin names are overriden by the simple
affectation stamtent, if you want to modify a global name, you need access it
as an attribute of a specific namespace.

glob.py:

A=0
B=0

script.py:

import glob
from glob import A

A=1
glob.B =1
print A, B, glob.A, glob.B

will print 1, 1, 0, 1.
in PHP "Require" would do the trick neatly ... so is there is
something I'm missing here or the whole technique is bad in which case
what do you suggest ?
I don't know what to suggest, in that you haven't yet stated anything
that appears to be a problem with how Python works. If two different
modules import the same third module, there is no big performance
penalty. The initialization code for the third module is only
executed on the first import, and the cost of having the import
statement find the already imported module is trivial.

it's not performance it's the jungle resulting
many imports in every file and suppose i changed the module used in
all modules name
guess what , it will have to be renamed in every import to it in all
modules --
(cf my previous comment).

--
_____________

Maric Michaud
Aug 24 '08 #4

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

Similar topics

16
by: max(01)* | last post by:
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*...
2
by: Matthias Kramm | last post by:
Hi All, I'm having a little bit of trouble using the "imp" module to dynamically import modules. It seems that somehow cyclic references of modules don't work. I'm unable to get the following...
4
by: Martin Blais | last post by:
Hi I'm a tad confused over a problem involving cycles between packages. Assume the following sets of files:: driver.py a/__init__.py a/alice.py
6
by: Ritesh Raj Sarraf | last post by:
Hi, I've been very confused about why this doesn't work. I mean I don't see any reason why this has been made not to work. class Log: def __init__(self, verbose, lock = None): if verbose...
5
by: marcroy.olsen | last post by:
Hi list and python gurus :-) I'm playing with some mod_python and web development. And in me code I need to do som dynamic imports. Right now I just do a: exec 'import '+some_modulename ...
0
by: Kay Schluehr | last post by:
Since their introduction in Python 2.5 I only reviewed the new "relative import" notation briefly by reading the "What's new in Python 2.5" article. Now I wanted checkout if I get comfortable with...
0
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...
0
by: Terry Reedy | last post by:
Mohamed Yousef wrote: If you want to use module A in both B and C, B and C should both import A. No problem. ??
0
by: Gabriel Genellina | last post by:
En Sun, 24 Aug 2008 07:34:41 -0300, Mohamed Yousef <harrrrpo@gmail.comescribió: Yes. That way, when you see a name "foo" used in a module, you can look at the imports to see where it comes from....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.