I have a python file but when I attempt to run it on my command line it wants to run it using Python 2.
I have #!/usr/bin/env python3
as the first line of code but it still wants to run it using Python 2. If it helps I'm on OSX.
Asked
Active
Viewed 494 times
1

card100
- 76
- 1
- 7
-
1try "python3" as command, worked for me a while ago – Banana Feb 07 '18 at 06:07
-
1Use _python3_ command to run file using python3. – PyMaster Feb 07 '18 at 06:07
-
1how do you run this program? – Sush Feb 07 '18 at 06:08
-
How are you invoking the command? – Stephen Rauch Feb 07 '18 at 06:08
-
I was running it as `python filename.py` – card100 Feb 07 '18 at 06:10
-
1Run `which python3` and then put that path on the shebang line, or just use `python3 filename.py` – cs95 Feb 07 '18 at 06:10
-
https://docs.python.org/3/tutorial/interpreter.html – whackamadoodle3000 Feb 07 '18 at 06:11
-
Great! `python3` worked. – card100 Feb 07 '18 at 06:11
1 Answers
1
Either,
Use
python3 filename.py
, orCreate an alias. Add
alias python=python3
in~/.bash_profile
, runsource ~/.bash_profile
, and thenpython filename.py
should invoke python3, orRun
which python3
, copy the path and add it at the top of your script as the shebang line. For me, the command returns/Library/Frameworks/Python.framework/Versions/3.4/bin/python3
. It may be different for you, orYou can also use
pyenv
, which supports multiple python environments simultaneously.

cs95
- 379,657
- 97
- 704
- 746