Homebrew macOS - Python installation - python

I've installed python via homebrew. It is located in:
/usr/local/Cellar/python/2.7.13_1
which should be right.
Now I am trying to use this python installation, but "which python" only shows the macOS python installation at "/usr/bin/python". So i am checking the $PATH and I see that everything should be ok.
"echo $PATH" results in this: /usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin
I restarted the terminal window and this occurs every time. I also did the
"brew doctor" and no warnings appeared.
What I am using:
Standard macOS Terminal-App
Has anybody a clue how this problem could be solved?

Update $PATH in your .bashrc file.
Example add the following line in your .bashrc
export PATH=/usr/local/Cellar/python/2.7.13_1/bin:$PATH

Either add the Python Homebrew prefix to your $PATH, like #Zico suggests in his answer, or link the Python executable into /usr/local/bin (which might already be in your path)
You'd do that with
$ brew link python
Good luck :)

Brew create an python2 alias to
/usr/local/Cellar/python/2.7.13_1/Frameworks/Python.framework/Versions/2.7/bin/python2.7
I just created a copy of python2 alias and rename it to python. That solved the problem

Related

python3.8 fails with "Fatal Python error: config_get_locale_encoding"

OK, so somehow I have mangled my python3 installation under macOS Mojave and I'm not sure how. I've used macports for years to keep python up to date but when I installed python38 now I cannot run python3 at all. I always get this:
$ python3.8
Fatal Python error: config_get_locale_encoding: failed to get the locale encoding: nl_langinfo(CODESET) failed
Python runtime state: preinitialized
$
I uninstalled the macports version and reinstalled, same thing. Uninstalled and then installed fresh from python.org, same thing.
python27 runs fine. python37 also runs fine. python38 won't even work if I use $python3.8 -I so it's not some site package weirdness.
Here's the really weird bit: while I cannot run python38 from a shell (any shell, tried from bash , I can launch python38 from the GUI using IDLE.app.
Oddly, on my other machine (my laptop), python38 installed with macports works just fine.
I'm flummoxed and I don't flummox easily. Any ideas?
Try setting LANG with a locale:
export LANG="en_US.UTF-8"
I had the same problem and fixed it by putting the following variable definition in my ~/.profile:
export LC_CTYPE="en_US.UTF-8"
LC_CTYPE was originally set to "De_DE" and was missing the encoding.
You should choose your language and country and set the value accordingly, e.g. this value for German/Germany:
export LC_CTYPE="de_DE.UTF-8"
There may be another not obvious problem. If you put export LC_CTYPE="en_US.UTF-8" in ~/.profile file, check if there's no ~/.bash_profile in the system. If ~/.bash_profile exists commands in ~/.profile will not be executed.

Install Python 3 to /usr/bin/ on macOS

