[Tutor] Running Files with Command Lines

Dave Angel d at davea.name
Sun Feb 12 18:40:24 CET 2012

On 02/12/2012 11:57 AM, Yony Torres wrote:
> 1. i tried this morning and it worked in the CMD check it out:
> copied and pasted from the CMD
> C:\Users\myusername>cd documents
> C:\Users\myusername\Documents>cd varios2
> C:\Users\myusername\Documents\varios2>cd pythonjourney
> C:\Users\myusername\Documents\varios2\pythonjourney>script.py<------- i did not write the name the file properly, my mistake!'script.py' is not recognized as an internal or external command,operable program or batch file.
> C:\Users\myusername\Documents\varios2\pythonjourney>script1.py<----- now i did it properlywin321267650600228229401496703205376spam!spam!spam!spam!spam!spam!spam!spam!
> --- i must say that previously i added C:\python32 to the environment path ;)
> yay!
> now im trying it from the python command line, with no success yet :(

Still some things you haven't caught onto yet.

1) please don't top-post

2) Please do use copy/paste, rather than laboriously (and inaccurately) 
retyping what you did & saw

3) Use more definite wording than

with no success yet

4) the syntax inside the python interpreter is different than in a 
Windows cmd prompt.  Once you get that prompt, you can type simple 
expressions like  3*4, or you can define and execute functions.  But if 
you want to "run" another script, that script needs to  be a valid module,
  and you import it with the   "import" statement.

So if you want to start python, and then run your script/module from the 
python prompt, you'd do something like:

davea at think:~/temppython$ python
Python 2.7.1+ (r271:86832, Apr 11 2011, 18:13:53)
[GCC 4.5.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
 >>> import myscript
Traceback (most recent call last):
   File "<stdin>", line 1, in <module>
   File "myscript.py", line 16
     assert w = -1
SyntaxError: invalid syntax

Note that a number of things are significant, and different than the 
command line:

1) the script must be in the current directory, or it must be in the 
sys.path string, which you probably haven't learned about yet.
2) you import the script by its module name, which does not include the 
.py   In fact, other extensions are possible and common.
3) when the module exits to the python prompt, any variables it defined 
are in the module's space, not the "global" space.  So if you defined a 
variable w, you'd reference it by myscript.w



More information about the Tutor mailing list