1

I am in the python command line (using python 2.7), and trying to run a Python script. My operating system is Windows 7. I have set my directory to the folder containing all of my scripts, using:

os.chdir("location").

os.getcwd() returns this location.

When I type:

python myscript.py

I get this error:

File "<stdin>", line 1

python myscript.py
              ^

SyntaxError: invalid syntax.

What have I done wrong?

The first uncommented line of the script I'm trying to run:

from game import GameStateData

octopusgrabbus
  • 10,555
  • 15
  • 68
  • 131
user1123936
  • 181
  • 3
  • 4
  • 14
  • 1
    Are you saying the `os.chdir` and `os.getcwd` lines are in `myscript.py`? – robert Jun 02 '12 at 19:08
  • 6
    Are you typing `python myscript.py` within the Python shell? – Ray Toal Jun 02 '12 at 19:11
  • Please show us the exact error. – robert Jun 02 '12 at 19:11
  • No. I typed them in the command line. – user1123936 Jun 02 '12 at 19:11
  • @user1123936 "I typed *them* at the command line" .. the os.chdir etc commands, or the `python myscript` command? – Levon Jun 02 '12 at 19:15
  • @user1123936 can you show us the start of your script, it will be helpful pinpointing the error. – Levon Jun 02 '12 at 19:15
  • Are you importing `os`? We'll be able to help much more if you share some of your code, like the 10 lines or so surrounding where the error occurs. Otherwise we can just guess, sort of hit and miss. – Levon Jun 02 '12 at 19:34
  • Possible duplicate of [How to run a python script from IDLE interactive shell?](http://stackoverflow.com/questions/17247471/how-to-run-a-python-script-from-idle-interactive-shell) – tripleee Mar 08 '17 at 05:46

4 Answers4

5

It sounds like you're trying to run your script from within Python. That's not how it works. If you want to run myscript.py, you need to do it from a system command prompt, not from inside the Python interpreter. (For instance, by choosing "Command Prompt" from your start menu. I think it's usually under "Accessories" or something like that.) From there you'll need to change to the directory where your scripts are by using the CD command.

BrenBarn
  • 242,874
  • 37
  • 412
  • 384
  • You can get a prompt quickly by pressing `Win + R` to open the run dialog, enter `cmd` and press enter. – hammar Jun 02 '12 at 19:14
  • @BrenBarn: Well it can be 'how it works', as in my answer. It just isn't a useful or convenient way to do it :P – Junuxx Jun 02 '12 at 19:30
2

Based on the additional information you have provided it does look like you are issuing the command inside of Python.

EDIT: Maybe part of the confusion comes from the term command line. You are at the command line in both the "Windows command" shell, and also when you are inside the "Python shell".

This is what I get in the command line when inside the Python shell:

D:\Users\blabla \Desktop>python

Python 2.7.2 (default, Jun 12 2011, 15:08:59) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.

>>> python testit.py
  File "<stdin>", line 1
    python testit.py
                ^
SyntaxError: invalid syntax
>>>

Or this:

>>> os.chdir("..")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'os' is not defined
>>>

My suggestion would be to open a Windows command shell window with the cmd command and then issue your python myscript.py from there.

For more help it would be helpful to see your code, at least the first few lines where the error occurs and some certainty as to where the python command is being issued.

Levon
  • 138,105
  • 33
  • 200
  • 191
1

As the other answers indicate, you are probably in the Python shell unintentionally. But if you really do want to run your script from there, try execfile("myscript.py")

Junuxx
  • 14,011
  • 5
  • 41
  • 71
0

on windows shell run echo %PATH%, and check if your .py is under any of the paths.

Dog Ears
  • 9,637
  • 5
  • 37
  • 54