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

Community
  • 1
  • 1
nad
  • 2,640
  • 11
  • 55
  • 96
  • 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__': ...` – sshashank124 May 26 '14 at 14:33
  • 3
    Perhaps you want to tell us about your encoding format issue instead? This sounds like an [XY problem](http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem) here. – Martijn Pieters May 26 '14 at 14:35
  • [The answers to this question](http://stackoverflow.com/questions/436198/what-is-an-alternative-to-execfile-in-python-3-0) might address what you're asking, but as @MartijnPieters says, you're probably better trying to fix the underlying issue properly. – Steven Maude May 26 '14 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. – Martijn Pieters May 26 '14 at 14:40
  • Maybe you could use `subprocess` module specially if you need to provide commandline arguments to your python script. – KurzedMetal May 26 '14 at 14:40

4 Answers4

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)
timgeb
  • 76,762
  • 20
  • 123
  • 145
  • In which case we'd close the question as a dupe, instead of repeating the highest voted answer here. – Martijn Pieters May 26 '14 at 14:42
  • @timgeb what you suggested didn't work. Instead below worked exec(open("./filename").read()) – nad May 26 '14 at 18:03
  • although that doesn't solve my original encoding problem – nad May 26 '14 at 18:07
  • @lilyhack Weird, I tested it out in the interpreter. What error did you get? – timgeb May 26 '14 at 18:08
  • @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) ... – nad May 26 '14 at 18:14
  • @lilyhack after exec(c) has been entered, the interpreter will display ... now you just hit enter again, without adding indentation. – timgeb May 26 '14 at 18:17
  • @timgeb ah works now. Although that doesn't solve my encoding problem. Will post that in a separate thread. – nad May 26 '14 at 18:27
2

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

Chinmoy Kulkarni
  • 187
  • 1
  • 13
  • While this code snippet may solve the question, [including an explanation](//meta.stackexchange.com/questions/114762/explaining-entirely-code-based-answers) 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! – Mogsdad Dec 06 '17 at 23:00
0

Try this :

    python "/path/Start.py"
eeeee
  • 3
  • 4
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
yrz
  • 57
  • 2
  • 10