简体   繁体   中英

hello world python program two version python on same machine

I have installed python2.7 and python3.7.3 on same windows 10 laptop. Now I am trying to run programmes separately. On windows cmd I type

cmd>py -2 print 'hello world'
C:\Users\data\installation\python2.7\python.exe: can't open file 'print': [Errno 2] No such file or directory

I tried running on powershell

PS> python2  print 'hello world'
python2 : The term 'python2' 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
+ python2  print 'hello world'
+ ~~~~~~~
    + CategoryInfo          : ObjectNotFound: (python2:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException`

I try to run on powershell

> py2 print 'hello world'
py2 : The term 'py2' 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 + py2 print 'hello world' + ~~~ + CategoryInfo : ObjectNotFound: (py2:String) [], CommandNotFoundException + FullyQualifiedErrorId : CommandNotFoundException`

If I run on powershell just

> py -2 print "hello world'

I get following prompt >>> but nothing else

same is problem with

> py -3 print ('Hello World')
C:\Users\data\installation\python\python.exe: can't open file 'print': [Errno 2] No such file or directory`

my script file ( script.py ) is:

#! py -3
python --version

I read these questions:

I expect hello world to be printed instead of this errors are coming.

You are not using the launcher or commands correctly.

The py launcher for Windows , like the Python command-line binary itself do not take Python code directly. So your first line

py -2 print 'hello world'

doesn't work because you asked the Python executable to find a file named print to run as a Python script. You can instead use the -c command line switch to run a line of Python code:

py -2 -c "print 'hello world'"

That tells the Windows console to start the process py , passing in the arguments -2 , -c and print 'hello world' , respectively (the " around the last part keep the console from seeing the spaces as delimiters). The py launcher then finds the Python 2.7 binary, and runs it with the remaining arguments.

Switching to the Powershell attempts, the first one doesn't work because Windows doesn't know where to find a python2 exectable:

The term 'python2' is not recognized as the name of a cmdlet, function, script file, or operable program

There is no py2 executable either .

Stick to using the py launcher, as the two questions you link to advice:

py -2 -c "print 'hello world'"

Your script.py file can't work, because everything past the first line, #! py -3 #! py -3 , is Python code . python --version is not valid Python. This will work:

#! py -3
import sys
print(sys.version_info)

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