简体   繁体   中英

Unable to run Python code

I have installed Python 3.5 in the directory c:\\users\\rshukla\\desktop\\python\\python35

I have downloaded a simple Python code that I have saved in Atom.io also in the same folder c:\\users\\rshukla\\desktop\\python\\happy_hour.py And I am trying to it run through 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}")

When I try running this in Powershell, I get the output below - what am I doing wrong?

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

This type string formating start supported on 3.6 version.

Use function format for python3.5:

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

Or other methods for string formating:

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

Try doing: c:\\users\\rshukla\\desktop\\python\\python35 c:\\users\\rshukla\\desktop\\python\\happy_hour.py

If you don't want to type complete path of python executable(python35) add this path c:\\users\\rshukla\\desktop\\python to PATH variable in your PS shell.

PATH variable is the location where powershell searches for executables you're try to executes any CMD line utility, right now it(powershell) is not able to find python35, so either you can give whole path of python35 exe or add the directory into path variable.

ps. ignore typos, written via mobile

you have to put python in path. see this How to add to the pythonpath in windows 7?

but i think this kind of format string is new in python 3.6 and not available in python 3.5

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM