I am trying to use python 3 instead of python 2 - python

Currently in my mac terminal when I type python --version it says I am using python 2.7.x. I would like to change so that I am using python 3.8.x. How can I update this in terminal? Also, if I would like to go back to using python 2.7, is there a way to revert it?
Thanks!

If Python 3 is installed, you should be able to run python3. If not, install the current distribution from their website.
Python 2.7.x is always available via the alias python2.

brew install python3
will install python 3 and you can use python3 command to run 3x version of python. If you want to use python 2x version you could either use virtualenv or you could just use python2 to run your program with python 2x version.
If you want to implicitly specify that inside a file you could use
#!/bin/env python2.7
on top of the script to run the program with python 2.7 version.
As you see, there are many options for the same.

Related

How can I change my python version in git bash?

I been looking at fixes for this but im terrible at search. I came across this fix
didnt work
$ PATH=$PATH:/c/Python36/python
$ python --version
Python 2.7.13
I came across an alias fix turned out to be temporary so that didnt work
I want basically every command i do like everything under Python36/Scripts like pip, pytest, etc to be from python36. But everything is going to python 2.7
how do you change your python version? I am trying to change my git bash python version from python 2.7.13 to 3.6. I don't want to delete python 2.7 tho
Do this:
pkg install python2
And to use python2 do
python2 yourcode.py
And for python3 use
python3 yourcode.py
To check Version if you check:
python - -version
It will give 3.9.2 for me and 3.6 for you but if you check
python2 - - version
It will give the result 2.7.18
Here is the proof:
Check this in order to change the default python version without removing the package.
https://unix.stackexchange.com/a/521060/214441

After switching to Python 3.6.5_1, the displayed version is still 3.7.2

I am trying to use Python 3.6.5_1 for OpenCV, so I have switched Python versions using "brew switch python 3.6.5_1", but after checking my current version of Python using "python --version", I apparently am still using Python version 3.7.2. Any solutions? By the way, this is the tutorial I am following: https://www.pyimagesearch.com/2018/08/17/install-opencv-4-on-macos/
Check your python path to see where your python executable is being stored---which python in terminal. I bet it's still pointing to your 3.7.2 directory's python.
If you are switching between python versions, virtualenv can be used to initialize isolated environment with a specific python version.
virtualenv -p [preferred-python-version] open_cv_project_env

How to use and install all the libraries of Python3?

Using Ubuntu 16.04.
I was doing development in Python2.7, although recently moved to Python3.5, both of which come by default. The problem is, I find all the python libraries have to be reinstalled or downloaded for the next Python3. Also, the behaviour of Python seems weird because to execute the same script in python3, I have to enter in terminal
python3 script.py
which is different from other applications where I do not have to give the version number. Anyway, the questions I am trying to find answers are
To what extent are the libraries, packages (such as pip etc.) shared between 2.7 and 3.5? Or do I need double installations (and double the space) for everything now? A bit space limited in my old laptop.
Most of the installation instructions and commands I find online do not specify whether they are for 2 or 3. Given that I have two versions, how do I control/make sure they go to 3.5?
Is it advisable and possible to completely remove everything related to 2.7? Can I keep working with 3.5?
The first thing you need to know is that all official libraries and python tools for python3 got the "3" character to separate them from the previous versions. So, you need to use pip3, and not pip, python3, not python, and the packages are called python-pygame, not python-pygame.
So, to answer to you in order:
Yes, the you have to double the space needed if you decide to use both python2 and python3
Usually, if in the tool name there is 3, it is for python3, and if not it's for python2
Python2 and python3 are completly indipendent (different path, indipendent versions, etc.) so having python 2.7 installed doesn't affect python3 BUT since python 3 is the next version of python 2, it makes it obsolete (in my opinion) so if you don't have enough space for both, keeping python2 is absolutely not needed nor useful
If you want to control your python execution then you have various method or techniques:
For downloading packages according to version
You can use pip{version-name} to download the libraries. Like if you want to download library of python 2.7 then write
pip2.7 install package-name
for python 3.5 then use
pip3.5 install package-name
For execution of program:
If you want to execute the program accoridng to you choice of version then just use
python{version-name} script.py
eg:
python2 script.py
python3 script.py
or you just write down the path of your python version on the top of script. Please refer this for more details:
Why do people write #!/usr/bin/env python on the first line of a Python script?

Switching from python 2.7 > 3.6

I initially used python 2.7 on my Mac, how can I now run 3.6 from the terminal? I have installed 3.6, but cannot find any instructions how to make 3.6 the default option.
You can specify the python version you want when running the program:
For Python 3.x
python3 filename.py
For Python 2.x:
python filename.py
You can also use an alias to specify the specific python version by typing this command:
alias python="python3"
In the cmd line type python3 where you would type python before.
To check which (and where) is currently the default version use which python
As others have pointed out, it's best to manage your installations through something like pip, Homebrew, or Anaconda. It makes it much easier to keep track of packages and versions.

Uninstalling Python 2.7 on OSX 10.8.4

Main problem:
I've installed recently Python3.3 - If I run now in Terminal: python script.py (where script.py is coded in version 3.3) I'll get a python 2.7 output e.g.:
print('String',Var) --> ('String',Var)
Instead of:
print('String, Var) --> String Var
How can I uninstall Python 2.7 easily with Macport (without reading through Shell commands (time restriction)?)
This one didn't worked.
Second (smaller) problem:
If I type in Terminal python, I'll get python2.7 idle as output. How can I change this, so that command python refers to python3.3 (instead of using the command python3)
(About me:
Python2.7 novice, absolutely no Shell knowledge, OS X 10.8.4 User, Xcode and Macport installed.)
Bad idea to uninstall the pre installed version of python. Better idea is to alias python to whatever you want in your bashrc/bash_profile.
In your home directory, aka ~, you might already have a .bash_profile(If you don't have one, you can make it). You can edit that with your favorite text editor and add alias python='python3' Or whatever you want called whenever you type python into bash.
(FWIW Homebrew is the new hotness, you might want to look into it as well)
I agree that it is bad idea to uninstall Python 2.7, just use following commands:
To list available Python versions:
port select --list python
To select desired version:
sudo port select python desired_version_from_list
This is proper and easy way to do it in MacPorts.
DON'T UNINSTALL PYTHON!!
It will mess up everything --> may be crash your OS. I tried that in Fedora 17 and it failed my package manager as yum is build in Python. One many great thing about Python is it supports multiple versions at once in the same platform which you already experienced.
Now, to resolve your problem do Edgar suggested.
Also, while writing your python code do this:
#!/usr/bin/env python3
print('Hello world!')
Then,
python hello.py would run code in python3.

Categories