Inconsistent python3 and pip3 on OSX - python

I cannot figure out how to get the correct version of pip3 on my Mac as the default. I installed and use Python3 via brew:
❯ which python3
/usr/local/bin/python3
❯ ls -l /usr/local/bin/python3
<ELIDED> /usr/local/bin/python3 -> ../Cellar/python#3.9/3.9.10/bin/python3
but pip3 defaults to what looks like the Mac default Python:
❯ pip3 --version
pip 20.2.3 from /Applications/Xcode.app/Contents/Developer/Library/Frameworks/Python3.framework/Versions/3.8/lib/python3.8/site-packages/pip (python 3.8)
How would I go about fixing this?

On linux you have the update-alternatives command, I don't know if you have it on OSX.
Otherwise You can create a symbolic link.
Or use python -m pip

Related to Make python3 as my default python on Mac.
It is necessary to add the path to the additional tools. e.g., in ~/.zshrc add:
export PYTHON_HOME=$(brew --prefix)/opt/python/libexec
export PATH=$PYTHON_HOME/bin:$PATH
However, the following will not work:
export PYTHON_HOME=$(brew --prefix)/opt/python/libexec/bin
export PATH=$PYTHON_HOME:$PATH
For some reason, part of the path will get cannibalized.

Try running these at startup in terminal before you run python:
alias python=/usr/local/bin/python3
alias pip=/usr/local/bin/pip3

Related

issue when attempting to install python library using pip [duplicate]

