confusion about package/module imports

2008-01-01 Thread Jugdish
Why doesn't the following work?

>>> ls $HOME
$HOME/pkg/__init__.py
$HOME/pkg/subpkg/__init__.py
$HOME/pkg/subpkg/a.py
$HOME/pkg/subpkg/b.py

>>> cat $HOME/pkg/__init__.py
# empty

>>> cat $HOME/pkg/subpkg/__init__.py
import a
import b

>>> cat $HOME/pkg/subpkg/a.py
class A:
pass

>>> cat $HOME/pkg/subpkg/b.py
import pkg.subpkg.a
class B(pkg.subpkg.a.A):
pass

>>> setenv PYTHONPATH $HOME:$PYTHONPATH
>>> python $HOME/pkg/subpkg/b.py
Traceback (most recent call last):
  File "pkg/subpkg/b.py", line 1, in ?
import pkg.subpkg.a
  File "$HOME/pkg/subpkg/__init__.py", line 2, in ?
import b
  File "$HOME/pkg/subpkg/b.py", line 2, in ?
class B(pkg.subpkg.a.A):
AttributeError: 'module' object has no attribute 'subpkg'
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: confusion about package/module imports

2008-01-01 Thread Jugdish
Thanks very much for your helpful response!

> You'll see that b is executed (making module __main__),
> (1) it imports pkg.subpkg.a,
> (2)   which is accomplished by importing pkg (successfully),
> (3)then by importing pkg.subpkg
> (4)  which imports pkg.subpkg.a (successfully)
> (5)   and then imports pkg.subpkg.b
> (6)   which then attempts to import pkg.subpkg.a

What I'm not really understanding here is why this fails at lines (5)
and (6). If pkg.subpkg.a has already been successfully imported at
line (4), then (6) should be detected as a duplicate import and just
be skipped, right? So the import at line (5) should succeed.
-- 
http://mail.python.org/mailman/listinfo/python-list


raw_input can't handle pound sign?

2008-12-26 Thread Jugdish
Hi, I'm having problems getting a pound sign to go through as input
sent to the raw_input() command. I'm running Python 2.5.1 on Windows
XP. Here's my simple little script:

while True:
response = raw_input("Please enter a file name: ")
if os.path.exists(response):
break

Problem is if the filename has a "#" in it, the script interprets that
as the beginning of a comment (not sure why -- isn't raw_input
supposed to treat the user's input as raw text and not do any sort of
evals?)

Any ideas how to get a # to go through? Thanks!
--
http://mail.python.org/mailman/listinfo/python-list


Re: raw_input can't handle pound sign?

2008-12-26 Thread Jugdish
Ahh ok, tried out your example and it works just fine. Turns out the
actual problem is what I was doing with the input. Elsewhere, I call
urlparse.urlparse() on the filename past in, which splits up the
filename where the # sign is, so that's why it looked to me like the
characters after the # were getting stripped away.

Thanks for your help, in the future I'll try to do a bit more
debugging of my own scripts before bringing my problems here! :)

On Dec 26, 9:00 pm, Steven D'Aprano  wrote:
> On Fri, 26 Dec 2008 20:20:16 -0800, Jugdish wrote:
> > Hi, I'm having problems getting a pound sign to go through as input sent
> > to the raw_input() command. I'm running Python 2.5.1 on Windows XP.
> > Here's my simple little script:
>
> > while True:
> >     response = raw_input("Please enter a file name: ") if
> >     os.path.exists(response):
> >         break
>
> > Problem is if the filename has a "#" in it, the script interprets that
> > as the beginning of a comment (not sure why -- isn't raw_input supposed
> > to treat the user's input as raw text and not do any sort of evals?)
>
> Yes it is. What makes you think it is being interpreted as a comment?
> What results are you getting?
>
> > Any ideas how to get a # to go through? Thanks!
>
> Works for me (although I'm not using Windows XP).
>
> Can you execute this line at the interactive interpreter?
>
> print raw_input("Type something with a hash sign: ")
>
> At the prompt, type "test # string" (without the quotes) and show us what
> result you get. When I do this, I get the following:
>
> >>> print raw_input("Type something with a hash sign: ")
>
> Type something with a hash sign: test # string
> test # string
>
> --
> Steven

--
http://mail.python.org/mailman/listinfo/python-list