473,573 Members | 2,840 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Relative-importing *

Hi, I'm a newbie at Python. :) Right now it's not letting me import *
from any relative package name--i.e., a name that starts with a dot.
For instance, none of the following work:

from . import *
from .sibiling import *
from .. import *
from ..parent_siblin g import *

....and so on. The same error occurs:
SyntaxError: 'import *' not allowed with 'from .'

(I have Python 2.5.1.)

Why would it not let me import * from a relative module at all? I read
that they're planning to make absolute imports the default, so I'd
think that this sort of thing would become more common in the future.

Thanks in advance! :)

Jul 27 '07 #1
9 2159
Ben Finney <bi************ ****@benfinney. id.auwrote in
news:87******** ****@benfinney. id.au:
rb**********@gm ail.com writes:
> from . import *
from .sibiling import *
from .. import *
from ..parent_siblin g import *

...and so on. The same error occurs:
SyntaxError: 'import *' not allowed with 'from .'

Interesting. I know that 'from foo import *' is frowned on and is
generally worse than importing names explicitly, but I wasn't aware
that it was officialy deprecated. The PEP introducing absolute and
relative imports <URL:http://www.python.org/dev/peps/pep-0328/>
doesn't mention it, except as a friendly "import * is *not* an option
:-)" aside.
Well, I may be looking it from the wrong perspective but what is the
meaning of :

from . import *
from .. import **

Import all modules available at that relative path?

I am not sure why the other 2 versions are not working, though.

../alex
--
..w( the_mindstorm )p.

Jul 27 '07 #2
Steven D'Aprano <st***@REMOVE.T HIS.cybersource .com.auwrites:
I read "from module import *" as explicitly saying "clobber the current
namespace with whatever names module exports". That's what from does: it
imports names into the current namespace. It isn't some sort of easy to
miss side-effect. If a name already existed, it gets clobbered, just like
any other import:
Seems to me that there should be a compiler warning when this happens.
Jul 28 '07 #3
Paul Rubin <http://ph****@NOSPAM.i nvalidwrites:
Steven D'Aprano <st***@REMOVE.T HIS.cybersource .com.auwrites:
I read "from module import *" as explicitly saying "clobber the
current namespace with whatever names module exports". That's what
from does: it imports names into the current namespace. It isn't
some sort of easy to miss side-effect. If a name already existed,
it gets clobbered, just like any other import:

Seems to me that there should be a compiler warning when this
happens.
The import happens at run-time. The compiler can't catch it.

