What version of Python is on my Mac? - python

I have a mac, when I do:
python --version
I got:
Python 2.7.6
but when I got to:
/System/Library/Frameworks/Python.framework/Versions/3.3
where is 2.7.6 located ?
any advice what should I do?
UPDATE:
$ which -a python python3
/usr/bin/python
/usr/local/bin/python3
$ brew list python python3
Error: No such keg: /usr/local/Cellar/python
$ locate python
WARNING: The locate database (/var/db/locate.database) does not exist.
To create the database, run the following command:
sudo launchctl load -w /System/Library/LaunchDaemons/com.apple.locate.plist
Please be aware that the database can take some time to generate; once
the database has been created, this message will no longer appear.
in PyCharm editor, the default settings interpreter shows:

You could have multiple Python versions on your macOS.
You may check that by command, type or which command, like:
which -a python python2 python2.7 python3 python3.6
Or type python in Terminal and hit Tab few times for auto completion, which is equivalent to:
compgen -c python
By default python/pip commands points to the first binary found in PATH environment variable depending what's actually installed. So before installing Python packages with Homebrew, the default Python is installed in /usr/bin which is shipped with your macOS (e.g. Python 2.7.10 on High Sierra). Any versions found in /usr/local (such as /usr/local/bin) are provided by external packages.
It is generally advised, that when working with multiple versions, for Python 2 you may use python2/pip2 command, respectively for Python 3 you can use python3/pip3, but it depends on your configuration which commands are available.
It is also worth to mention, that since release of Homebrew 1.5.0+ (on 19 January 2018), the python formula has been upgraded to Python 3.x and a python#2 formula will be added for installing Python 2.7. Before, python formula was pointing to Python 2.
For instance, if you've installed different version via Homebrew, try the following command:
brew list python python3
or:
brew list | grep ^python
it'll show you all Python files installed with the package.
Alternatively you may use apropos or locate python command to locate more Python related files.
To check any environment variables related to Python, run:
env | grep ^PYTHON
To address your issues:
Error: No such keg: /usr/local/Cellar/python
Means you don't have Python installed via Homebrew. However double check by specifying only one package at a time (like brew list python python2 python3).
The locate database (/var/db/locate.database) does not exist.
Follow the advice and run:
sudo launchctl load -w /System/Library/LaunchDaemons/com.apple.locate.plist
After the database is rebuild, you can use locate command.

To check third version, we can use,
python3 --version
To install python on macOS, we need to install command line developer tools. From Catalina, python is removed from OS bundle. For more information, Apple Official and Python Official.

Use below command to see all python installations :
which -a python

Use the which command. It will show you the path
which python

Take a look at the docs regarding Python on Mac.
The version at /System/Library/Frameworks/Python.framework is installed by Apple and is used by the system. It is version 3.3 in your case. You can access and use this Python interpreter, but you shouldn't try to remove it, and it may not be the one that comes up when you type "Python" in a terminal or click on an icon to launch it.
You must have installed another version of Python (2.7) on your own at some point, and now that is the one that is launched by default.
As other answers have pointed out, you can use the command which python on your terminal to find the path to this other installation.

You can use python official installer. It's very easy and interactive.
Refer https://www.python.org/downloads/release/python-2716/

Just type python and then hit tab-button You will get the list of all the python available in your os

Related

python -v not returning anything in Cygwin

I am trying to run python3.6.8 in cygwin.
Typing python -v just results in the terminal window hanging. I have to escape out of it to make it respond further.
Typing python36 -v results in the error python36 command not found. I definitely installed this module while installing Cygwin.
I notice in /cygdrive/c, only python27 seems to be present. Running python -i also shows me that 2.7 is installed.
I want to run 3.6.8, have tried reinstalling multiple times, same issue.
Running "which python" shows only
/cygdrive/c/Python27/python
How can I troubleshoot this?
Generally all the python versions with 3 can be accessed using python3 as binary. In windows often the path variable doesn't hold the python3 binary path so the binary is not called.
do
echo $PATH | grep python
Check for the version of the python if python36 is present then do
python3 --version
Look in /usr/bin for all python exe's. Use ls -l /usr/bin/python*.*. I suspect you will not find python3.6m.exe.
Run cygwin setup again and select the python3.6 package. You can also install a version of 3.7, 3.8 or some older ones if you wish. You can only get the latest release of the e.g. 3.6 series.

Unable to Run Python 3 After Homebrew Installation

