user1123936
user1123936

Reputation: 181

Why can't Python find and run my scripts when calling Python from Python command line?

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

Upvotes: 1

Views: 11480

Answers (4)

user2056214
user2056214

Reputation: 1

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

Upvotes: 0

Levon
Levon

Reputation: 143102

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.

Upvotes: 2

Junuxx
Junuxx

Reputation: 14271

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")

Upvotes: 1

BrenBarn
BrenBarn

Reputation: 251438

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.

Upvotes: 5

Related Questions