-1

I'm currently trying to install a package on python that can only be installed on Python 3. I have both 3.6 and 2.7. I'm on a Windows machine. Whenever I type "python" into a newly opened command prompt it returns python 2.7. Then whenever I type "python" it says

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'python' is not defined

I believe python 3.6 is set as an environment variable on my path. Can someone offer some advice on how to switch these over? I've read py.exe from python 3's installion should switch between python 2 and 3, but I do not see how I am supposed to run that command other than clicking on it in my File Explorer and that does nothing.

Paul Rooney
  • 20,879
  • 9
  • 40
  • 61
Jin
  • 527
  • 6
  • 21

2 Answers2

1

Once you type in python, you go into Python's interpreter mode, where you can type Python code and get the result. You can type quit to leave that mode. If you want to run a script, you need to run, instead of just python, python filename.py, with the appropriate filename.

But you want to do that outside of the interpreter mode (otherwise known as REPL).

Note that the above will probably cause Python 2.X to be used to run your script, so if you want to run Python 3.X you will want to include this at the top of your script

#!/usr/bin/env python3

and then just run it from a newly opened command prompt (or any command prompt that is not in Python's interpreter mode) like filename.py.

See this question for more information.

sleighty
  • 895
  • 9
  • 29
  • Great! That makes a ton of sense. So that works for me just fine. When I try to install the package with pip I get this error `pip install tensorflow File "", line 1 pip install tensorflow` – Jin Aug 08 '18 at 04:42
  • @Mrl same thing with the `pip` commands, try using them from the command prompt rather than the Python interpreter! – sleighty Aug 08 '18 at 04:46
  • Oh yes that's right. I totally forgot I was in the interpreter when I typed in python. Thanks! – Jin Aug 08 '18 at 04:52
  • @JamesLiu So what should I do to switch between different versions of python 3 like 3.5 or 3.6? – Jin Aug 08 '18 at 04:56
  • @Mrl for python3.5 use py -3.5 for python3.6 use py -3.6 – James Liu Aug 08 '18 at 05:01
  • ah okay. so when I use pip to install packages does it install it in relation to a specific version of python? If so, how do I set that? The package I'm trying to install is only supported on 3.5 – Jin Aug 08 '18 at 05:05
1

You type python in python repl

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'python' is not defined

You should open a new command prompt or type ctrl+Z or quit() in the python repl

For switching python 2 and 3

Use

py -3
py -2
James Liu
  • 497
  • 3
  • 8