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

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.

Related

Editors not honoring Python shell aliases

I run macOS Catalina with zshell.
Out of the box the os has one python2 and one python3 version in /usr/bin/python and /usr/bin/python3. I have installed a newer python3 via Homebrew. That version is in /usr/local/opt/python#3.8/bin/python3.
I have added aliases to my ~/.zshrc-file so that both python and python3 will launch into the 3.8 Homebrew version.
When using editors (e.g. Atom) that run python scripts by calling python3 this aliasing does not seem to work. I guess this is because it is specific to the terminal shell.
What is a better way of getting my homebrew python3.8 to become the default python on my system?
Don't uses aliases for selecting alternate programs. Use your PATH variable to manage your preferences.
Start by creating a local bin directory if you don't already have one.
mkdir -p ~/bin
Assuming your PATH is already set up to prefer Homebrew versions over system-installed versions, add ~/bin to the front of the path.
# In .bash_profile
PATH=~/bin:$PATH
Now, create a symbolic link ~/bin/python to the desired Python 3 interpreter.
ln -s /usr/local/opt/python#3.8/bin/python3 ~/bin/python
Now when you run python, you'll get your Homebrew python3.8 interpreter. You can still access the system Python 2 with /usr/bin/python when needed. Your editors should also inherit and respect your PATH variable, unless it is configured to use a specific hard-coded path.
Note that Homebrew still(?) links /usr/local/bin/python to its own Python 2 interpreter; I don't recommend changing that to python3, lest other Homebrew-managed programs get Python 3 when they require Python 2, hence the use of ~/bin. (There's still a chance that programs using python via path lookup will assume it is Python 2, but this should minimize the problems.)

Homebrew not linking python correctly?

I installed both python 2.7.13 and python 3.6.2 with homebrew and updated my ~/.bash_profile as such:
# Homebrew
export PATH=/usr/local/bin:$PATH
Python3 was linked fine. However, "which python" would still give me
/usr/bin/python
while "which python2" produces
/usr/local/bin/python2
It looks like homebrew installed python 2 as "python2" and never linked "python" to the new installation. This is causing me a lot of trouble down the road when installing virtualenvwrapper etc.
By the way, I also did
brew link python.
Anybody know why this is and how to fix it?
Much appreciated!!
They change that behavior here
Today I’d like to announce Homebrew 1.3.0. The most significant change
since 1.2.0 is that brew install python no longer installs a python
binary without manual PATH additions and instead installs a python2
binary. This avoids overriding the system python binary by default
when installing Python as a dependency. It also paves the way to
eventually have python be Python 3.x.
You will have to symlink python to the version of python installed by homebrew that you want.
You can do:
$ln -s /usr/local/bin/python2 /usr/local/bin/python
To symlink python to the homebrew Python2.x installation or do:
$ln -s /usr/local/bin/python3 /usr/local/bin/python
to link it to the Python 3.x hombrew installation.
gsi-frank's solution solves the problem quite well but after using his solution, you might encounter problems with your pip3. Your pip3 might be linked to the old instance and therefore unusable for your new python instance.
Type which pip3 to see where your pip3's path.
If your path is linked to your new instance, then ignore the rest of this answer.
Type echo $PATH to see your current path. If it contains your old python instance's path then you will need to remove it from your .bash_profile
To solve this: check your .bash_profile and see if there is a path to your old python instance.
If the old path is in there, you can delete it.
I had this problem and here's a link to the question if needed:
How to change pip3 path after installing python with homebrew?
To find your .bash_profile:
Go to your Finder
Right click and click Go to a folder... and type ~
Press CMD + shift + . to see hidden files
Right click your .bash_profile and open in a text editor

change python version in terminal and intelliJ

