-1

I am starting fresh with python and trying to execute a code from the python command window. I wrote a file on Desktop\practice\new.py and lunched the python command window.

when I type

C:\users\user\Desktop\practice\new.py

it gives me

SyntaxError: invalid syntax 

Executing from CMD worked, but from python window didnt!

Any help?

EDIT2: when i put the compiled code in the directory and use the 'import' it runs, but when the compiled is not in the same directory it won't execute

EDIT: the file contains a simple print statement nd is sytax error free

Syntax_Error
  • 5,964
  • 15
  • 53
  • 73

6 Answers6

3

Everything is explained in here: http://docs.python.org/faq/windows.html#how-do-i-run-a-python-program-under-windows

The main point that when you launch python shell. Its like a live programming. Try to type in it:

>>> print 'hello world'

If you want to launch your file - run in cmd: python C:/users/user/Desktop/practice/new.py

UPDATE: If you do want to run file from within python shell - it was answered here: How to execute a file within the python interpreter?

Community
  • 1
  • 1
JackLeo
  • 4,579
  • 9
  • 40
  • 66
  • I successfully ran from the cmd, but wanted to try from the python shell, because In Orelie learning book they have an example to use import and reload and they somehow load the file within the shell! – Syntax_Error Sep 20 '11 at 14:12
2

When you say you're using the "python command window" I'm guessing you mean IDLE...? If so, rather than try to type a command to run a script you've already created as a file, just use File > Open to open that file and then press F5 to run it. Good luck!

jgarbers
  • 231
  • 3
  • 13
2

The python command window is expecting python commands. Try typing 'import system' or 'print 1+2'.

If you want to run the code in another file you need to use 'import'. Its easier if you start in the same directory, in which case just doing 'import new' will work.

However, there's already a 'new' module in the python library, so the easiest thing to do is to rename your file something else...

Spacedman
  • 92,590
  • 12
  • 140
  • 224
1

It is not working because you are entering the path like c:\users\user\desktop\practice\new.py.....

now try this way: c:/users/user/desktop/practice/new.py
I hope this will work for you i.e. just change '\' to '/' have a try...

diffracteD
  • 758
  • 3
  • 10
  • 32
0

You can run the file like this:

execfile(r'C:\users\user\Desktop\practice\new.py')
reader_1000
  • 2,473
  • 17
  • 15
-1

Edit: read the comments below this answer before trying it!

Try this:

import sys
sys.path.append("C:\users\user\Desktop\practice\")
import new #won't work - call it something other than new.py...
ed.
  • 1,373
  • 8
  • 10
  • Note that this doesn't work (even ignoring the syntax error) for all module names. Namely, the first file or package that's in any of the dirs in `sys.path` is loaded. Stdlib modules and `easy_install`'d modules usually come before your own paths. –  Sep 20 '11 at 14:00
  • And there _is_ a 'new' module in (at least) my python library! – Spacedman Sep 20 '11 at 14:02
  • 1
    Fair point. Call it something other than "new.py" then, or run: "python c:\users\user\Desktop\practice\new.py" to execute it directly. – ed. Sep 20 '11 at 14:02