How can I use pip in oh-my-zsh? I was trying to install nltk through pip, but it told me zsh: command not found: pip. When I check plugins under .oh-my-zsh/custom/plugins, there is a folder named pip. I don't know what the problem is.
Edit:
$ echo $PATH
/home/xxx/bin:/usr/local/bin:/home/xxx/bin:/home/xxx/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin
$ type pip
pip is an alias for noglob pip
Maybe you have installed both python2 and python3. python3 may have been installed later.
You may try to use pip3 instead of pip.
First, input the command:
pip3 -V
If you see the version, the pip3 can be used.
Then you can input command line to install nltk:
pip3 install nltk
I got a way to help you use pip in zsh. We can use nano to edit files. In nano, ctrl+X to save and exit
In the ~ directory, input the command:
nano .bash_profile
You may see some codes like:
# Setting PATH for Python 3.5
# The original version is saved in .bash_profile.pysave
PATH="/Library/Frameworks/Python.framework/Versions/3.5/bin:${PATH}"
export PATH
Copy them and paste them to the end of .zshrc file by using command:
nano .zshrc
Then input the command:
pip -V
If you see the version, pip can be used.
In case you do
which pip
and it doesn't show the path, just do
which pip3
This will print the path which is /usr/local/bin/pip3
Then do open ~/.zshrc or nano ~/.bash_profile.
Make alias for pip like:
alias pip=/usr/local/bin/pip3
N.B: You copy that line above and paste in your .zshrc file.
After do source ~/.zshrc and close .zshrc
For me it's working to do
python -m pip install [package_name]
instead of
pip install [package_name]
If you installed python3.x, you should run with pip3(not pip)
So you are using oh-my-zsh framework for zsh or Z shell.
First, try the command:
pip3 -V
If you get something like this below, that means you have the pip3 package already and must be having python3 as well.
pip 22.0.4 from /Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/pip (python 3.10)
Then edit your .zprofile instead of .bashprofile as you are using zsh. This is the command.
nano ~/.zprofile
Then it should have the two alias like this.
# Setting PATH for Python 3.10
# The original version is saved in .zprofile.pysave
PATH="/Library/Frameworks/Python.framework/Versions/3.10/bin:${PATH}"
export PATH
alias python=python3
alias pip=pip3
Make sure you save it. Exit and Re-open you terminal. Type the command:
pip -V
It should have the same result as the pip3 -V like this:
❯ pip -V
pip 22.0.4 from /Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/pip (python 3.10)
Then you can use pip or pip3 interchangeably for installing your nltk package like this.
pip install nltk
I'm on MacOS and use ZSH. It seems pip 2.7 can't be found, although it is installed. I believe my paths to "pip" are linked wrong (I also have python3 and pip3 installed via brew).
To get around the issue I created an alias. If you don't have an .aliases file, create one in your homedir. Then open the file:
nano ~/.aliases
and add:
## PIP for python2.7 ##
alias pip="python -m pip "
You need to tell ZSH to pick up the alias file (assuming you don't have this setup already). Open your .zshrc:
nano ~/.zshrc
The add the following near the bottom of the file:
[ -f "$HOME/.aliases" ] && source "$HOME/.aliases"
From the terminal, run:
source ~/.zshrc
Or quit your terminal and reopen it.
Now you can run:
pip install <command>
Edit your rc file:
vim ~/.zshrc
Find the config plugins and delete the pip entry.
In a new terminal:
which pip
This will show you the real path of pip
If you're running into this issue, it probably is due to versioning complications. Python 2 versus Python 3 on your OS may be resolving unexpectedly. Below is a quick workaround to get you to functioning behavior.
Try using the below for Python 2:
python -m pip install <command>
Try using the below for Python 3:
pip3 install <command>
In my case my OS was Ubuntu 20.04 and the pip doesn't come with python.
So, i've installed pip through the command
sudo apt install python3-pip
and I'm done.
To ensure run pip -V or pip3 -V
My pip script is missing for some reason, so I have to install it.
$ python -m ensurepip --upgrade
More methods can be found here:pip installation
You should consider upgrading.
Enter this in your terminal
/Library/Frameworks/Python.framework/Versions/3.10/bin/python3.10 -m pip install --upgrade pip
and then...
Type: pip -V

How to set Python's default version to 3.0.0 on Mac

The Python's default version is 2.7.0 on my MacOS,
python --version
Python 2.7.16
python3 --version
Python 3.9.9
and Python3 has been installed via homebrew, how to make it as default? I want to make it point to the correct directory instead of using alias.
type -a python
python is /usr/bin/python
type -a python3
python3 is /usr/local/bin/python3
python3 is /usr/local/bin/python3
python3 is /usr/local/bin/python3
python3 is /usr/bin/python3
Also, it would be very grateful if you can help with pip also! So far only pip3 has been installed.
type -a pip
pip not found
type -a pip3
pip3 is /usr/local/bin/pip3
pip3 is /usr/local/bin/pip3
pip3 is /usr/local/bin/pip3
pip3 is /usr/bin/pip3
I don't know why my python2 exists in /usr/bin, where ln is not permitted.
Generally doing this can impact legacy programs reliant on Python 2.x, but the below was taken from a website to demonstrate how to change the default.
How to set Python3 as a default python version on MacOS?
This has been answered on this website, and the below are snippets from the website
https://dev.to/malwarebo/how-to-set-python3-as-a-default-python-version-on-mac-4jjf#:~:text=Change%20Python%20Version%20MacHow%20to%20set%20Python3%20as,how%20it%20should%20be%20done%3A%20More%20items...%20
Look where python is installed & view the output.
ls -l /usr/local/bin/python*
Change the default python symlink to the version you want to use from above.
Note that, we only need to choose the one that end with python3.*. Please avoid using the ones' that end with config or python3**.***m or python3.*m-config. Below command shows how it should be done:
ln -s -f /usr/local/bin/python3.7 /usr/local/bin/python
Close the current terminal session or keep it that way and instead open a new terminal window
python --version

pip not found via zsh yet shows up via python3 -m pip --version

So, I've used homebrew to install python on my MPB (Catalina OS) and currently have both the system python (2.7.16) and python3 (3.9.0). Using "which" I get the following:
which python => /usr/bin/python
which python3 => /usr/bin/local/python3
When I type "pip list" I get
pip list => zsh: command not found: pip
Yet if I type the following I get this:
python3 -m pip --version =>
pip 20.3.1 from /usr/local/lib/python3.9/site-packages/pip (python 3.9)
What's going on that pip (and pandas) are installed within the python3.9 directory but zsh doesn't find them? It is an 'alias' issue I need to fix? I'm concerned to mess with the system python 2. Help!
Create a symlink in one of the directories in your PATH variable or manipulate the PATH variable in your shell environment:
Look it up by echo $PATH - my result would be:
/usr/local/sbin:/usr/local/bin:...
Now I can create a symlink in one of my directories by:
ln -s /usr/local/lib/python3.9/site-packages/pip /usr/local/bin/pip
or add /usr/local/lib/python3.9/site-packages/ to my PATH by
PATH=$PATH:/usr/local/lib/python3.9/site-packages/; export PATH
Then you would be able to find pip and execute it in zsh.

Make the 'python' phrase refer to Python 3 on Linux?

So whenever I run python -v it says I'm using Python 2, why is python reserved for Python 2 on Ubuntu, and is there a way to make python reserve python 3? The latest version.
For example, Id prefer to write
`python /files/app.py`
To execute my files than write
`python3 /files/app.py`
It seems messy to have to add the 3, you don't on Windows, so why is Ubuntu different?
You can use an alias to do this. Add the following line to your .bashrc:
alias python=python3
Don't forget to reopen your terminal, or do source ~/.bashrc for the changes to take effect.
Duplicate of this post.
Ubuntu does this because you probably have two versions of Python installed, a system variant and a 3.x variant you are using. You can try python --version and python3 --version to see. If they return different things, you have two installations.
For example:
$ python --version
Python 2.7.6
$ python3 --version
Python 3.4.3
$ alias python=python3
$ python --version
Python 3.4.3
To circumvent the alias use the command built-in command:
$ command python --version
Python 2.7.6
Another way to circumvent the alias is to use \ before the command.
$ \python --version
Python 2.7.6
To disable the alias in the current shell use the unalias built-in command:
$ unalias python
$ python --version
Python 2.7.6
As already stated here, you have system python and user python and one option is to create aliases for python and pip on a user level ~/.bashrc
Another option is to create a virtual environment for each project you work on. The advantage of this is that you only install modules relevant to each project.
Create a virtual environment
mkdir someproject
cd someproject/
python3 -m venv venv
Activate environment
source venv/bin/activate
Check python and pip version
$ pip -V
pip 9.0.1 from ~/someproject/venv/lib/python3.6/site-packages (python 3.6)
$ python -V
Python 3.6.7
Deactivate environment
deactivate
Python, pip and any other modules you decide to install is installed in the venv folder instead of system wide.
Only gotcha is to remember to activate your environment.

I want to make command python3 to run python3.7

I am currently using Mac OS Mojave version 10.14.2
I just installed python3.7 and uninstalled python3.6.
Then, the following result occurs
$ python3
-bash: /Library/Frameworks/Python.framework/Versions/3.6/bin/python3: No such file or directory
$ which python3
-bash: type: python3: not found
$ which python3.7
python3.7 is /usr/local/bin/python3.7
I want to make the python3 command runs python3.7 but I am very confused of the setting of python.
I searched on Google but I couldn't solve it.
Could you please help me?
Thank you
That's a bad soft link since you removed python3.
Check out where python3 is by
which python3
Then use the return of that output to see what it's currently pointing to
ls -la $(which python3)
Replace the bad python3 softlink with the correct python3.7
ln -sf $(which python3.7) /usr/local/bin/python3
Test out the version
python3 --version
In the future, check out pyenv because it will make installing new versions of python and switching between versions much easier.

Categories

Resources