I had some issues with installing Python 3 on my Mac with OSX 10.12. Now I have stuck on point when Python is installed but is not properly linked.
I installed it via homebrew by brew install python#3.9 and command python3 gives me
pavelprdel#Pavels-Mac-mini ~ % python3
Python 3.9.7 (default, Sep 3 2021, 04:31:11)
[Clang 12.0.5 (clang-1205.0.22.9)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
But with command python I get
pavelprdel#Pavels-Mac-mini ~ % python
zsh: no such file or directory: /usr/local/bin/python3.9.7
pavelspurny#Pavels-Mac-mini ~ %
I have found solution via brew link --overwrite python
command but I only get this
Warning: Already linked: /opt/homebrew/Cellar/python#3.9/3.9.7
To relink, run:
brew unlink python#3.9 && brew link python#3.9
And relink doesn't work, I used which python command and it seems that it is installed in different location
python: aliased to /usr/local/bin/python3.9.7
I have also installed python from https://www.python.org/ but result is still the same.
Just link it manually:
# remove current link
unlink /usr/local/bin/python
# add new link
ln -s $(which python3) /usr/local/bin/python
Also check $PATH variable:
echo ${PATH}
...:/usr/local/bin:/usr/bin:/bin:/usr/sbin:...
/usr/local/bin must prepend /usr/bin because here is default python 2.x interpreter /usr/bin/python and /usr/local/bin/python must be called first
Also I don't recommend to override system's /usr/bin/python with python3, it should be reverted if already overridden
Check for upvoted 41 times warning (However that is not a so big problem for mac OS in compare to Linux OS):
Just a warning: Do not attempt to change the /usr/bin/python symlink to point to python3 instead of 2.7. Many programs available in the Ubuntu repos require /usr/bin/python to be compatible to python 2.x.
Anyway usually the python is used to run python 2.x and the python3 to run python 3.x. I recommend to use pyenv (for shell session scope) or virtualenv (for project scope) to be able override python with python3 only where it needed.
In other cases execute python 3 via python3 command is a normal practice
A better way to install python is by downloading it from the following link:
https://www.python.org/. Then, click on the download tab and select the newest version.
This should fix your problem. Installing via brew might be outdated and does not always work on OSX.
Related
I'm using WSL how I upgrade Python to the last version through the console?
Right now I have 3.8.10
I tried these steps and it worked.
Note: in step 3 you just need to change the version that you want to install
Installation steps
Run the following commands in your WSL terminal:
# Update package lists{
sudo apt update
# Install dependent libraries:
sudo apt install build-essential zlib1g-dev libncurses5-dev libgdbm-dev libnss3-dev libssl-dev libsqlite3-dev libreadline-dev libffi-dev curl libbz2-dev
# Download Python binary package:
wget https://www.python.org/ftp/python/3.10.8/Python-3.10.8.tgz
# Unzip the package:
tar -xzf Python-3.10.8.tgz
# Execute configure script
cd Python-3.10.8
./configure --enable-optimizations
# Build Python 3.10
make -j 2
# Install Python 3.10
sudo make install
# Verify the installation
python3.10
You should see:
Python 3.10.8 (default, october 15 2022, 14:44:10)
[GCC 6.3.0 20170516] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>
If you want to make Python 3.10 the default that runs when you type python, you can follow these steps:
Change ~/.bashrc file to add the following line:
alias python='/usr/local/bin/python3.10'
And then run the following command to have it take effect in the current shell:
source ~/.bashrc
You can verify it using the python --version command, which should now show:
Python 3.10.8
Originally found in this article.
A couple of notes -- First, to quote #juanpa.arrivillaga's comment:
You don't "upgrade" python, you just get a new interpreter
The word "upgrade", of course, means to replace one version with a later/greater version. You don't want to do this. So, so, so many packages in most Linux distributions rely on Python, and these distributions are usually heavily tested against a particular Python release. You don't mention which Linux distribution you are using in WSL, but since Ubuntu is the default, we'll assume that. To see a list of all packages that are currently installed in Ubuntu that depend on Python, run:
apt-cache rdepends python3 | grep -v python | less
From this Ask Ubuntu answer
What you really want to do, most likely, is have access to a second (or third) Python version for a specific use. There are multiple ways to do this. I would suggest reading the answers on How do I install a different Python version using apt-get? on Ask Ubuntu. There are some good methods there as well. My solution is to use a Docker Python image -- There are images available for every currently support Python release (and some unsupported Python2 releases, even) there is.
Thanks for the excellent instructions!
FYI: I had to manually add pip soft links to /usr/local/bin also. I was getting file not found when trying to install pytorch (on python3.10 for my case).
So currently, I have ubuntu 19. And it comes by default with python 3.7.5. I need to downgrade to 3.6.5.
EDIT:
I am using virtualenv
The following talks about upgrade from 3.6.7 to 3.7.0 but you can use the same process for downgrade. You should not change the system python unless you really know what you're doing
First Install Pyenv
Installlation Instructions are here
Look at Pyenv Options
$ pyenv
pyenv 1.2.14
Usage: pyenv <command> [<args>]
Some useful pyenv commands are:
commands List all available pyenv commands
activate Activate virtual environment
commands List all available pyenv commands
deactivate Deactivate virtual environment
doctor Verify pyenv installation and deevlopment tools to build pythons.
exec Run an executable with the selected Python version
global Set or show the global Python version
help Display help for a command
hooks List hook scripts for a given pyenv command
init Configure the shell environment for pyenv
install Install a Python version using python-build
local Set or show the local application-specific Python version
prefix Display prefix for a Python version
rehash Rehash pyenv shims (run this after installing executables)
root Display the root directory where versions and shims are kept
shell Set or show the shell-specific Python version
shims List existing pyenv shims
uninstall Uninstall a specific Python version
--version Display the version of pyenv
version Show the current Python version and its origin
version-file Detect the file that sets the current pyenv version
version-name Show the current Python version
version-origin Explain how the current Python version is set
versions List all Python versions available to pyenv
virtualenv Create a Python virtualenv using the pyenv-virtualenv plugin
virtualenv-delete Uninstall a specific Python virtualenv
virtualenv-init Configure the shell environment for pyenv-virtualenv
virtualenv-prefix Display real_prefix for a Python virtualenv version
virtualenvs List all Python virtualenvs found in `$PYENV_ROOT/versions/*'.
whence List all Python versions that contain the given executable
which Display the full path to an executable
See `pyenv help <command>' for information on a specific command.
For full documentation, see: https://github.com/pyenv/pyenv#readme
Look at Python Versions
$ pyenv versions
system
* 3.6.7 (set by /home/taarimalta/.pyenv/version)
Install a new Python
$ pyenv install 3.7.0
Installing Python-3.7.0...
WARNING: The Python bz2 extension was not compiled. Missing the bzip2 lib?
WARNING: The Python readline extension was not compiled. Missing the GNU readline lib?
WARNING: The Python sqlite3 extension was not compiled. Missing the SQLite3 lib?
Installed Python-3.7.0 to /home/taarimalta/.pyenv/versions/3.7.0
If you run into an issue with _ctypes install libffi-dev library
Now look at the versions
$ pyenv versions
system
* 3.6.7 (set by /home/taarimalta/.pyenv/version)
3.7.0
Select 3.7.0 for local environment
$ pyenv local 3.7.0
See that the version changed
$ pyenv versions
system
3.6.7
* 3.7.0 (set by /home/taarimalta/.python-version)
$ python
Python 3.7.0 (default, Jan 1 2020, 10:52:57)
[GCC 9.2.1 20191008] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>
Switch to a different folder
cd ../project2
pyenv versions
system
* 3.6.7 (set by /home/taarimalta/.pyenv/version)
3.7.0
The python version may be different here depending on which python version you have set locally
Set pyenv version globally
This globally sets a python version for a user
pyenv global 3.7.0
Note that pyenv sets local version by adding a .python-version file
$ pyenv local 3.7.0
$ cat .python-version
3.7.0
Note that pyenv knows the global version by looking at the ~/.pyenv/version file
cat ~/.pyenv/version
3.8.2
Install it from the pre-compiled Ubuntu deb package repos.
$ sudo apt update
$ sudo apt install software-properties-common
$ sudo add-apt-repository ppa:deadsnakes/ppa
When prompted, pres ENTER to continue.
sudo apt install python3.6
Alternatively, you can easily create a virtual environment.
Suppose you have python 3.8 (or higher) installed on the system, but for a specific task, you need python 3.7 (or lower). The best idea is to Create a virtual environment with python 3.7(or any 3.x, change the commands below according to your desired version. Below is an implementation of a virtual environment with python 3.7)
Steps: (Checked August 2022)
Install python 3.7 and it’s virtual environment packages.
sudo apt-get install python3.7-dev python3.7-venv
Find out where your python 3.7 is located by this command:
which python3.7 (Should be something like /usr/bin/python3.7, if not found, then install python 3.7 manually)
Create Virtual Environment in the Home directory.
cd
mkdir virtual_env
/usr/bin/python3.7 -m venv ~/virtual_env/venv_with_python3.7
source ~/virtual_env/venv_with_python3.7/bin/activate
python --version (Should be python 3.7 now)
Done. Python 3.7 can be used in this virtual environment. Type which python, and you’ll see you have created python 3.7 in a virtual environment, rather than in the system globally.
Run deactivate when you need to deactivate.
You can also use virtualenv to use different Python environments.
If you type python and tab twice or so, you might see a number of Python versions available. I say this because when I type
python3.6 -V
I have
Python 3.6.9
available. My alias for python is 3.7.5
When you type python you are probably referring to an alias defined in .bashrc
I'm using robotframework for automation. I'm trying to use a method from my python library which needs psutil. The problem is when I execute from the robotframwork it gives error ImportError: No module named psutil but when I execute it with Pycharm I don't get any error. I have installed psutil using pip install psutil . I searched a lot on web but couldn't find any reason for this issue.
My experience with this error was that I had multiple versions of python installed on my ubuntu server 16.04LTS.
My PATH resolved to /usr/local/python -> python 2.7.10 meaning typing python into the commandline I got 2.7.10, but the standard path /usr/bin/python linked to python 2.7.12. The psutil module and others were only installed for version 2.7.10
My resolution was to re-symlink /usr/bin/python to point to my 2.7.10 version:
# Find which python your PATH is pointing at and the version
$ which python
# Returns
/usr/local/bin/python
$ /usr/local/bin/python --version
# Returns
Python 2.7.10
$ /usr/bin/python --version
# Returns
Python 2.7.12
Unlink and relink to correct python version in /usr/bin/python
$ cd /usr/bin
$ sudo unlink python
$ sudo ln -s /usr/local/bin/python python
# check python version points correctly
$ /usr/bin/python --version
# Now correctly returns
Python 2.7.10
P.s. Bear in mind it's still worth checking all your modules are correctly installed now that your environment is pointing at the 1 python version
Could you try setting the PYTHONPATH in your environment or by passing it as an option.
This person had a different issue, but the fix I think will be the same:
setup pythonpath before starting test suite
Cheers,
K
When I ran Homebrew's brew doctor (Mac OS X 10.9.2), I get the following warning message:
Warning: Python is installed at /Library/Frameworks/Python.framework
Homebrew only supports building against the System-provided Python or
a brewed Python. In particular, Pythons installed to /Library can
interfere with other software installs.
Therefore, I ran brew install and followed the steps provided in the installation's caveats output to install Homebrew's version of Python. Running which python confirms that Homebrew's version of it is indeed at the top of my PATH. Output is /usr/local/bin/python.
Despite all this, when I rerun brew doctor, I am still getting the same warning message. How do I suppress this warning? Do I need to delete the /Library/Frameworks/Python.framework directory from my computer? Am I just supposed to ignore it? Is there a different application on my computer that may be causing this warning to emit?
Note that I don't have any applications in particular that are running into errors due to this warning from brew doctor. Also note that this warning message didn't always print out when I ran brew doctor, it was something that started to appear recently. Also, I am using Python 2.7 on my computer, trying to stay away from Python 3.
I also received this message.
Something, sometime installed
/Library/Frameworks/Python.framework
on my machine (the folder date was about 4 years old).
I've chosen to remove it.
Please note that the Apple provided framework lives in
/System/Library/Frameworks/Python.framework/
I had the same problem. When I upgraded python3 through Homebrew, I started getting this:
-bash: python3: command not found
I had the same conflict with Python somehow being installed in /Library/Framework/Python.framework. I just did a brew link overwrite and everything is working fine now. There is some info about what to do with the Python version in the /Library/Framework/Python.framework here.
I guess you could try deleting that version as the link suggests, just make sure that version isn't being used. When I got into the Python.framework directory I was seeing some EPD version of Python, which I think is Enthought. You could delete it, but I if it isn't causing you any problems besides the unsightly Homebrew warning message, then I think you should just ignore it for now.
Update:
I did delete the Python.framework directory which, through some poking around inside that directory, I started seeing a few old versions of Python that I didn't install with Homebrew. One was from Enthought, and another was a distribution of Python3.3. I think some of these installs in the Framework directory are user installs. I installed R on my system, and there is also an R.framework directory, so I think most of these are user installs. After I deleted the directory, I just had to call brew prune to remove the old symlinks. I checked both brew versions of python 2.7.6 and 3.3.4, and they seem to be in good working order with all of my installed packages. I guess I leave the decision to remove that directory, or python version, to your discretion.
per this thread, enter this command:
sudo rm -rf /Library/Frameworks/Python.framework
because there are multiple installations of Python on your computer, and this removes the one that may cause additional problems in the future.
You can use this solution as I've put below
sudo rm -rf /Library/Frameworks/Python.framework
brew doctor
brew prune
brew update
That combo fixed it for me, even thought this error usually doesn't cause any major problems its just was annoying me to see them pop up under brew doctor
Removing directories manually can be a nightmare, but fortunately 'brew' can take care of that. Once you are done with the removal, put this:
brew doctor
The above command will list the broken system links. In order to get rid of these broken symlinks, put this:
brew prune
Check with 'brew doctor' once more to ensure no links are broken. Your system will then be ready to brew.
I had installed Python 3 from Homebrew and then another Python 3 directly from the Python website.
I deleted /Library/Frameworks/Python.framework. I did not add any PATH or PYTHONPATH variables in .bash_profile. Then I checked the following in the shell:
~$ python
Python 2.7.10 (default, Feb 7 2017, 00:08:15)
[GCC 4.2.1 Compatible Apple LLVM 8.0.0 (clang-800.0.34)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> quit()
~$ python3
Python 3.6.4 (default, Mar 9 2018, 23:15:12)
[GCC 4.2.1 Compatible Apple LLVM 9.0.0 (clang-900.0.39.2)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> quit()
~$
In addition, I checked the following:
~$ whereis python
/usr/bin/python
~$ whereis python3
~$ which python
/usr/bin/python
~$ which python3
/usr/local/bin/python3
~$ which pip
/usr/local/bin/pip
~$ which pip3
/usr/local/bin/pip3
~$ whereis pip
~$ whereis pip3
I was a bit worried that whereis python3, whereis pip, and whereis pip3 returned nothing, but since there was no problem launching Python, I'd think it's safe to delete /Library/Frameworks/Python.framework. Ask me again in a month.
I try to switch to Homebrew (after using fink and macport) on Mac OS X 10.6.2. I have installed python 2.7 with
brew install python
The problem is that, contrary to Macport, it seems that there is no python_select utility, and my default mac python is always default
which python
give me
/usr/bin/python
and /usr/bin/python is not a symlink
How can I do to make python brew flavour to be my default python ?
As suggested by the homebrew installer itself, be sure to add this to your .bashrc or .zshrc:
export PATH="/usr/local/opt/python/libexec/bin:$PATH"
As you are using Homebrew the following command gives a better picture:
brew doctor
Output:
==> /usr/bin occurs before /usr/local/bin This means that system-provided programs will be used instead of those provided by
Homebrew. This is an issue if you eg. brew installed Python.
Consider editing your .bash_profile to put: /usr/local/bin ahead of
/usr/bin in your $PATH.
See: How to symlink python in Homebrew?
$ brew link --overwrite python
Linking /usr/local/Cellar/python/2.7.3... 28 symlinks created
$ which python
/usr/local/bin/python
Quick fix:
Open /etc/paths
Change the order of the lines (highest priority on top)
In my case /etc/paths looks like:
/usr/local/bin
/usr/local/sbin
/usr/bin
/bin
/usr/sbin
/sbin
If you want to know more about paths in OSX I found this article quite useful:
http://muttsnutts.github.com/blog/2011/09/12/manage-path-on-mac-os-x-lion/
I did "brew install python" for OSX High Sierra. The $PATH had /usr/local/bin before any other path but still which python was pointing to the system's python.
When I looked deeper I found that there is no python executable at /usr/local/bin. The executable is named python2. To fix this problem create a symbolic link python pointing to python2:
/usr/local/bin $: ln -s python2 python
For Apple Silicon machines, the path are slightly different. After running brew install python, you must ensure your ~/.zshrc uses the correct Homebrew paths:
# Homebrew
eval "$(/opt/homebrew/bin/brew shellenv)"
# Homebrew: Python
export PATH="/opt/homebrew/opt/python/libexec/bin:$PATH"
Results:
% which python
/opt/homebrew/opt/python/libexec/bin/python
% python --version
Python 3.9.9
% which pip
/opt/homebrew/opt/python/libexec/bin/pip
% pip -V
pip 21.3.1 from /opt/homebrew/lib/python3.9/site-packages/pip (python 3.9)
python formula now uses python3(v3.6.5 for now), brew will link the directory:
/usr/local/opt/python -> ../Cellar/python/3.6.5
it will also link the binary:
/usr/local/bin/python3 -> ../Cellar/python/3.6.5/bin/python3
If you still need to use python2.x, use:
brew install python#2
To use homebrew's python, just put its directory in PATH, for bash:
export PATH="/usr/local/opt/python/libexec/bin:$PATH"
for fish:
set -x PATH /usr/local/opt/python/libexec/bin $PATH
Note:
doing this will shadow the system default version of python
homebrew used to link python to /usr/local/share/python in older versions.
Homebrew does NOT replace stuff in "/usr/bin". You'll just want to put "/usr/local/bin" ahead of "/usr/bin" in your path, then "which python" will give you "/usr/local/bin/python".
Replacing /usr/bin/python (or /usr/bin/ruby) is highly unrecommended.
Modify your $PATH, Add this in your bashrc or bash_profile:
export PATH=/usr/local/bin:/usr/local/sbin:~/bin:$PATH
more click here:
Issue #89791
I did brew install python, my $PATH was good, but still, which python gave me the system installed one. Restarting the terminal fixed it.
You need to edit your PATH environmental variable to make sure wherever the homebrew python is located is searched before /usr/bin. You could also set things up in your shell config to have a variable like PYTHON be set to your desired version of python and call $PYTHON rather than python from the command line.
Also, as another poster stated (and especially on mac) DO NOT mess with the python in /usr/bin to point it to another python install. You're just asking for trouble if you do.
python now points to python3, if you need python 2 then do:
brew install python#2 and then in your .zshrc or .bashrc file
export PATH="/usr/local/opt/python#2/libexec/bin:$PATH"
Now, pyhon --version = Python 2.7.14 and python3 --version = Python 3.6.4.
That's the behavior I'm used to seeing in my terminal.
I believe there are means to make homebrew python default, but in my opinion the proper way to solve a problem is not to mess with system python paths: it is better to create a virtualenv in which homebrew python would be default (by using virtualenv --python option). Using tools like python_select is almost always a bad idea.
Use pyenv instead to install and switch between versions of Python. I've been using rbenv for years which does the same thing, but for Ruby. Before that it was hell managing versions.
Consult pyenv's github page for installation instructions. Basically it goes like this:
- Install pyenv using homebrew. brew install pyenv
- Add a function to the end of your shell startup script so pyenv can do it's magic. echo -e 'if command -v pyenv 1>/dev/null 2>&1; then\n eval "$(pyenv init -)"\nfi' >> ~/.bash_profile
Use pyenv to install however many different versions of Python you need. pyenv install 3.7.7.
Set the default (global) version to a modern version you just installed. pyenv global 3.7.7.
If you work on a project that needs to use a different version of python, look into pyevn local. This creates a file in your project's folder that specifies the python version. Pyenv will look override the global python version with the version in that file.
Add the /usr/local/opt/python/libexec/bin explicitly to your .bash_profile:
export PATH="/usr/local/opt/python/libexec/bin:$PATH"
After that, it should work correctly.
Just do:
brew install python
brew link python
After doing that, add this to your bashrc or bash_profile:
alias python='/usr/local/bin/python2'
Enjoy!
You can edit /etc/paths. Here is mine:
/usr/local/bin
/usr/bin
/bin
/usr/sbin
/sbin
Then add a symlink for the python version. In my case
$ cd /usr/local/bin
$ ln -s python3 python
Voila!
If you are fish shell
echo 'set -g fish_user_paths "/usr/local/opt/python/libexec/bin" $fish_user_paths' >> ~/.config/fish/config.fish
try this
which python3
Try typing python3 instead of just python
Greeting folks! I have the need to use python 3.10 version to harness its new features. My solved solution at 2022-07-03 is as follows. Have fun coding python!
» rm '/usr/local/bin/pip3.10'
» brew link python#3.10
» echo 'export PATH="/usr/local/opt/python#3.10/bin:$PATH"' >> ~/.zshrc
» python3
Python 3.10.5 (main, Jun 23 2022, 17:15:25) [Clang 13.1.6 (clang-1316.0.21.2.5)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
Since High Sierra, you need to use:
sudo chown -R $(whoami) $(brew --prefix)/*
This is because /usr/local can no longer be chowned
brew link python
And you must create/add an alias for python and put it in your .zprofile (Located in Users/username folder, if you press Shift+command+.
This must point to your homebrew python installation location.
alias python ='opt/homebrew/bin/python3'
No idea what you mean with default Python. I consider it bad practice to replace the system Python interpreter with a different version. System functionality may depend in some way on the system Python and specific modules or a specific Python version. Instead install your custom Python installations in a safe different place and adjust your $PATH as needed in order to call you Python through a path lookup instead of looking for the default Python.