Installing Python using Homebrew - python

I'm currently following this guide to install python and its libraries onto my Mac OS. http://www.nyx.net/~mlu/pages/computing/installing_and_configuring/installing_and_configuring_Python_on_Mac_OS_X/#.WCLNgeErJE4
In step 4, I brew installed python and python3
Upon checking the path of python and python3, I found that python was in /usr/bin/python while python3 is in /usr/local/bin/python3
How do I get my python to install in /usr/local/bin?
UPDATE
Upon looking on my terminal, I can see that the brew install python had installed it in /usr/local/bin (version is 2.7.12) but somehow I am accessing a python that was installed in /usr/bin (version is 2.7.10). How do i fix this?

Related

Why does my WSL Ubuntu point to a different Python Version?

This is the first time I have installed Ubuntu and Python in my Windows laptop, and upon checking, it seems that my Python version is 3.8.5.
python3 --version
Ubuntu WSL
However, when I check my cmd, and run
python --version
I'm getting 3.7.9. Just curious as to what the difference is as I don't remember installing 3.8.5
CMD
Because these are different pythons.
Try to run
> where python
You'll probably get something like
C:\Users\user\AppData\Local\Programs\Python\Python37\python.exe
C:\Users\user\AppData\Local\Microsoft\WindowsApps\python.exe
And then run
$ which python3
inside your WSL. You may get
/usr/bin/python3
which is different from Widows' executable. WSL has its own filesystem which doesn't share files with the parent system and python executables also aren't shared.
When you install a package / program in wsl, you installing it separately, this mean you not installing on your local os storage, but in wsl environment where you can access it.
You are installed python on own local machine, for example python 3.9, if you will install just python
sudo apt-get install python you will install python 2.7, this mean you are installing python on wsl, and another python have in own os, if you want to have the same version, try to install on your os and wsl the same version
in ubuntu you can do :
sudo apt-get install python( for python 2.7 )
sudo apt-get install python3.9( for python 3.9.5 )

Switching Python version installed by Homebrew

I have Python 3.8 and 3.9 installed via Homebrew:
~ brew list | grep python
python#3.8
python#3.9
I want to use Python 3.9 as my default one with python3 command. I tried the following:
~ brew switch python 3.9
Error: python does not have a version "3.9" in the Cellar.
python's installed versions: 3.8.6
I tried to uninstall Python and reinstall it, but it's used by other packages:
~ brew uninstall python
Error: Refusing to uninstall /usr/local/Cellar/python#3.8/3.8.6
because it is required by glib and php, which are currently installed.
You can override this and force removal with:
brew uninstall --ignore-dependencies python
How can I use Python 3.9?
There is an Homebrew known issue related to side by side install of Python 3.8 / 3.9. To workaround, following commands should work for you:
brew unlink python#3.9
brew unlink python#3.8
brew link --force python#3.9
Re-opening your terminal or execute command rehash can be required to take account the change.
Use pyenv. It's a software that lets you switch between any and all Python versions installed on your system. To install pyenv, use the following code in the command-line:
curl https://pyenv.run | bash
exec $SHELL
Then find the name of the python version you want to switch to with this:
pyenv versions
And select it with this:
pyenv global <version-name>
In your case, it's most likely named 3.9.0.
Updated MacOs Monterrey
For who are facing this problem add the pyenv path to your ~/.zshrc shell file.
export PATH="/Users/username/.pyenv/shims:${PATH}
eval "$(pyenv init --path)
Run in the terminal:
source ~/.zshrc
Check it out:
python3 --version
Font from the issue on GitHub.

How do I deal with pip in different python versions?

I had python 3.8 installed and then I installed python 2.7. I am trying to run a python program with py -2 program.py in vs code using with python 2.7 as selected environment and I am getting an error, ImportError: No module named googlemaps even though I have already installed.
If I run the program using Python3 then it would run fine. Also when I open vs code using python 2.7 as selected runtime environment then I would get a warning Linter Pylint is not installed. If I click on install then I would get another warning There's no Pip installer available in the selected environment.
Also even though I have changed the python path from 3.7 to 2.7, default python version will still show up as 3.7 when I runPython in command line.
Things that I have tried to install the googlemaps module for python 2 after googling for solutions,
pip2 install googlemaps--upgrade
py -2 -m pip install googlemaps
If you have your python2 binary located, you can just call it directly:
/usr/bin/python2 -m pip install googlemaps
And if you're not sure where your python binary is, you can use
import sys
print(sys.executable)
to locate it.
And if you don't have pip, you should install it by downloading this file:
https://bootstrap.pypa.io/get-pip.py
then running:
/usr/bin/python2 get-pip.py
It is recommended to install Python 3.8 using Pyenv and also when you are using different versions of python, it is very useful
curl https://pyenv.run | bash
pyenv install 3.8.1
pyenv virtualenv 3.8.1 venv
pyenv local venv
with pyenv local you set your version for use.
If after this you run
pyenv version
It will output to 3.8.1
With regards to pip installation, run
whereis python
and if it outputs to
usr/bin/python2
then you can use pip for installing python2 packages and pip3 for packages compatible to python3.