I installed python2.x and python3.x using homebrew and the executable python paths are listed below:
$ which python
/Library/Frameworks/Python.framework/Versions/2.7/bin/python
$ which python3
/Library/Frameworks/Python.framework/Versions/3.5/bin/python3
It's quite too long and not so clean to write a shebang in a python code to make it runnable on Terminal:
#!/Library/Frameworks/Python.framework/Versions/2.7/bin/python OR
#!/Library/Frameworks/Python.framework/Versions/3.5/bin/python3
I prefer
#!/usr/bin/python OR
#!/usr/bin/python3
My issue here is, how can I correcly move or reinstall python on macOS to /usr/bin such as /usr/bin/python OR /usr/bin/python3 Instead of
/Library/Frameworks/Python.framework/Versions/2.7/bin/python
/Library/Frameworks/Python.framework/Versions/3.5/bin/python3
This is NOT possible on Mac OS X El Capitan anymore as from then on System Integrity Protection prevents that. More info in Cannot create a symlink inside of /usr/bin even as sudo
Create a symbolic link in /usr/bin/
Open terminal and do:
$ sudo ln /Library/Frameworks/Python.framework/Versions/2.7/bin/python /usr/bin/python
$ sudo ln /Library/Frameworks/Python.framework/Versions/3.5/bin/python3 /usr/bin/python3
You can now do what you wanted to do.
Edit: Unfortunately as you can read from the other answers, this solution no longer works on MacOS >= El Capitan due to System Integrity Protection. (See here)
A possible alternative is to use the folder /usr/local/bin that should be accessible.
The basic question in the OP seems to not be doable because the newer Mac OSes have "System Integrity Protection" which prevents "unauthorized" changes to key directories such as /usr/bin, and sudo cannot override that.
The suggestion of using /usr/local/bin and /usr/local/Frameworks seems like it should work (I haven't tried it). However, at https://opensource.com/article/19/5/python-3-default-mac, Matthew Broberg suggests that it is likely to create problems when updating.
In that same article, Moshe Zadka recommends using pyenv to manage Python environments and using shell aliases rather than symlinks.
I was going to go with the /usr/local suggestion above, but having found this contraindication, I'm going to try Moshe's method. I'll report back here if I hit any snags.
Update: I followed the method and I still was getting pip not found and which python and python -V were not giving me the expected results. Doh! I ran:
. ~/.bash_profile
(where I had put the recommended eval "$(pyenv init -)" command) and suddenly everything was finding the paths and versions as expected.

Error Installing OpenCV with Python on OS X

I have been trying to install Open CV 3 on my mac using this tutorial but I cannot get past step three.
So after I do
brew install python
I do
nano ~/.bash_profile
And the at the bottom of the script I paste
# Homebrew
export PATH=/usr/local/bin:$PATH
After that I reload the file like this
source ~/.bash_profile
Finally I check the python like this
which python
And it prints
/usr/bin/python
instead of
/usr/local/bin/python
I have also tried edited the file in TextEdit but it has the same result.
Am I doing something wrong or is this just a bad tutorial?
Thank You in Advance!
Edit:
# Setting PATH for Python 3.5
# The orginal version is saved in .bash_profile.pysave
PATH="/Library/Frameworks/Python.framework/Versions/3.5/bin:${PATH}"
export PATH
##
# Your previous /Users/UserName/.bash_profile file was backed up as /Users/UserName/.bash_profile.macports-saved_2016-07-26_at_12:50:19
##
# MacPorts Installer addition on 2016-07-26_at_12:50:19: adding an appropriate PATH variable for use with MacPorts.
export PATH="/opt/local/bin:/opt/local/sbin:$PATH"
# Finished adapting your PATH environment variable for use with MacPorts.
# Homebrew
export PATH=/usr/local/bin:$PATH
pydoc3.5
python3
python3-32
python3-config
python3.5
python3.5-32
python3.5-config
python3.5m
python3.5m-config
Is there a
/usr/local/Cellar/python/2.7.12/
directory? (Version number might differ.)
Is there a
/usr/local/bin/python
file?
If the Cellar directory is present, but the file isn't, then Homebrew decided to be careful and not put Python in /usr/local/bin/ immediately.
You could manually do
brew link python
and see if there's now a
/usr/local/bin/python
file.
In your case, it appears you have some files related to Python (they might be from a Python 3 installation, can't tell), such as 2to3. You can safely overwrite them, since Python 2 also has this.
Thus:
brew link --overwrite python
is fine.
Note:
Specific Python versions will always exist as python2.7, python3.5 etc (including the full path as necessary). Thus, even overwriting the python executable is safe (provided it's not the system one in /usr/bin): you should then simply be explicit which python executable to use.
Also, when using a tool like pip, you can make sure you're using the correct version by running it e.g. as
/usr/local/bin/pythnon2.7 -m pip <...>
or whatever python executable you want to install things for.
Okay so one brute force solution could be this one https://stackoverflow.com/a/9821036/128517
But maybe you could check the value of your $PATH after source ~/.bash_profile
typing
> echo $PATH
and see if /usr/local/bin is indeed at the beginning.
if it's not, you might need to check if there's another export before yours or maybe you need to edit .profile instead.

Conflicting Python Installs in OSX Mavericks

I've got myself in a pickle and would like some guidance before my laptop suffers GBH.
I have been using my Macbook for a few years without probs but when I got a new iMac at work I noticed everyone recommended useing homebrew for new Python installs (esp. on Mavericks).
Now my laptop has worked fine with the original Python. But I decided to try and do it the new homebrew way (its now Mavericks btw).
First I clear out Mavericks due to some other conflict then reinstall a fresh. Get nginx, php-fpm, mysql, etc working.
Now Python.
I can use pip to install packages. But when I try
workon myproject
I get:
/usr/bin/python: No module named virtualenvwrapper
/usr/bin/python: No module named virtualenvwrapper
But then it switches to that virtualenv anyway - but not the directory that the project is in!
When I run where python I get several entries:
/usr/local/bin/python
/usr/bin/python
/usr/local/bin/python
I've tried fiddling with my path but keep getting other errors that all seem to indicate my two Python installations are conflicting badly. Is there a simple solution or do I need to wipe out hombrew and start again from the beginning? Or just give up on homebrew and use OSX built in?
To clarify my current $PATH is:
/usr/local/bin:/usr/local/sbin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/op‌​t/X11/bin:/usr/local/go/bin:/Users/me/Development/Android/sdk/tools:/Users/me/Dev‌​elopment/Android/sdk/platform-tools
My solution was an unusual one but here goes:
In my .zshrc file I had the following layout:
ZSH=$HOME/.oh-my-zsh
ZSH_THEME="steeef"
plugins=(osx virtualenv virtualenvwrapper python github)
source $ZSH/oh-my-zsh.sh
# various aliases
export PATH=/usr/local/bin:/usr/local/sbin:$PATH
The problem was the Oh My ZSH plugin virtualenvwrapper was looking in the default python path location as the custom path had yet to be set later in the .zshrc file.
The solution therefore was to move the PATH declaration before the plugins like so:
ZSH_THEME="steeef"
export PATH=/usr/local/bin:/usr/local/sbin:$PATH
plugins=(osx virtualenv virtualenvwrapper python github)
source $ZSH/oh-my-zsh.sh
# various aliases
Now the ZSH plugins are referencing the correct Python install and therefore the correct Python packages path.
Try putting your preferred version earlier in your PATH variable. So if you want to use /usr/local/bin/python, from the command line you could modify your PATH by export PATH=/usr/local/bin/python:$PATH. See here for more info: http://www.cyberciti.biz/faq/appleosx-bash-unix-change-set-path-environment-variable/

How make Python 3 default on a Mac [duplicate]

Currently running Mac OS X Lion 10.7.5 , and it has python2.7 as default. In the terminal, i type 'python' and it automatically pulls up python2.7. I don't want that.
from terminal I have to instead type 'python3.2' if i want to use python3.2.
How do i change that?
The safest way is to set an alias in ~/.bashrc:
alias python=python3
That way you avoid breaking things for scripts relaying on python being python2.
You could edit the default python path and point it to python3.2
Open up ~/.bash_profile in an editor and edit it so it looks like
PATH="/Library/Frameworks/Python.framework/Versions/3.2/bin:${PATH}"
export PATH
If you have python 2 and 3 on brew. Following worked for me.
brew unlink python#2
brew link python#3 (if not yet linked)

Categories