--
\ "I stayed up all night playing poker with tarot cards. I got a |
`\ full house and four people died." -- Steven Wright |
_o__) |
Ben Finney
Jul 28 '07 #4
Steven D'Aprano a écrit :
(snip)
I do take your point that importing * has the potential to unexpectedly
clobber names you didn't intend,
Another problem is that it makes harder to know from which module a name
comes from.
and that's a good reason to avoid it
unless you have a good reason to use it. But there are good reasons.

The "from module import *" statement is very useful if you wish to export
all the public objects from a "private" module, e.g. emulate what the os
module does with platform specific functions.
Indeed.

(snip)
I certainly agree that "from module import *" is easily abused, but to go
from there to the conclusion that it is a blight that must be stamped out
is far too strong.
As with any "you shall not..." programming rule (goto's, multiple return
points, public attributes, etc, etc), it stands as long as you don't
fully understand when and why it's ok to do it.

Jul 31 '07 #5
Paul Rubin a écrit :
Steven D'Aprano <st***@REMOVE.T HIS.cybersource .com.auwrites:
>>I read "from module import *" as explicitly saying "clobber the current
namespace with whatever names module exports". That's what from does: it
imports names into the current namespace. It isn't some sort of easy to
miss side-effect. If a name already existed, it gets clobbered, just like
any other import:


Seems to me that there should be a compiler warning when this happens.
if some_runtime_co ndition:
from foo import *

Paul, I do know why you think compile-time checks are a good thing, but
you do have to understand that Python is *highly* dynamic. Except for
syntax errors, don't expect much help from the Python's compiler.
Jul 31 '07 #6
rb**********@gm ail.com writes:
Yes, I'm importing * for a reason, a good one, I think.
Reading your description, I must say I don't see a good reason.
I have a set of modules (the number planned to reach about 400) that
would be dynamically loaded by my program as needed, and they're
somewhat similar to each other. I wish each of them to import * from
a certain "parent" module, so that they'll receive whatever
functions and variables I want all of them to share (using the
parent module's __all__), which may be overrided by the "child"
modules at their discretion. Sort of like class inheritance, but I'm
not doing that because implementing that would be a lot more tedious
and less elegant.
It seems to me, based only on this description, that class inheritance
would be far *more* elegant, and much easier to follow when reading
the code.

If all these functions and other objects are so closely-related that
they form the core of some inheritance-like system, what's so
inelegant about wrapping them in a class so that the inheritance is
explicit in the module where it happens?

--
\ "The only tyrant I accept in this world is the still voice |
`\ within." -- Mahatma Gandhi |
_o__) |
Ben Finney
Aug 4 '07 #7
rb*******@gmail .com writes:
(In addition, it probably would make the program somewhat slower to
have an internal class inside every module, and performance is
important to me, as I'm planning to use this project in a future
game.
This is known as "premature optimisation", and it's harmful. It's
folly to avoid a core part of the language because you're worried
about hypothetical performance issues.

Instead, write your program so that it's easy to understand and
maintain; then, if it's noticeably slow, *measure* the performance
using profiling so you know exactly what is slow; then, and only then,
work on speeding up the slow parts.
These modules are not going to have full-fledged object construction
and so forth. I don't want to construct and use objects at all
If that's truly what you desire, you're using the wrong
language. Everything in Python is an object, even if you don't use
object-oriented programming.
it seems like a waste of time and memory to me to inherit __new__,
__del__, and so on.
You've stated that you're a beginner to programming, so you probably
are unaware that this statement has no basis in how class inheritance
actually works. The point is that inherited behaviour is implemented
in *one* place, not that it's repeated everywhere.

Moreover, this is a further expression of your tendency for premature
optimisation. You should aim instead to write code naturally (learning
what's natural when you're a beginner), and only once you have
something that *works* should you think about performance issues.
All that I want these modules to do is to encapsulate varying
information and behavior for /other/ objects to use.)
This sounds like an ideal case for seeting up a class hierarchy for
this move behaviour.
But regardless of if it's the "right thing to do", do you all think
it's an unintended bug?
Do we think what is an unintended bug?

I think if you go out of your way to avoid the class inheritance
system for implementing what looks strongly like a hierarchy of
behaviour, you will have design bugs. Hopefully now that you're
reading this thread, they won't be *unintended* bugs; whether you
*intentionally* proceed with a buggy design is up to you :-)

