Running test01.py under Windows (basic level)

K

K Viltersten

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.)
 
G

Gabriel Genellina

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/BeginnersGuide - 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.diveintopython.org (somewhat old now, but most of the book is
still aplicable; it just misses the new features in the language)
 
K

K Viltersten

I have v2.5.2 installed and i've composed
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)?

Not yet. I started off using some small things.
I tend to learn by doing. Or rather making. A
lot of errors, that is. :)
You may benefit from the Dive into Python
http://www.diveintopython.org

I'll do that. Thank you.
 
K

K Viltersten

def bloppA ():
go to File -> Open, open your saved file,
and use the Run menu (or press F5).

When i try that i get this.

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

Gabriel Genellina

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-51bd6f7abd3f1033.mspx

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.
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).
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.
 
G

Gabriel Genellina

When i try that i get this.


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

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

bloppA()

and try again.
 
D

Dennis Lee Bieber

When i try that i get this.


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
(e-mail address removed) (e-mail address removed)
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: (e-mail address removed))
HTTP://www.bestiaria.com/
 
K

K Viltersten

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-51bd6f7abd3f1033.mspx

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!
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!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,430
Messages
2,571,676
Members
48,796
Latest member
Greg L.

Latest Threads

Top