V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
tania2046
V2EX  ›  问与答

[ Python ] 编程小白, Python 终端读文件 SyntaxError

  •  
  •   tania2046 · 2017-12-15 10:45:47 +08:00 · 2382 次点击
    这是一个创建于 2317 天前的主题,其中的信息可能已经有所发展或是发生改变。
    初学 python
    用 sublime 编辑器,写好的文件与 python 安装目录一致
    在 poweshell 终端运行的时候,一直提示 SyntaxError: invalid syntax
    求问大神,怎么破!

    P.S.:powershell 运行路径
    1.
    >>> python input.py
    File "<stdin>", line 1
    python input.py
    ^
    SyntaxError: invalid syn

    2.
    PS C:\Users\DELL> python
    Python 3.6.4rc1 (v3.6.4rc1:3398dcb, Dec 5 2017, 20:41:32) [MSC v.1900 64 bit (AMD64)] on win32
    Type "help", "copyright", "credits" or "license" for more information.
    >>> input.py
    Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    AttributeError: 'builtin_function_or_method' object has no attribute 'py'
    11 条回复    2017-12-15 16:38:19 +08:00
    tania2046
        1
    tania2046  
    OP
       2017-12-15 11:07:11 +08:00
    为什么只有点击没有回复啊!!
    moonmagian
        2
    moonmagian  
       2017-12-15 11:11:04 +08:00 via Android
    直接输入 python 是进入了交互模式,正确的运行方法是 cd 到 python 的安装目录跑 python input.py
    SyntaxError 应该是没把 python 加到 PATH 里导致的
    moonmagian
        3
    moonmagian  
       2017-12-15 11:12:16 +08:00 via Android   ❤️ 1
    好像 SyntaxError 也是因为进入了交互式模式,不进交互式模式就好了
    msputup
        4
    msputup  
       2017-12-15 11:13:28 +08:00   ❤️ 1
    python input.py
    iridescent0rsy
        5
    iridescent0rsy  
       2017-12-15 11:14:23 +08:00 via Android
    @moonmagian 对的,他在交互模式里执行了
    python abc.py
    这句应该在命令行里执行
    topbandit
        6
    topbandit  
       2017-12-15 11:15:11 +08:00   ❤️ 1
    这个问题,明明是电脑小白,算不上编程小白
    Aalen
        7
    Aalen  
       2017-12-15 11:19:22 +08:00   ❤️ 1
    所以我觉得你们并没说清楚。。。

    错误的做法:打开 powershell 后 输入 python 然后 python input.py

    正确的做法:打开 powershell, 进入 input.py 所在的目录, 输入 python input.py
    SingeeKing
        8
    SingeeKing  
       2017-12-15 11:20:05 +08:00   ❤️ 1
    > python input.py
    tania2046
        9
    tania2046  
    OP
       2017-12-15 11:31:10 +08:00
    @Aalen
    完美!已解决~
    电脑小白谢大神
    hyi
        10
    hyi  
       2017-12-15 15:11:15 +08:00 via Android
    嗯,不是不可以用函数名当文件名的吗?
    tania2046
        11
    tania2046  
    OP
       2017-12-15 16:38:19 +08:00
    @hyi
    嗯,我傻。。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5193 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 08:24 · PVG 16:24 · LAX 01:24 · JFK 04:24
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.