0

Using Python3.3

Trying to run a script from python command line. Need to run this from python command line instead of windows command line because of some encoding format issue. But I am getting below error:

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

I think I am already within Python so the above is invalid. I tried execfile but that doesn't help either.

Can anyone please help?

EDIT

The problem with running the script from python command line is solved. Although that doesn't solve the original encoding issue. See the thread here Changing the preferred encoding for Windows7 command prompt

5
  • You cannot do that from the python command prompt. You either do it from cmd or in python you do: import Start.py but note that the second method won't work if you have if __name__ == '__main__': ... Commented May 26, 2014 at 14:33
  • 3
    Perhaps you want to tell us about your encoding format issue instead? This sounds like an XY problem here. Commented May 26, 2014 at 14:35
  • The answers to this question might address what you're asking, but as @MartijnPieters says, you're probably better trying to fix the underlying issue properly. Commented May 26, 2014 at 14:39
  • @sshashank124: The name is set to __main__ if you use execfile() though. Except that's no longer available in Python 3. Ahem. Commented May 26, 2014 at 14:40
  • Maybe you could use subprocess module specially if you need to provide commandline arguments to your python script. Commented May 26, 2014 at 14:40

4 Answers 4

2

You are already running Python, so there's no need to run the python command. execfile is gone in Python3, but you can do it like this:

with open("Start.py") as f:
    c = compile(f.read(), "Start.py", 'exec')
    exec(c)
Sign up to request clarification or add additional context in comments.

7 Comments

In which case we'd close the question as a dupe, instead of repeating the highest voted answer here.
@timgeb what you suggested didn't work. Instead below worked exec(open("./filename").read())
although that doesn't solve my original encoding problem
@lilyhack Weird, I tested it out in the interpreter. What error did you get?
@timgeb the python command line keeps asking for more command like this (doesn't execute anything) >>> with open("Start.py") as f: ... c = compile(f.read(), "Start.py", 'exec') ... exec(c) ...
|
2

exec(open('Start.py').read(),globals())

1 Comment

While this code snippet may solve the question, including an explanation really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion. Please also try not to crowd your code with explanatory comments, this reduces the readability of both the code and the explanations!
0

Try this :

    python "/path/Start.py"

Comments

0

For Windows, we must write

C:\Python31\python.exe test.py > results.txt

// that is from CMD - from the Summersfeld's evergreen "Programming in Python 3 - A Complete intro". And if we have an environment variable for python, we don't even need the C:\Python31\ part just

C:\>python.exe test.py > results.txt

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.