short Q,
In a mac OS sierra terminal,
If I do:
whereis python
/usr/bin/python
Then if I do:
/usr/bin/python it opens python 2.10
but if I execute
python it opens python 2.7.8.
and this one comes from
/Library/Frameworks/Python.framework/Versions/2.7/bin/python
How do I change the default python to point to python 2.7.8? In the terminal and in intelliJ?
Check the PATH environment variable with
echo $PATH
The python version you get when typing bare 'python' will be the first one found in that list of directories.
It is possible to control which python version is launched by, for example, rearranging the entries in PATH or by adding a symbolic link to the desired version in a position before the current version.
However, a more popular way to manage multiple python versions on the same machine is to use virtualenv. This will give you much less headaches when using pip to install/uninstall packages for particular python versions.
As wim mentioned you will get the first python that is found in $PATH.
A nice way may be to ln -s /Library/Frameworks/Python.framework/Versions/2.7/bin/python /usr/local/bin/python. This will create a symbolic link in /usr/local/bin
That way you don't change the order in your $PATH variable. Also note that if you echo $PATH, /usr/local/bin should be before /usr/bin - in case you have other versions in /usr/bin (which you do given your example)
I would strongly recommend you do what wim mentioned and use virtualenv to manage you packages.

What version of Python is on my Mac?

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

Trying to upgrade Python to 3.0 on Mac OS 10.5.8

I'm having some problems upgrading Python on my Mac. For my first attempt, I downloaded and installed the 2.6.4 dmg MacPython installer from http://python.org/download/mac/. This did install 2.6.4, and when I ran 'python' from the terminal it says that version.
However, I also had a test script where I am doing:
import os, json
But I get an error that the 'json' library was not found. In the script I included this shebang at the top to make it run from the terminal:
#! /usr/bin/python
I suspect that the symlinks that come directly from Apple that point to Python 2.5 were not updated by the 2.6.4 installer, so directly from the terminal 'python' is running the newer version, but my test.py file is executing 2.5.
So at this point I read a couple of other SO pages on doing this upgrade, and people recommended using 3rd party packages that sit side-by-side so as not to break the OS-level dependencies on v2.5. I then found ActivePython offered a 3.x installer(that was also recommended on another SO page). I installed that, but 'python' still shows 2.6.4 and my script still can't find the json library.
Finally, I'm baffled at how to safely remove MacPython( the Mac installer I mentioned above ). There's one sentence on the page that says to remove some things that seem pretty vital to Python on the Mac. Quote:
A MacPython 2.5 folder in your Applications folder. In here you find IDLE, the development environment that is a standard part of official Python distributions; PythonLauncher, which handles double-clicking Python scripts from the Finder; and the “Build Applet” tool, which allows you to package Python scripts as standalone applications on your system.
A framework /Library/Frameworks/Python.framework, which includes the Python executable and libraries. The installer adds this location to your shell path. To uninstall MacPython, you can simply remove these three things. A symlink to the Python executable is placed in /usr/local/bin/.
So now I have 3 versions of Python installed and I'm not sure how to resolve this stupid mess.
First, /usr/bin/python should always point to the Apple-supplied python and on 10.5 that means python2.5. Don't change this!
When you installed the python.org python2.6, by default it installs symlinks in /usr/local/bin/ so one way to invoke it is /usr/local/bin/python2.6 or, most likely, just python2.6. Since json was added to the python library in python 2.6, you'll find the json module is there. One way to solve your orignal problem then is to change the shebang line to be:
#!/usr/bin/env python2.6
Also by default, the python.org installer updates your shell profile to add its bin directory to your $PATH, which is why typing python probably now invokes python2.6.
You shouldn't need to but if you really want to remove all traces of the python.org 2.6:
Delete the extra lines at the end of your .bash_profile and/or .profile by reverting to .bash_profile.pysave and .profile.pysave.
Remove the python2.6 framework directory:
sudo rm -r /Library/Frameworks/Python.framework/Versions/2.6
Remove IDLE and the extras installed in /Applications:
sudo rm -r /Applications/Python\ 2.6
Also there's nothing wrong with moving on to Python 3. For the moment, both Python 2 and Python 3 are being actively developed; search the archives for the various pros and cons. However, Python 3.0 should not be used. Not surprisingly for something that major, Python 3.0 had a number of serious first-time bugs so, with the release of Python 3.1, 3.0 support was immediately dropped.

Categories

Resources