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
4 Answers 4
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)
7 Comments
exec(open('Start.py').read(),globals())
1 Comment
Try this :
python "/path/Start.py"
Comments
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
import Start.pybut note that the second method won't work if you haveif __name__ == '__main__': ...__main__if you useexecfile()though. Except that's no longer available in Python 3. Ahem.subprocessmodule specially if you need to provide commandline arguments to your python script.