How to fix broken python 2.7.11 after OSx updates? An in-depth look into Homebrew and beyond

What happened:
After an OSx update and installing a new version of python 2.7 my virtualevn environment completely broke and I struggled in fixing it. I wasn't sure what caused it and went through a whole set of things that I did and read initially that didn't work are listed below. What solved my problem is provided in the answer section.
What didn't work to fix virtualenv command not found:
Installed python through homebrew and then used pip to install virtualenv
Installed python through https://www.python.org and then used pip to install virtualenv
Related questions that helped me but did not provide the solution to my problem:
virtualenv-command-not-found
virtualenv-workon-command-not-found
Complete manual recovery I went through (What not to do!):
This didn't completely solved my problem. It is just to give you an idea of what steps I went through before I found the correct way to fix my python dev environment on my OSx.
Removed python 2.7 by using the post in here
Removed the homebrew installed version
Installed python through the pkg file in Mac OS X 32-bit i386/PPC installer or Mac OS X 64-bit/32-bit installer
Manually installed virtualenv following the instructions from here:
curl -O https://pypi.python.org/packages/source/v/virtualenv/virtualenv-13.1.2.tar.gz
tar xvfz virtualenv-13.1.2.tar.gz
cd virtualenv-13.1.2
sudo python setup.py install
Manaully install pip through 7:
curl --silent --show-error --retry 5 https://bootstrap.pypa.io/get-pip.py | sudo python2.7
PIP was still broken after all this:
After all this after creating a virtual environment my pip still installed the packages in the main python folder instead of installing them under the virtual environment and non of the threads here neither here helped. My solution to that was to run pip under my virtual env with the following options:
1- Activate the virtual environment so that $VIRTUAL_ENV is set:
source venv/bin/activate
2- Forces pip to install in the right destination:
pip install --target=$VIRTUAL_ENV/lib/python2.7/site-packages
Summary
Something was badly broken and best way I fix my dev environment is provided in the answer to this question.
The reason
In my case was an OSx upgrade that affected my homebrew and after upgrading to python 2.7.11 is didn't install it properly.
How I got it to work:
I found steps 3 and 4 in a thread here and many thanks to https://github.com/baronomasia.
1 - Removed python 2.7 by using the post in here
2 - Removed the homebrew python installed version
brew uninstall python
3- Reinstall your Xcode command tools:
sudo xcode-select --install
4- Upgrade homebrew and reinstall python through homebrew:
brew update && brew reinstall python
After doing brew upgrade python my system python was broken and was throwing fits about virtualenvwrapper.sh, as well as my pip command was just suddenly missing.
I went to python.org and downloaded the python 2.7.13 installer, ran it, I now have python 2.7.13, pip, and can run pip install virtualenvwrapper and things seem to work.

Installing pip with correct python version

I am on shared hosting and I need to install pip with the correct python version, 2.7. To install pip, I did:
$ easy_install pip
However, after it was installed I get the following:
[dave#web1 lib]$ pip --version
pip 1.0.2 from /home/premiere/dave/financials/lib/pip-1.0.2-py2.7.egg (python 2.4)
How would I re-install pip to work on the python2.7 version, which is also installed on the machine?
[premiered#web1 ~]$ python --version
Python 2.6.6
Which is strange, since it is installing to python2.4.
You may want to create a virtualenv using -p /path/to/python-2.7.binary param, and then activate it. Then all stuff you installed using pip would be correctly into your virtualenv.
If multiple versions of python are installed on the system, then you should invoke the version you want when installing. i.e.
$ python27 easy_install pip
This creates a pip file in your path that contains the specified version of python in the hashBang line.

Categories

Resources