无法运行Python代码

时间:2017-07-08 10:39:05

标签: python

我已在目录c:\users\rshukla\desktop\python\python35

中安装了Python 3.5

我已经下载了一个简单的Python代码,我在Atom.io中保存了同样的文件夹c:\users\rshukla\desktop\python\happy_hour.py 我正试图通过Powershell

import random

bars = ["Shoolbred's",
    "The Wren",
    "The Scratcher",
    "ACME",
    "Blind Barber"]

people = ["Mattan",
      "Chris",
      "Pooje",
      "that person you forgot to text back",
      "Kanye West",
      "Samuel L. Jackson"]

random_bar = random.choice(bars)
random_person_1 = random.choice(people)
random_person_2 = ramndon.choice(people)

print(f"How about you go to {random_bar} with {random_person_1} and {random_person_2}")

当我尝试在Powershell中运行它时,我得到下面的输出 - 我做错了什么?

PS C:\Users\rshukla\desktop\python> python happy_hour.py
python : The term 'python' is not recognized as the name of a cmdlet, function, script file, or operable program.
Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:1 char:1
+ python happy_hour.py
+ ~~~~~~
    + CategoryInfo          : ObjectNotFound: (python:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

3 个答案:

答案 0 :(得分:0)

3.6版本支持

This type string formating

使用函数format  对于python3.5:

print('{two} {plus} {two} = 4'.format(two=2, plus='4'))

或其他字符串格式化方法:

print("%d %s %d = 4" % (2, '+', 2))
print("%(two)d %(plus)s %(two)d = 4" % {"two": 2, "plus": '+'})

答案 1 :(得分:0)

尝试做: c:\users\rshukla\desktop\python\python35 c:\users\rshukla\desktop\python\happy_hour.py

如果您不想键入py​​thon可执行文件的完整路径(python35),请将此路径c:\users\rshukla\desktop\python添加到PS shell中的PATH变量。

PATH变量是powershell搜索您尝试执行任何CMD行实用程序的可执行文件的位置,现在它(powershell)无法找到python35,因此您可以给出python35 exe的完整路径或添加目录到路径变量。

PS。忽略错别字,通过手机写的

答案 2 :(得分:-1)

你必须把python放在路径中。请参阅此How to add to the pythonpath in windows 7?

但我认为这种格式字符串在python 3.6中是新的,在python 3.5中不可用