After installing Homebrew using the script on their homepage and checking if everything was alright with brew doctor, I issued brew install python3 in order to install Python 3 on my Mac.
Everything seemed fine until I tried running python3 --version; I ended up getting:
-bash: /Library/Frameworks/Python.framework/Versions/3.5/bin/python3: No such file or directory
I checked in the file directory to see what was going on and indeed, I didn't see any files pertaining to Python in my framework folder. It also looks like Python 2.7 isn't on my Mac either.
This is what I got after installing Python 3:
Summary
🍺 /usr/local/Cellar/python3/3.5.1: 3,438 files, 51.5M
edit_2: maybe this has something to do that there is no Python framework? I just read this off the Python website:
The Apple-provided build of Python is installed in /System/Library/Frameworks/Python.framework and /usr/bin/python, respectively. You should never modify or delete these, as they are Apple-controlled and are used by Apple- or third-party software. Remember that if you choose to install a newer Python version from python.org, you will have two different but functional Python installations on your computer, so it will be important that your paths and usages are consistent with what you want to do.
I think I detected what the problem is.
I guess that, at a certain moment, you had installed python from the official site instead of via Homebrew.
In my case, I installed it via the official website Python 3.6.4. A few months later, I wanted to upgrade it and noticed that it was very complex. So, I decided to move to Homebrew. Open a terminal window and let's try to fix this:
First, let's uninstall previous Python versions:
sudo rm -rf /Library/Frameworks/Python.framework
sudo rm -rf /usr/local/bin/python3
Then, remove the previous frameworks from the $PATHvariable:
nano ~/.bash_profile
You will see something like that:
# Setting PATH for Python 2.7
# The original version is saved in .bash_profile.pysave
PATH="/Library/Frameworks/Python.framework/Versions/2.7/bin:${PATH}"
export PATH
# Setting PATH for Python 3.6
# The original version is saved in .bash_profile.pysave
PATH="/Library/Frameworks/Python.framework/Versions/3.6/bin:${PATH}"
export PATH`
This is the problem: These paths don't exist. Comment the $PATH editions (or erase them):
# Setting PATH for Python 2.7
# The original version is saved in .bash_profile.pysave
# PATH="/Library/Frameworks/Python.framework/Versions/2.7/bin:${PATH}"
# export PATH
# Setting PATH for Python 3.6
# The original version is saved in .bash_profile.pysave
# PATH="/Library/Frameworks/Python.framework/Versions/3.6/bin:${PATH}"
# export PATH
Restart the computer and install via Homebrew Python 2 and 3:
brew update
brew install python
brew install python3
This worked for me. Now, if type python3 --version I get Python 3.7.0, and everything works fine :)
I had the same issue. I learned how to fix it for good:
Open "Applications" in Mac Finder and drag Python to the trash bin.
Empty the trash bin
If you have an error as above, then an official Python installation has been performed (as others have mentioned) via e.g. Python.org. This creates some kind of alias for the python or python3 commands outside a Bash alias. So while the command where python3 may point to /usr/local/bin/python3, python3 will still try to call /Library/Frameworks/Python.framework/Versions/3.5/bin/python3.
Note:
the MacOS system Python is /usr/bin/python
Homebrew Python(s) will be located in /usr/local/bin/
Pythons installed as an Apple application live in /Library/Frameworks/Python.framework/
Okay, this is what I gathered:
Don't delete the Python framework!
If it's deleted, then python3 --version won't work
Just install Python from the Python website
The framework will return and python3 --version will work
This error:
-bash: /Library/Frameworks/Python.framework/Versions/3.5/bin/python3: No such file or directory
suggests a remnant of some previous (attempt at an) installation of Python 3 using a different way (not Homebrew).
(I think this is actually where the Python installation from www.python.org goes. I wouldn't know though, as I've either never tried that package, but only installed the www.python.org version from source. This would suggest, though, that you already had an attempt at installing Python 3.5, something failed, and you're now trying Homebrew instead.)
I'd suggest moving (renaming) this out of the way, so your system doesn't pick it up. Something like
mv /Library/Frameworks/Python.framework/Versions/3.5 /Library/Frameworks/Python.framework/Versions/3.5-aside
(if there other versions of Python 3 in that directory, you may want to do the same for those.)
Also check that python3 isn't an alias. Commands such as
which python3
type python3
alias python3
will reveal that.
With the interfering Python 3 out of the way, try re-installing Python 3 through homebrew again. You may have to do an uninstall + reinstall.
Read carefully any homebrew messages once the installation is done, in particular if it mentions something about linking files: you may need to run something like brew link python3.

How can I use Python 2.6 in Ubuntu 11.10?

The default version of Python on Ubuntu 11.10 is 2.7, but I'm looking for 2.6. How do I make it default and where is the executable located?
I type which python2.6 but it returns nothing, yet I did have a python2.6 folder under /usr/lib/python2.6. But it doesn't look like the python2.7 which is at the same path /usr/lib/. Inside the python2.6, there are two folders: dist-packages and lib-dynload.
Actually I am configuring PyDev, and it requires me to specify where the python2.6 executable is. The python2.7 executable has been easily located by just using auto configuration as it is the default.
You can install the package python2.6 (apt-get install python2.6). At this point, the default version of Python will still be 2.7. You can change this via
ln -s /usr/bin/python2.6 /usr/bin/python
Note that there's a decent chance this could cause problems with your system. Several scripts assume the default version of Python is 2.7 and may break when run under a different version. If you have a script that explicitly requires Python 2.6, you can add a shebang at the beginning of your script to specify the version
#!/usr/bin/python2.6
On many systems, one version of python is the default. The rest get called by their name and version number:
~ $ python --version
Python 2.7.2
~ $ python2.6 --version
Python 2.6.7
Per the release notes, these should be available in Oneiric.
Your other questions:
Where is it? Run $ which python2.6 to find out.
How to make it the default? The safest way is to use alias so that change will be only visible to you. Otherwise, if you repoint /usr/bin/python to an unexpected version of Python, you will may break the OS scripts that rely on Python2.7. Rather than changing the default, it is better to just call the specific version of Python you need.

I can't change the default Python version from Apple supplied 2.7.2 to 3.2.2 (OS10.7)

in Lion, have installed python 3.2.2 from python.org. It comes with a terminal command that is supposed to move this version of python earlier by updating my shell profile. When i run it, and then check my default python in Terminal, it still says 2.7.2 which is the Apple supplied one.
Do I need to run the command with sudo to get it to change the default python to 3.2.2?
I've also tried the VERSIONER method but it doesn't work.
For Python 3 interpreters, you must enter python3 not python.
$ python -V
Python 2.7.2
$ python3 -V
Python 3.2.2
For more background on the current recommendation that python refer to a version of Python 2 and python3 refer to a version of Python 3, see the draft PEP 394 - The "python" Command on Unix-Like Systems.
Unfamiliar with osX, but as several system applications might depend on a certain python version, it might be a bad idea to swap the default.
One alternative that might suite your needs is to use pythonbrew, it allows you to have several python versions side by side and lets you set a default version on a user to user basis, without having to meddle with your system python, among other things. It is the equivalent of RVM for those who are familiar with ruby, or a sort of virtualenv for python interpreters. One big advantage is that it allows you to easily install and use the exact python versions you need independently from what is available in your operating system.
Some examples:
Install a python interpreter:
pythonbrew install 2.7.2
Permanently use the specified python (for current user):
pythonbrew switch 2.7.2
pythonbrew switch 3.2
Use the specified python in current shell:
pythonbrew use 2.7.2
Runs a named python file against specified and/or all pythons:
pythonbrew py test.py
pythonbrew py -v test.py # Show verbose output
pythonbrew py -p 2.7.2 -p 3.2 test.py # Use the specified pythons
The only downside of pythonbrew, is the need of a compiler and header files, and that an install can take a bit of time, as it compiles from source.

How to make python_select work for '$>python' command?

I installed a couple of pythons in different versions with macports, and the apple python 2.6 is also working. Now I need to run a program which requires MySQLdb package support in python, and this package was installed to the python I installed by macports. The program tells me that there is no MySQLdb installed, so I guess it is the apple python working for that program.
I searched for some help and found python_select for switching between pythons. However after the command
$>sudo python_select python25
told me that it selected the version "python25" for python, when I type
$>python
it is still apple python 2.6 that launches.
The question is that how can I make python25(the one with MySQLdb) work for the program rather than apple python?
Another important thing, the program is NOT a .py file and needs to be compiled before running. So do I need to re-install this program? My Mac OS version is Snow Leopard 10.6.
Any answer is appreciated.
By default, MacPorts installs user programs (or links to them) in /opt/local/bin. The MacPorts select_python command selects which python instance is linked to /opt/local/bin/python. It has no effect (nor should it) on what Apple installs in /usr/bin, which is where the Apple-supplied python and python2.x commands are.
To invoke the MacPorts python2.5, you either need to ensure that /opt/local/bin precedes /usr/bin on your shell $PATH (you can do this by modifying your .bash_profile or other shell initialization script) or you can simply invoke the desired python with an absolute path reference:
$ /usr/bin/python your-program.py
to use the Apple-supplied default python;
$ /opt/local/bin/python your-program.py
to use the version selected with python_select, or:
$ /opt/local/bin/python2.5 your-program.py
to explicitly select the MacPorts 2.5 one.
EDIT:
To modify your search PATH to use MacPorts, add this line to .bash_profile:
export PATH=/opt/local/bin:/opt/local/sbin:$PATH
First, I am not sure with Mac, coz I never use it before.
but in Linux, when I do whereis python
It will show like /usr/bin/python /usr/local/bin/python ....etc
in my .bashrc file, I just export PATH=/usr/local/bin:/usr/bin:$PATH when I want /usr/local/bin more priority
or you still can run like
/usr/bin/python yourpython.py
or
/usr/local/bin/python yourpython.py
depends on your python install locations
just my 2 cents. sorry if my answer dont make you any helps.
'python' in the Mac is just a link. Do a 'which python', 'cd' to the directory in which 'python' resides, and then do an 'ls -a py*'. You shall see where python is pointing too. If you want that python to point to your different version of python, just make it link to the right version.

Categories

Resources