newbie question

P

Paul Metzger

I'm brand new to using/playing with Python, and I have what is likely a
very simple question but can't seem to figure it out.
Python is fun and it's easier to use a real OS to develop the programs,
then if need be, not that difficult to move them to windows:)
I wrote up a script in my preferred text editor. It contains maybe ten
lines of code. I want to be able to execute those code lines with a
single command either from the inline mode or from IDLE. How do I do
this? I saved the file (myscript.py) in a folder that I've specified
in my PYTHONPATH environment variable, and when I type

If you are going to use a text editor to do python with, may I suggest
gVim, or vim it will take care of ALOT of syntax errors dure to
indentation for you. Grant it I am a command line junky, but still...
the script runs. If, later during the same session, I type


all I get for output is

<module 'myscript' from 'c:\documents and settings\t_crane\my
documents\Python Modules\myscript.pyc'>

Somwhere in the beginning tutorial there's this line:

"The script can be given a executable mode, or permission, using the
chmod command:

$ chmod +x myscript.py"
Ok, the above seems to me like you are useing windows. If so, the chmod
commands are going to be just about worthless. If you install unix
services for windows, you will get some functionality, but not also, and
still what's the point. Linux is free and much more stable, secure,
etc...
Which I took to mean that if I enter that I would be able to do what I
wanted. But no, it just highlights 'myscript' in red and says it's a
syntax error.

What am I missing?
OK, excuseing my editorial comments above, here is what I see and
correct me if I'm wrong.
1) you use windows
2) You have not been around any OS other than windows
and I mean this as no insult, just making sure I understand you. The
answer to your questions are as follows. In Unix/Linux/XXix/Solaris and
most anyother unixy(yes that's a word, I just made it up) OS you can add
the first line of the script being something like
#!/usr/bin/python
or where ever you installed python to, then FROM A COMMAND LINE run
chmod <perms> <file name and path>
and it will make the script executable. What happens is when you tell
the script to run, it looks at the line and then knows where to pull the
inter. from. the # just comments it out so it don't try to run it with
the rest of the script, the ! tells the computer to pay attention to it.
As far as I know, you cannot do this in Windows. to run it in a single
command, you would have to either compile it with py2exe which will give
you the .exe and a dll or two or you can simple make a batch file that
would look something like
@ECHO OFF
<path to python>python <path to script>script.py
the @ECHO OFF is not needed, but I think it looks better and more
professional, but that is just me. I do realize I have rambled on for a
while here but I do hope it helps. I have been out of the python loop
for a while, but am being forced back into it, which really don't hurt
my feelings at all, just had to wait for a project that required it:)
Anyway if you dont understand anything just let me know and i'll try to
do a better job explaining it.

Paul
 
O

orangeDinosaur

Hi,

I'm brand new to using/playing with Python, and I have what is likely a
very simple question but can't seem to figure it out.

I wrote up a script in my preferred text editor. It contains maybe ten
lines of code. I want to be able to execute those code lines with a
single command either from the inline mode or from IDLE. How do I do
this? I saved the file (myscript.py) in a folder that I've specified
in my PYTHONPATH environment variable, and when I type

the script runs. If, later during the same session, I type

all I get for output is

<module 'myscript' from 'c:\documents and settings\t_crane\my
documents\Python Modules\myscript.pyc'>

Somwhere in the beginning tutorial there's this line:

"The script can be given a executable mode, or permission, using the
chmod command:

$ chmod +x myscript.py"

Which I took to mean that if I enter that I would be able to do what I
wanted. But no, it just highlights 'myscript' in red and says it's a
syntax error.

What am I missing?

thanks for your help!
 
S

Steve Juranich

orangeDinosaur said:
Hi,

I'm brand new to using/playing with Python, and I have what is likely a
very simple question but can't seem to figure it out.

I wrote up a script in my preferred text editor. It contains maybe ten
lines of code. I want to be able to execute those code lines with a
single command either from the inline mode or from IDLE. How do I do
this? I saved the file (myscript.py) in a folder that I've specified
in my PYTHONPATH environment variable, and when I type


the script runs. If, later during the same session, I type


all I get for output is

<module 'myscript' from 'c:\documents and settings\t_crane\my
documents\Python Modules\myscript.pyc'>

Somwhere in the beginning tutorial there's this line:

"The script can be given a executable mode, or permission, using the
chmod command:

$ chmod +x myscript.py"

This is a Unix-specific command (POSIX-specific, actually). It won't work
on a Windows box.

To run the script from a command line,

