472,958 Members | 2,745 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,958 software developers and data experts.

access to submodules

TG
hi.

This is my first try on modules.

I've got :

tom/
__init__.py
core.py
ui.py
data.py

then, when I'm in my ipython shell :

?from tom import *
this works, it loads core, ui and data
but when I do this :
?import tom

?tom.core
AttributeError: 'module' object has no attribute 'core'
Well, i guess I missed something, but I don't see what ...

Jul 19 '06 #1
9 2131
TG
I've just found this :

If I add :

"import core, data, ui" inside my "tom/__init__.py" file, it will
work. But this line does not seems to exist in other files (after
having a look at several files inside /usr/lib/python2.4).

Jul 19 '06 #2
from tom import *

You CAN do this, but it's a bad habit.

Try:
>>import tom
Then call by tom.function()

rd

Jul 19 '06 #3
TG
I know this is a bad habit ... I was just doing it to show what is
disturbing me.

Obviously the "star" syntax finds the submodules because they are
loaded, but when I properly load the module alone with "import tom",
the "dot" syntax does not find "tom.core".

BartlebyScrivener wrote:
from tom import *

You CAN do this, but it's a bad habit.

Try:
>import tom

Then call by tom.function()

rd
Jul 19 '06 #4
http://tinyurl.com/6yz2g

If you do

from tom import *

then you no longer need tom, you imported all of his FUNCTIONS (never
heard of submodule).

rd

Jul 19 '06 #5
TG
BartlebyScrivener wrote:
then you no longer need tom, you imported all of his FUNCTIONS (never
heard of submodule).
my mistake, I was using the wrong name

tom/ <-- package
__init__.py
core.py <
data.py < these are modules contained in tom/
ui.py <
if I import tom, it is supposed to load functions defined in
tom/__init__.py and make all the modules inside accessible through the
"dot" syntax.

Therefore, this is supposed to work :

?import tom
?help(tom.core)

AttributeError: 'module' object has no attribute 'core'

But if I use the baaaaad star syntax

?from tom import *
?help(core)

this will work. So that means star loaded functions of __init__.py AND
modules contained, whereas dot syntax does not give me access to
modules inside. The issue is not about using or not using the import *
....

Jul 19 '06 #6
TG wrote:
if I import tom, it is supposed to load functions defined in
tom/__init__.py and make all the modules inside accessible through the
"dot" syntax.

Therefore, this is supposed to work :

?import tom
?help(tom.core)

AttributeError: 'module' object has no attribute 'core'

But if I use the baaaaad star syntax

?from tom import *
?help(core)

this will work.
No, it won't. Try again with a fresh interpreter (no prior imports)
>>from tom import *
core
Traceback (most recent call last):
File "<stdin>", line 1, in ?
NameError: name 'core' is not defined
>>import tom
tom.core
Traceback (most recent call last):
File "<stdin>", line 1, in ?
AttributeError: 'module' object has no attribute 'core'

Only after an explicit import of tom.core...
>>from tom import core
del core
from tom import *
core
<module 'tom.core' from 'tom/core.py'>

is core added as an attribute to tom and will therefore be copied into the
namespace of a module doing a star-import.

Peter
Jul 19 '06 #7
TG
okay,

so only when I have inside __init__.py

__all__ = ["core"]

this works

?from tom import *
?help(core)

but (in a brand new interpretor)

?import tom
?help(tom.core)

AttributeError: 'module' object has no attribute 'core'

got it. But ...

?import numpy
?help(numpy.core)

this will work, even if core is a subpackage of numpy. and i don't have
to explicitly import numpy.core.

I must be missing something ... :-/

Jul 19 '06 #8
TG wrote:
okay,

so only when I have inside __init__.py

__all__ = ["core"]

this works

?from tom import *
?help(core)

but (in a brand new interpretor)

?import tom
?help(tom.core)

AttributeError: 'module' object has no attribute 'core'

got it. But ...

?import numpy
?help(numpy.core)

this will work, even if core is a subpackage of numpy. and i don't have
to explicitly import numpy.core.

I must be missing something ... :-/
numpy does the equivalent of

import core

in its __init__.py

"""
Help on method __call__ in module numpy._import_tools:

__call__(self, *packages, **options) unbound
numpy._import_tools.PackageLoader method
Load one or more packages into parent package top-level namespace.

Usage:

This function is intended to shorten the need to import many of
subpackages, say of scipy, constantly with statements such as

import scipy.linalg, scipy.fftpack, scipy.etc...

Instead, you can say:

import scipy
scipy.pkgload('linalg','fftpack',...)
"""

Peter
Jul 19 '06 #9
TG
okay, thanks everyone. this is much clearer now.

Jul 20 '06 #10

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

Similar topics

35
by: Gabriel Zachmann | last post by:
Is there any generic way to use C++ libraries from within Python. I seem to recall that there are tools to generate wrappers for C-libraries semi-automatically. But those were still way too...
63
by: Jerome | last post by:
Hi, I'm a bit confused ... when would I rather write an database application using MS Access and Visual Basic and when (and why) would I rather write it using Visual Studio .Net? Is it as easy...
13
by: bill | last post by:
I am trying to convince a client that dotNet is preferable to an Access project (ADP/ADE). This client currently has a large, pure Access MDB solution with 30+ users, which needs to be upgraded....
1
by: Dave | last post by:
Hello NG, Regarding access-declarations and member using-declarations as used to change the access level of an inherited base member... Two things need to be considered when determining an...
13
by: Simon Bailey | last post by:
I am a newcomer to databases and am not sure which DBMS to use. I have a very simplified knowledge of databases overall. I would very much appreciate a (simplifed) message explaining the advantages...
0
by: Frederick Noronha \(FN\) | last post by:
---------- Forwarded message ---------- Solutions to Everyday User Interface and Programming Problems O'Reilly Releases "Access Cookbook, Second Edition" Sebastopol, CA--Neither reference book...
49
by: Yannick Turgeon | last post by:
Hello, We are in the process of examining our current main application. We have to do some major changes and, in the process, are questionning/validating the use of MS Access as front-end. The...
13
by: Robin Haswell | last post by:
Hey people I'm an experience PHP programmer who's been writing python for a couple of weeks now. I'm writing quite a large application which I've decided to break down in to lots of modules...
1
by: Frank Aune | last post by:
Hi, First take a look at: http://www.network-theory.co.uk/docs/pytut/Packages.html as I will use this module file layout as an example. If I want to do the following:
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...

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.