【问题标题】:How to launch python scripts from Windows Shell?如何从 Windows Shell 启动 python 脚本?
【发布时间】:2018-10-15 01:25:25
【问题描述】:

我想启动一个 python 脚本(file.py)

为了做到这一点,我在 Python 解释器中复制了这个文件的路径,但我收到了错误 SyntaxError: unexpected character after line continuation character

我是初学者,这是迄今为止我发现的最简单的启动 Python 脚本的方法...如果您有更简单的方法,我愿意接受提示...

谢谢

这是我的 Python 脚本:

import os
import csv

ACCEPTED_MSG="""
Hi {},

We are thrilled to let you know that you are accepted to our 
programming workshop.

Your coach is {}.

Can't wait to see you there ! 

Thank you,

Workshop Organizers
"""

REJECTED_MSG="""
Hi {},

We are verry sorry to let you know that due to a big number
of applications we couldn't fit you at the workshop this time...

We hope to see you next time.

Thank you, 

Workshop Organizers
"""

path_to_file = "C:/Users/Julien/Downloads/data.csv"

file_exists = os.path.exists(path_to_file)

if file_exists:

    csv_file = open(path_to_file)
    csv_reader = csv.reader(csv_file, delimiter=',')
    next(csv_reader)

    for row in csv_reader:
        name, email, accepted, coach, language = row
        print (name, email, accepted, coach, language)

        if accepted =='Yes':
            msg = ACCEPTED_MSG.format(name, coach)
        else:
            msg = REJECTED_MSG.format(name)

            print ("Send e-mail to: {}".format(email))
            print("E-mail content:")
            print (msg)
csv_file.close()

【问题讨论】:

  • 在命令行上运行python file.py

标签: python windows cmd launch


【解决方案1】:

你可以跑

python<version> <path>

例子:

Python3 test.py

从您的错误来看,您似乎运行正确,但代码包含编译错误。添加代码sn -p 这样我们就可以看到问题出在哪里了

【讨论】:

  • 我认为 SyntaxError 来自 OP 实际上试图将 python shell 用作系统 shell ;)
  • 正如@bruno desthuillers 所建议的那样,这可能是 pyShell 上的错误命令导致的 sintax 错误,完全有道理,让我们保持更新^^
  • 有人知道合成器吗?我在谷歌上搜索,但什么也没找到
  • 系统税是为了什么?我们的回答没有解决您的问题吗?
  • 在 Python 解释器 shell 中启动脚本的语法......我想我找到了我正在阅读的有用信息......
【解决方案2】:

如果你只是想运行一个脚本,那么语法是(来自你的 system__shell, __not python shell):

$ python path/to/your/script.py

如果你想在现有的 python shell 中执行,你可以运行(在 python shell 中):

>>> execfile("path/to/your/script.py")

【讨论】:

  • >>> python C:\Users\Julien\Desktop\Python\Python 3\CIFG\Python Basics\Reading Data From CSV.py File "", line 1 python C: \Users\Julien\Desktop\Python\Python 3\CIFG\Python Basics\Reading Data From CSV.py ^ SyntaxError: invalid syntax
  • @JulienBlomme 抱歉,我的意思没有说清楚:第一种语法是从您的 system shell 运行脚本,而不是从 python shell 中运行。
【解决方案3】:

我无法从 Windows 控制台启动 Python 脚本,因为我没有添加变量 PATH。

所以我在这个 YouTube 教程之后添加了变量路径: https://www.youtube.com/watch?v=uXqTw5eO0Mw

然后,当我尝试启动脚本时,出现错误“未找到此类文件或目录”。那是因为我没有在脚本“C:\ ...”的路径中放置空格而不是“C:\”

最后,我收到一条消息“python.exe 找不到 'ma​​in' 模块”。这是因为我需要再次使用“.py”扩展名保存我的脚本。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-03-16
    • 1970-01-01
    • 2011-11-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-20
    • 2015-06-14
    相关资源
    最近更新 更多