[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

Running test01.py under Windows (basic level

K Viltersten

2/28/2008 11:06:00 PM

I have v2.5.2 installed and i've composed
a source code i'm sure everybody will be
impressed by. It goes like this.

def bloppA ():
print "a very advanced piece of code"

What i get to work is to make it run from
the the snakes shell. Then, i realised
that such a masterpiece needs storing in
a file. So i saved it in a file called
great.py but when i executed:

exec "c:\loj\python\great.py"

i got errors and the pointer showed the
colon claiming it's invalid syntax.

Of course, everybody will agree it's
right syntax and that the computer is
stupid. But let's pretend it has won and
try to make it happy. How?

(Background: I'm a programmer since a few
years back but it's mostly Java/C/C++/C#
and Python way is very new to me.)

--
Regards
Konrad Viltersten
--------------------------------
sleep - a substitute for coffee for the poor
ambition - lack of sense to be lazy

8 Answers

Gabriel Genellina

2/29/2008 12:08:00 AM

0

En Thu, 28 Feb 2008 21:05:45 -0200, K Viltersten <tmp1@viltersten.com>
escribió:

> I have v2.5.2 installed and i've composed
> a source code i'm sure everybody will be
> impressed by. It goes like this.
>
> def bloppA ():
> print "a very advanced piece of code"
>
> What i get to work is to make it run from
> the the snakes shell. Then, i realised
> that such a masterpiece needs storing in
> a file. So i saved it in a file called
> great.py but when i executed:

To execute the file, use:

python great.py

from the system prompt (cmd).

Or, if you are using IDLE (the snake's shell?), go to File -> Open, open
your saved file, and use the Run menu (or press F5).

> exec "c:\loj\python\great.py"

That's the way to exec another script from inside the current one; it's
seldom used. Beware of \ as it's the escape character (as in C), so you
have to use "c:\\loj\\python\\great.py" or r"c:\loj\python\great.py" (the
latter being a "raw string").

See the wiki http://wiki.python.org/moin/Begi... - have you worked
out the Tutorial?

> (Background: I'm a programmer since a few
> years back but it's mostly Java/C/C++/C#
> and Python way is very new to me.)

You may benefit from the Dive into Python book, available online at
http://www.diveinto... (somewhat old now, but most of the book is
still aplicable; it just misses the new features in the language)

--
Gabriel Genellina

K Viltersten

2/29/2008 4:31:00 AM

0

>> I have v2.5.2 installed and i've composed
>> a source code i'm sure everybody will be
>> impressed by. It goes like this.
>>
>> def bloppA ():
>> print "a very advanced piece of code"
>>
>> What i get to work is to make it run from
>> the the snakes shell. Then, i realised
>> that such a masterpiece needs storing in
>> a file. So i saved it in a file called
>> great.py but when i executed:
>
> python great.py
> from the system prompt (cmd).
> Or, if you are using IDLE ...
> File -> Open, open your saved file, and use
> the Run menu (or press F5).

There will be poking around with %PATH%, i can
tell. Never liked to do that under Windows.

> Beware of \ as it's the escape character, so
> you have to use "c:\\loj\\python\\great.py"
> or r"c:\loj\python\great.py"...

I've tried to add the extra backslashes (or "r"
attribute) but i still get the same error at
the colon. Should i understand that i made two
mistakes (the first being not using double "\"
and the second calling exec alltogether)?

> http://wiki.python.org/moin/Begi... -
> have you worked out the Tutorial?

Not yet. I started off using some small things.
I tend to learn by doing. Or rather making. A
lot of errors, that is. :)

>> (Background: I'm a programmer since a few
>> years back but it's mostly Java/C/C++/C#
>> and Python way is very new to me.)
>
> You may benefit from the Dive into Python
> http://www.diveinto...

I'll do that. Thank you.

--
Regards
Konrad Viltersten
--------------------------------
sleep - a substitute for coffee for the poor
ambition - lack of sense to be lazy

K Viltersten

2/29/2008 4:38:00 AM

0

>> def bloppA ():
>> print "a very advanced piece of code"
>
> go to File -> Open, open your saved file,
> and use the Run menu (or press F5).

When i try that i get this.

>>> ====== RESTART =======
>>>

And nothing more. Do i use wrong "print"?!

--
Regards
Konrad Viltersten
--------------------------------
sleep - a substitute for coffee for the poor
ambition - lack of sense to be lazy

Gabriel Genellina

2/29/2008 6:04:00 AM

0

En Fri, 29 Feb 2008 02:30:43 -0200, K Viltersten <tmp1@viltersten.com>
escribió:

>>> I have v2.5.2 installed and i've composed
>>> a source code i'm sure everybody will be
>>> impressed by. It goes like this.
>>>
>>> def bloppA ():
>>> print "a very advanced piece of code"
>>>
>>> What i get to work is to make it run from
>>> the the snakes shell. Then, i realised
>>> that such a masterpiece needs storing in
>>> a file. So i saved it in a file called
>>> great.py but when i executed:
>>
>> python great.py
>> from the system prompt (cmd).
>> Or, if you are using IDLE ...
>> File -> Open, open your saved file, and use
>> the Run menu (or press F5).
>
> There will be poking around with %PATH%, i can
> tell. Never liked to do that under Windows.

No need to do that... Create an "alias.txt" file containing:
python=c:\path\to\your\python.exe $*

Execute (once, logged as administrator):
reg add "HKLM\SOFTWARE\Microsoft\Command Processor" /v AutoRun /t REG_SZ
/d "doskey /macrofile=path\to\your\alias.txt"