`python C:\path\to\script\myscript.py' should do the trick, I can't say for
sure, though because I'm a heathen Linux user.

HTH
 
D

D

Yep, that should work. Just keep in mind that if python.exe is not in
your path, you will need to either specify the entire path to it
(i.e. 'C:\python24\python C:\path\to\script\myscript.py'), or cd into
its directory first (i.e. if you want to just run 'python
C:\path\to\script\myscript.py').

Doug
 
J

John Zenger

orangeDinosaur said:
I wrote up a script in my preferred text editor. It contains maybe ten
lines of code. I want to be able to execute those code lines with a
single command either from the inline mode or from IDLE. How do I do
this? I saved the file (myscript.py) in a folder that I've specified
in my PYTHONPATH environment variable, and when I type

From the Python shell, you can use execfile to run a script:

This works regardless of your OS. The file must be in your Python path.
If it isn't, just specify the full path. Like:

While viewing your code with IDLE, hit F5 to execute it.
 
M

Michael Tobis

I think the answers so far are unnecessarily confusing and off the
mark.

Here is what I think you think you want to know:

1) import only works once. If you try import again, it will see the
module already exists, and will ignore you

2) the functionality you think you want is reload.will essentially reimport mymodule after the first time.

However, what you think you want is not what you want, which is why the
experienced people are giving misleading and overcomplicated answers.
Normally reload is a fairly advanced feature and beginners don't need
it.

Usually, an import statement invokes a module containing a bunch of
definitions (usually functions or classes, but sometimes even
constants), but it doesn't DO anything unless it is invoked as the main
program.

So after you satisfy yourself that "reload" does what you want, try to
think about how you would work things so you don't need it.

For instance, instead of something like

#mystuff.py

print "hello ",
print "world"

# end of file



is



### newstuff.py

def newstuff():
print "hello",
print " world"

# end of file


hello, world


hth
mt
 
D

Dennis Lee Bieber

Yep, that should work. Just keep in mind that if python.exe is not in
your path, you will need to either specify the entire path to it
(i.e. 'C:\python24\python C:\path\to\script\myscript.py'), or cd into
its directory first (i.e. if you want to just run 'python
C:\path\to\script\myscript.py').
Actually, on XP at least, some combination of commands enables
things such that one can run without the "python" or the ".py" (but I
think it garbages I/O redirection and maybe command line arguments --
though this one worked)

Directory of E:\UserData\Dennis Lee Bieber\My Documents\Python Progs

02/24/2006 11:17 AM <DIR> .
02/24/2006 11:17 AM <DIR> ..
12/11/2005 07:21 PM 372 make_ou_class.py
12/11/2005 07:21 PM 1,231 make_ou_class.pyc
11/30/2005 09:11 AM 106 run.py
02/23/2006 10:04 AM 767 Script1.py
02/19/2006 12:46 PM 1,213 Sociable_Chain.py
07/13/2005 07:46 PM 6,309 sudoku.py
12/11/2005 07:17 PM 60 tdriver.py
02/24/2006 09:49 AM 434 timetest.py
11/30/2005 09:11 AM 104 ut_00.py
9 File(s) 10,596 bytes
2 Dir(s) 307,370,438,656 bytes free

E:\UserData\Dennis Lee Bieber\My Documents\Python Progs>type run.py

import math

def run_ut(test):
print math
execfile(test)

print math
run_ut("ut_00.py")

E:\UserData\Dennis Lee Bieber\My Documents\Python Progs>run
<module 'math' (built-in)>
<module 'math' (built-in)>
in module: <module 'math' (built-in)>
in function: <module 'math' (built-in)>

E:\UserData\Dennis Lee Bieber\My Documents\Python Progs>run >t.dat

E:\UserData\Dennis Lee Bieber\My Documents\Python Progs>type t.dat
<module 'math' (built-in)>
<module 'math' (built-in)>
in module: <module 'math' (built-in)>
in function: <module 'math' (built-in)>

E:\UserData\Dennis Lee Bieber\My Documents\Python Progs>
--
 
O

orangeDinosaur

Thanks!!

This makes sense. And you were right about my misunderstanding.
 
T

Tim Roberts

Dennis Lee Bieber said:
Actually, on XP at least, some combination of commands enables
things such that one can run without the "python" or the ".py" (but I
think it garbages I/O redirection and maybe command line arguments --
though this one worked)

That's the PATHEXT environment variable.

C:\tmp>set PATHEXT
PATHEXT=.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.PY;.PYW;.tcl

If you give a lone file name without an extension, it will try all of those
extensions, in that order, to find an executable. Just add .PY to the end.

There is a bug in NT's CMD.EXE that screws up redirection of stdin, but
command line arguments and stdout work just fine.
 
P

P Boy

Since you are a Windows user, I strongly recommend that you install
activestate python instead of the one you download from python.org. The
PythonWin from active state is much more user friendly. It's basically
an IDE (integrated development environment), which includes interactive
shell, editor, debugger, online help. Just to name a few.

Good luck.
 

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

Similar Threads


Members online

Forum statistics

Threads
473,981
Messages
2,570,188
Members
46,733
Latest member
LonaMonzon

Latest Threads

Top