4

I'm trying to learn python but have some problem running source files from power shell. When I type 'python' it opens up and I can type python commands directly in the shell. I think this is called interactive mode. But when I try to run/execute a source file I get an error message: It sayys: Syntax error: invalid syntax.

I use 'python myfile.py' when I try to execute the script.

If I run the same file from IDLE it works just fine. Ana idea what I'm doing wrong?

Here is myfile.py. I'm running python 2.7

 # filename: myfile.py

while True:
      s = raw_input('Enter something: ')
      if s == 'Quit':
          break
      print 'Lenght of the string is', len(s)
print 'Done'
user1009453
  • 707
  • 2
  • 11
  • 28
  • Can you show us `myfile.py`, as well as what version of python you're using? – wkl Apr 25 '12 at 19:02
  • You can run the Python script directly as `myfile.py` in command line. – Fenikso Apr 25 '12 at 19:03
  • Now I've added code for myfile.py – user1009453 Apr 25 '12 at 19:11
  • It does not work when I just type myfile.py and hit enter. I get: file "", line 1, break.py (with an arrow pointing at the point before py. indicating that there's something wrong there) – user1009453 Apr 25 '12 at 19:14
  • Your script works for me - can you type in `python -V` in powershell and tell us what it is? – wkl Apr 25 '12 at 19:20
  • Ok, how strange, I get 'python 2.7.2' when I type 'python -V' – user1009453 Apr 25 '12 at 19:25
  • I'm running Python 2.7.3, which shouldn't be the problem. 2.7 is just the major-minor, the .2 at the end is patch version. However, your script as it it, works fine for me if I just copy and paste it into my text editor. – wkl Apr 25 '12 at 19:31
  • Okay, Is there a possibility that I haven't set the path right. Here's what I use: [Environment]::SetEnvironmentVariable("Path", "$env:Path;C:\Python27", "User") Looks okay? – user1009453 Apr 25 '12 at 19:35
  • Not familiar with powershell and environment variables. Looks sensible though. How does it run without the comment at the beginning of the file. It might be that there is some form of non-printable character in that line that is messing with the script somehow and posting it into SO has removed it. – Will Apr 25 '12 at 19:39
  • Path looks correct, Will's suggestion is probably the best shot. Here's a straight copy-paste from my editor, which should work without issue: http://pastebin.com/qAfAZ953 – wkl Apr 25 '12 at 19:41
  • No, I copied the script directly but It does'nt work. I get the same error as before: file "", line 1, Syntax error. Thanks for staying with me on this. Really don't know how to proceed here, though. Feels like I'm stuck! – user1009453 Apr 25 '12 at 19:53
  • have you tried just the while loop section copied and pasted into the interactive shell you got from `>python`. (you won't be able to copy and paste the `print 'Done'` from the end as well due to the way the interactive shell works) – Will Apr 25 '12 at 20:03
  • Another thought would be what encoding is your editor using? Python expects UTF-8 if I am not mistaken. If you are on windows there is every chance you have saved it as UTF-16. – Will Apr 25 '12 at 20:06
  • Okay, problem solved! I had to write the full path in the commnd line, eg: 'python C:\Python27\demo.py' But I have to execute the script from the command line. As soon as I enter Python (Type 'Pyhon' and then press Enter) and try to execute tge script with C:\Python27\demo.py I get an error. Don't know why this is, but at least I can run the scripts from the command line now. Tank's! – user1009453 Apr 25 '12 at 20:22

6 Answers6

3

You might have more than one version of Python installed and the version IDLE is using is newer. To see what version of python you have you can type >python -V at a command line. If that version looks appropriate then you might need the full path to the file as the second parameter. E.g >python C:\myfile.py.

If you installed Python correctly there is always a chance that just typing the name of the script will run it with python. E.g. >myfile.py

I always find that adding C:\Python27 to the %PATH% variable and .PY to the %PATHEXT% variable makes running scripts easier. In this case just >myfile should work.

Edit after Update:

Typing just >python with no parameters opens python in 'interactive mode' which is different from the batch or scripting mode that your script is intended for. If executed with arguments the first argument is taken as the file path and further arguments are passed to the script in the sys.argv list.

Will
  • 4,585
  • 1
  • 26
  • 48
  • If none of these work then there is always the chance you actually do have a syntax error in your python file. – Will Apr 25 '12 at 19:09
  • Here's what I use to set Path: [Environment]::SetEnvironmentVariable("Path", "$env:Path;C:\Python27", "User") Does this look okay? – user1009453 Apr 25 '12 at 19:30
2

You will need to put the full path of the Python executable within the command line in order for it to work. You could check and ensure that your python exe is included in your Path among your system variables.

Jay
  • 94
  • 7
1

Disclaimer: I don't know PowerShell, but I do know cmd.exe.

I don't know why python myfile.py doesn't work, but assuming that PowerShell bears at least some similarity to cmd.exe, the following should probably work: myfile.py. That's right, just enter the name of the Python script and hit enter.

Julian
  • 4,176
  • 19
  • 40
0

If you started by typing "python" in powershell you will need to get out of that script.

If you are in python type:

quit() then type python myfile.py

This should work if your python is installed correctly.

Pranav Singh
  • 17,079
  • 30
  • 77
  • 104
0

Try to type this in Powershell:

$env:path="$env:Path;C:\Python33

After this, command

python yourfile.py

should work.

ProgramFOX
  • 6,131
  • 11
  • 45
  • 51
-1

This my sound silly, especially coming from a beginner.

Just save the file on your desktop. Open up powershell and drag the file directly into powershell and it opens. kind of tedious but it works