Open a new cmd console. Typing python is enough to invoke the interpreter
- no need to modify PATH.

Documentation for the DOSKEY command:
http://technet2.microsoft.com/WindowsServer/en/library/f7f45601-5178-48c6-9219-51bd6f7abd3...

If you don't like the above recipe, create a "python.cmd" file containing:
@c:\path\to\your\python.exe %*
and save it somewhere in your PATH.

>> Beware of \ as it's the escape character, so
>> you have to use "c:\\loj\\python\\great.py"
>> or r"c:\loj\python\great.py"...
>
> I've tried to add the extra backslashes (or "r"
> attribute) but i still get the same error at
> the colon. Should i understand that i made two
> mistakes (the first being not using double "\"
> and the second calling exec alltogether)?

exec is used to execute a string containing python code, not a file name.
execfile does what you want, but don't use it. Either execute your program
with `python name.py`, or load it into IDLE and run it with F5, or learn
how to make your favorite IDE/text editor Python-aware (if supported).

>> have you worked out the Tutorial?
>
> Not yet. I started off using some small things.
> I tend to learn by doing. Or rather making. A
> lot of errors, that is. :)

At least overview it. Python syntax is very clear and legible, so probably
you can figure yourself a lot of things, but there are some important
topics that you have to know and are explained in the Tutorial. It isn't
very long.

--
Gabriel Genellina

Gabriel Genellina

2/29/2008 6:08:00 AM

0

En Fri, 29 Feb 2008 02:38:04 -0200, K Viltersten <tmp1@viltersten.com>
escribió:

>>> def bloppA ():
>>> print "a very advanced piece of code"
>>
>> go to File -> Open, open your saved file,
>> and use the Run menu (or press F5).
>
> When i try that i get this.
>
>>>> ====== RESTART =======
>>>>
>
> And nothing more. Do i use wrong "print"?!

You *defined* a function, but aren't *executing* it. Append a line:

bloppA()

and try again.

--
Gabriel Genellina

Dennis Lee Bieber

2/29/2008 6:55:00 AM

0

On Fri, 29 Feb 2008 05:38:04 +0100, "K Viltersten" <tmp1@viltersten.com>
declaimed the following in comp.lang.python:

> >> def bloppA ():
> >> print "a very advanced piece of code"
> >
> > go to File -> Open, open your saved file,
> > and use the Run menu (or press F5).
>
> When i try that i get this.
>
> >>> ====== RESTART =======
> >>>
>
> And nothing more. Do i use wrong "print"?!

Uh... If the above is the ENTIRE file, it probably ran properly...
You define a function that contains a print statement, but you never
/call/ that function.
--
Wulfraed Dennis Lee Bieber KD6MOG
wlfraed@ix.netcom.com wulfraed@bestiaria.com
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: web-asst@bestiaria.com)
HTTP://www.bestiaria.com/

K Viltersten

3/1/2008 1:36:00 PM

0

<snip>
>> There will be poking around with %PATH%, i can
>> tell. Never liked to do that under Windows.
>
> No need to do that... Create an "alias.txt" file containing:
> python=c:\path\to\your\python.exe $*
> Execute (once, logged as administrator):
> reg add "HKLM\SOFTWARE\Microsoft\Command Processor"
> /v AutoRun /t REG_SZ /d
> "doskey /macrofile=path\to\your\alias.txt"
> Open a new cmd console. Typing python is enough to invoke the interpreter.
> Documentation for the DOSKEY command:
> http://technet2.microsoft.com/WindowsServer/en/library/f7f45601-5178-48c6-9219-51bd6f7abd3...
>
> If you don't like the above recipe, create a "python.cmd" file containing:
> @c:\path\to\your\python.exe %*
> and save it somewhere in your PATH.

It worked. Thanks!

>>> have you worked out the Tutorial?
>>
>> Not yet. I started off using some small things.
>> I tend to learn by doing. Or rather making. A
>> lot of errors, that is. :)
>
> At least overview it. Python syntax is very clear and legible, so probably
> you can figure yourself a lot of things, but there are some important
> topics that you have to know and are explained in the Tutorial. It isn't
> very long.

Naa, reading tutorials is for idiots... You can
answer my questions instead. It's not like you've
got anything better to do. I bet you've read the
tutorial, haven't you?

(a period of awkward silence...)

(a short while of WTF?!)

Oh, ah! This guy was joking. Pfew...

Yes, i was definitely joking here. :)
I do intend to go through the tutorial and i do
deeply appreciate all the help i've received/ i'll
receive. If all goes the way i hope, i'll be at a
new project soon and it's written in Python. Great
opportunity to learn it, right?

By the way - thanks!

--
Regards
Konrad Viltersten
--------------------------------
sleep - a substitute for coffee for the poor
ambition - lack of sense to be lazy

K Viltersten

3/1/2008 1:37:00 PM

0

>>>> def bloppA ():
>>>> print "a very advanced piece of code"
>>>
>>> go to File -> Open, open your saved file,
>>> and use the Run menu (or press F5).
>>
>> When i try that i get this.
>>
>>>>> ====== RESTART =======
>>>>>
>>
>> And nothing more. Do i use wrong "print"?!
>
> You *defined* a function, but aren't *executing*
> it. Append a line:
>
> bloppA()
>
> and try again.


Rookie mistake. Thank you.

--
Regards
Konrad Viltersten
--------------------------------
sleep - a substitute for coffee for the poor
ambition - lack of sense to be lazy