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.
Related
I cannot link Python 3.8 over system Python 3.9.
I have few installed python versions (3.8, 3.10, etc) by brew and system Python 3.9.
P.S. I cannot uninstall system one (it does not appear in Applications).
$ python3 --version
Python 3.10.9
$ brew unlink python#$(python3 --version | grep -oE '[0-9]+\.[0-9]+'); brew link python#3.8
Unlinking /opt/homebrew/Cellar/python#3.10/3.10.9... 25 symlinks removed.
Warning: Already linked: /opt/homebrew/Cellar/python#3.8/3.8.16
To relink, run:
brew unlink python#3.8 && brew link python#3.8
$ python3 --version
Python 3.9.6
$ type python3
python3 is /usr/bin/python3
$ python3[press TAB]
python3 python3-config python3.10 python3.10-config python3.11 python3.11-config
$ python3.8 --version
zsh: command not found: python3.8
$ echo $PATH
/opt/homebrew/bin:/opt/homebrew/sbin:/usr/local/bin:/System/Cryptexes/App/usr/bin:/usr/bin:/bin:/usr/sbin:/sbin:/opt/X11/bin
Questions:
How to set brewed Python over system one?
How to uninstall system python? (not recommended)
How to link python in addition to python3?
Why there is no python3.8 available? (solved with brew reinstall python#3.8)
UPD:
python3.8 became available after brew reinstall python#3.8.
UPD 2:
It looks like there is no way to fix this properly. Must be a bug of 3.8 installer.
Here is workaround commands:
mkdir ~/bin/
ln -s /opt/homebrew/bin/python3.8 ~/bin/python
chmod u+x,og-rwx ~/bin/python
And this ~/bin to your PATH.
#micromoses described the same in Method #4.
How to set brewed Python over system one?
All you need to do is prepend /opt/homebrew/bin to the beginning of your path. Your $PATH variable seems to be correctly set, so if it is not working I'd try restarting your terminal.
If all else fails, you can also explicitly prepend /opt/homebrew/opt/python#VERION_NUMBER/bin to the beginning of your path.
How to link python in addition to python3?
If you run brew info python, Homebrew tells you this caveat:
Unversioned symlinks python, python-config, pip etc. pointing to
python3, python3-config, pip3 etc., respectively, have been installed into
/opt/brew/opt/python#3.10/libexec/bin
So, all you need to do is prepend /opt/brew/opt/python#VERSION_NUMBER/libexec/bin to the beginning of your path. Just add this line to the end of your zshrc, bashrc, or equivalent:
export PATH="/opt/brew/opt/python#VERSION_NUMBER/libexec/bin:$PATH"
This will make all the unversioned Python utils available to you.
It is best to avoid directly overriding (modifying) symbolic or hard links pointing to the system's python version. These might be used by system functions/scripts which are (hopefully) tested against that specific python binary. Changing the version (especially downgrading, but sometimes even upgrading) can break things for the OS.
For the same reason stated above, uninstalling/deleting the system's python should also be avoided.
I struggled myself with MacOS not having python as an executable, so I'll focus my answer on that.
Looks like you already found a way to resolve that.
I can think of 4 ways to get a python executable;
Method #1 - probably not what you want
The easy way to make python available in (z)shell on mac is to add an alias:
echo "alias python=python3" >> ~/.zshrc
With this, in all new zsh sessions (or where .zshrc is reloaded), running python will run the python3 executable in your PATH. You can also set the alias's target to be a full path, so point it to the python version you like (if, for example, one is installed using homebrew). There are two problems: changing the target requires reloading .zshrc, and this will only take effect when executing python from an open shell (will not work for executable scripts/entrypoints - more on that later).
Method #2 - optimal for homebrew
Unversioned python, pip and other such symlinks are installed in libexec/bin directories of brew packages. If you are using homebrew you can add these to your path, like so:
echo 'export PATH="$PATH:/opt/homebrew/opt/python#3/libexec/bin"' >> .zprofile
As homebrew uses symlinks for package versioning, you can replace python#3 in the above example with a more explicit version (e.g. python#3.8 or python#3.10).
Method #3 - requires admin privileges
If you have admin privileges, you can add a symlink in your path to the executable, like so (change /usr/bin/python3 to the brew's python executable you like):
sudo ln -s /usr/bin/python3 /usr/local/bin/python
While hardlinks will work as well, I recommend avoiding them as there might be a chance that it will negatively affect minor version upgrades by keeping the older executable (haven't tested myself).
The upside of this method is that it will probably work for all users and all applications, as it is extremely rare for applications to not include /usr/local/bin in the execution search path.
Method #4 - intermediary executable
This (much like method #2) probably won't work for applications that are not invoked by (z)shell (possibly your favorite IDE), but it is most flexible, and does not require admin privileges. It entails adding an intermediary binary, and adding that to your PATH. I usually create a bin directory in my homedir for such occasions, so I'll use that path in the examples. First, create the executable:
mkdir ~/bin
echo '#!/bin/sh\nexec /usr/bin/python3 "$#"' > ~/bin/python
chmod +x ~/bin/python
Now add ~/bin to your shell's path:
echo 'export PATH="$PATH:$HOME/bin"' >> .zprofile
Alternatively, .zshrc or .zshenv can be used, but additional precaution has to be taken to avoid repeatedly adding ~/bin to the path in subshells:
echo '_home_bindir="$HOME/bin"\n[ "${PATH##*:}" = "$_home_bindir" ] || export PATH="$PATH:$_home_bindir"' >> ~/.zshrc
Now, every new shell (or if using .zshrc - if it is reloaded) will have python executable in its path, and you can easily edit the file to point to your favorite python version.
Notes:
Method #2 is probably the best option if using homebrew, which is the case in question. Still, as I struggled with not having a python executable on a "clean" MacOS installation (without homebrew), I've added methods #1, #3, and #4, in case I'm not the only one not using homebrew's python.
Methods #2, #3, and #4, allow for executable scripts/entrypoints. By that, I'm referring to executable scripts with shebang, most commonly using the env executable:
#!/usr/bin/env python
These 3 methods will also honor additional python arguments (e.g. python -u).
As MacOS no longer comes with a python executable (only python3), I think that method #3 is fairly safe to use (at least from a perspective of not breaking OS scripts). As for $PATH manipulation, I wouldn't recommend prepending it with user-writeable paths (like ~/bin), as this may lead to a security issue, or worse - a debugging nightmare (of course, no one would ever create executables like ~/bin/ls or ~/bin/which, right?).
As for the specific shell, this will also work for bash by changing the relevant files (.z.. to .bash..) as long as the profile/rc files are properly loaded. For other shells (e.g. tcsh) the syntax needs to be adjusted.
I'm on a new Mac and installing Python. I believe I reinstalled my Python using Homebrew. The path, however, is unusual to me, and I'm wondering why it is located here:
/Library/Frameworks/Python.framework/Versions/3.9/bin/python3
I'm used to Python being somewhere like this, instead (which is also is):
/Users/user.name/Library/Python/3.9/bin
What is different about the first installation? Are there any special considerations if my Python is located here?
/Library/Frameworks/Python.framework/Versions/3.x/bin/python3 is the Python distribution shipped with macOS.
It's usually a good practice to create your own Python environment for your projects instead of using the default Python installation (consider reading about pyenv).
If you want to use your Homebrew installation, run brew info python to find your Python installation path, then append the parent directory's path to your PATH variable by editing ~/.zshrc (assuming you're using zsh, the default shell).
$ brew info python
....
Python has been installed as
/opt/homebrew/bin/python3
...
# inside ~/.zshrc
...
export PATH="/opt/homebrew/bin:$PATH"
....
I am attempting to install some Python libraries by executing variations of the following command in Bash:
pip install --user -U numpy
I installed Python3 using Homebrew.
I then get variations of the following message each time:
WARNING: The scripts f2py, f2py3 and f2py3.7 are installed in '/Users/x/Library/Python/3.7/bin' which is not on PATH.
Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.
How can I fix this issue to avoid problems in the future?
The error message is telling you to add Python 3 to your path.
To do that, use a text editor to open /Users/<you>/.profile, and as the very last line add:
export PATH=/Users/<you>/Library/Python/3.8/bin:$PATH
Then you'll need to launch a new Terminal window for those settings to take effect. (you could make it take effect in your current shell by entering the line directly into your shell)
[Edit: now that macOS 12.3 has removed all versions of Python, Homebrew is the easiest way to install Python. Fortunately things are simpler because there's only one version of Python on your system and you won't need to override the system's version (because there isn't one any longer).]
Update: As of python 3.8, the following path should be used:
export PATH=/Library/Frameworks/Python.framework/Versions/3.8/bin:$PATH
If you're using bash, you can store this in your /.bashrc
If you're using zsh, you can store this in your /.zshrc
You have to update the seeking path location, to needed bin folder, in your .zshrc, .bashrc etc.
Example
For /Library/Python/3.8/bin,
you can prepend the variable $HOME, and use with the needed path:
export PATH="$HOME/Library/Python/3.8/bin:$PATH"
Epilogue
In current Terminal tab, you have to reload your shell, with the config, by . ~/.zshrc, or .bashrc etc
I installed python3.5.0 in /opt/python3.5.0/bin/, but I couldn't use it beyond this bin folder. I know this is a path issue. Could somebody point out the correct procedure to make python3 show up in the system for use? The operating system is CentOS6.6
The os needs to know where your python executable is.
Try running it from somewhere else
cd
/opt/python3.5.0/bin/python -V
If that works the your Python3 installation in opt is ok and you just need to let your shell (probably bash) know where the python binary is by changing your PATH environment variable.
export PATH=/opt/python3.5.0/bin/:$PATH
Run that line on the bash terminal and try running python again with no path like this:
python -V
If that works put the export PATH=/opt/python3.5.0/bin/:$PATH line in your .bashrc or .bash_profile so it will work in the console in future.
Failing all that take a look at setting the PYTHONHOME and PYTHONPATH environment variables.
Also a better approach might be to install python3 using your package manager. The python3 executable should give you a version of Python-3 if you have one installed. Though you might get a different version of Python3 than 3.5 that way.
What's the best way to set up symbolic links to current installs, e.g python -> python2.6?
I've just installed python2.6 through Macports at /opt/local/bin/python2.6, I'd now like to set up a symbolic link called python here /usr/local/bin/. I then want to be able to add this line at the beginning of my pythons scripts so it knows where to look: #!/usr/local/bin/python. But what happens when I upgrade python to python2.7 for example, do I just need to remember to go to my symbolic link and change it? I guess I'll remember because it likely won't work anymore? Is there a better way to do this?
By default, MacPorts deliberately and carefully installs everything into a separate directory space: /opt/local. This ensures it does not conflict with anything installed as part of OS X or third-parties. To ensure that MacPorts-installed executables are found first, the recommended solution is to modify your shell PATH to put /opt/local/bin before /usr/bin.
MacPorts also provides a special port package, python_select, to manage which python version is pointed to by the command python in /opt/local/bin.
sudo port install python_select
sudo python_select
Then, to make your scripts use your current preferred python, the traditional solution is to use the env program in the shebang line of your scripts.
#!/usr/bin/env python
Symlink the version you use most.
When you need another version, run it by specifying the version number, e.g.:
$ python2.5 dev_appserver.py myapp
Not sure about OSX, here is what I do on Ubuntu 9.04:
>which python
#/usr/bin/python
Just replace that file with a sym link to the version of Python you actually want to use:
>sudo ln -s /usr/bin/python2.6/python /usr/bin/python