--
\ "With Lisp or Forth, a master programmer has unlimited power |
`\ and expressiveness. With Python, even a regular guy can reach |
_o__) for the stars." -- Raymond Hettinger |
Ben Finney
Aug 6 '07 #8
On Tue, 07 Aug 2007 02:45:23 +0000, rbygscrsepda wrote:
Specifically, in Python 1.5, all of the following generate the error
below:
In Python *1.5*!? I somehow doubt that. ;-)
from . import *
from .sibiling_packa ge import *
from .. import *
from ..cousin_packag e import *

SyntaxError: 'import *' not allowed with 'from .'

Importing * is a feature, but this seems like an artificial
inconsistency to me. It's as if the compiler raises an error when it
tries to import * as soon as it sees "from .", regardless of what
comes after it. So is this a bug in python? And if it happens to be a
bug, how can I alert the developers of the python compiler?
I'm just guessing here but as * imports are considered bad style the
developers might took the opportunity to forbid them in relative imports
because relative imports are new and this doesn't break old programs.

Ciao,
Marc 'BlackJack' Rintsch
Aug 7 '07 #9
Marc 'BlackJack' Rintsch wrote:
On Tue, 07 Aug 2007 02:45:23 +0000, rbygscrsepda wrote:
>Specifically , in Python 1.5, all of the following generate the error
below:

In Python *1.5*!? I somehow doubt that. ;-)
> from . import *
from .sibiling_packa ge import *
from .. import *
from ..cousin_packag e import *

SyntaxError: 'import *' not allowed with 'from .'

Importing * is a feature, but this seems like an artificial
inconsistenc y to me. It's as if the compiler raises an error when it
tries to import * as soon as it sees "from .", regardless of what
comes after it. So is this a bug in python? And if it happens to be a
bug, how can I alert the developers of the python compiler?

I'm just guessing here but as * imports are considered bad style the
developers might took the opportunity to forbid them in relative imports
because relative imports are new and this doesn't break old programs.
Yup, I searched around to find a specific statement of the reason but
couldn't find anything definite. Of course, if you are writing a set of
plugins then wildcard imports would seem like a train wreck waiting to
happen in terms of the discipline required to avoid name collisions.

regards
Steve
--
Steve Holden +1 571 484 6266 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://del.icio.us/steve.holden
--------------- Asciimercial ------------------
Get on the web: Blog, lens and tag the Internet
Many services currently offer free registration
----------- Thank You for Reading -------------

Aug 7 '07 #10

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

Similar topics

5
4100
by: jason | last post by:
Can anyone help me find a solution to quickly working out relative paths to a folder in the root of my server... Although it easy when you go - say - two levels down: .../includes it becomes increasingly difficult to ball park the folder when you go deeper and wider down:
2
12013
by: Catherine Lynn Wood | last post by:
I need to know how to overlap DIV content within 'relative' associated rendering. I am building div layers in the middle of a page and when I set positioning to absolute in the CSS, it references back to 0,0 on the entire page. If I set it to relative, the div layers will not overlap as needed. I prefer to avoid javascripting an 'innerHTML'...
3
19757
by: Markus Ernst | last post by:
Hello Reading the follwing document: http://www.w3.org/TR/WD-positioning-970131#In-flow it seems very clear that position:relative should be relative to the parent element. So in the following test case element1 and element2 should be placed side by side inside a centered white container element: http://www.markusernst.ch/test.htm
6
11757
by: openleren | last post by:
Hi all, how can I use a relative path in my web.config file for an Access db?: Instead of using <configuration> <appSettings> <add key="conAccess" value="microsoft.jet.oledb.4.0;data source=c:/Inetpub/MyApp/data/database.mdb" /> ...
4
4059
by: dropdeadster | last post by:
Trying to line up a tic-tac-toe board type grid of images using style= tags to <img inside a table TD but it's not working, I get more like a set of steps, can I get an explanation of what's wrong with this and how to do it correctly?: <table border="1"> <tr> <td valign="top" align="left">
8
2595
by: JJ | last post by:
I'm confused about paths. I have a functionn that uses the mappath method, which I think requires a virtual path (is that the same as a relative path?). But this doesn't always work as the path can take the form of the following: 1. /directory/images/xyz.gif (works fine) 2....
12
2717
by: Alan Isaac | last post by:
Are relative imports broken in 2.5? Directory ``temp`` contains:: __init__.py test1.py test2.py File contents: __init__.py and test2.py are empty test1.py contains a single line::
15
6442
by: Lars Eighner | last post by:
Aside from the deaths of a few extra electrons to spell out the whole root relative path, is there any down side? It seems to me that theoretically it shouldn't make any difference, and it would make it much easier to slap modualar blocks of markup into page frameworks, which may change and so forth. And the few extra bytes, which even for a...
0
1383
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 them. Opening the tutorial I found following notice ( 6.4.2 ): "Note that both explicit and implicit relative imports are based on the name of...
0
1479
by: Gabriel Genellina | last post by:
En Sat, 18 Oct 2008 05:52:04 -0300, Stef Mientki <stef.mientki@gmail.com> escribió: Why don't you let the caller tell you its own location, using __file__? The above code is too much magic for me. Yes.
0
7760
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...
0
7996
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. ...
0
8049
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...
1
5573
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...
0
5284
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...
0
3724
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...
0
3723
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2185
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
1
1289
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.