从python命令lin运行脚本

2024-04-28 07:42:34 发布

您现在位置:Python中文网/ 问答频道 /正文

使用Python3.3

尝试从python命令行运行脚本。由于某些编码格式问题,需要从python命令行而不是windows命令行运行此命令。但我犯了以下错误:

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

我想我已经在Python中了,所以上面的内容是无效的。我试过execfile,但也没用。

有人能帮忙吗?

编辑

从python命令行运行脚本的问题得到了解决。尽管这并不能解决最初的编码问题。请看这里的线程Changing the preferred encoding for Windows7 command prompt


Tags: 命令行py命令脚本编码windows格式错误
3条回答

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

试试这个:

    python "/path/Start.py"

您已经在运行Python,因此不需要运行python命令。 execfile在Python3中消失了,但是您可以这样做:

with open("Start.py") as f:
    c = compile(f.read(), "Start.py", 'exec')
    exec(c)

相关问题 更多 >