I want to make command python3 to run python3.7 - python

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.

Related

Inconsistent python3 and pip3 on OSX

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

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

Python and pip installation on Mac don't show version?

On Mac OS.
Downloaded the Python from their website.
Python -V return Python 2.7.16, and
python3 -V return Python 3.9.4
Installed pip with : python3 get-pip.py, got Successfully installed pip-21.0.1
But when I run pip -V
I get File "/usr/local/bin/pip", line 1.... SyntaxError: invalid syntax
After reading here a lot, i could not understand (in simple words for dumbs) :
How could you "alias" or update python to show/run in version 3+ ?
Why I can't get the pip version if it's installed ?
Use pip as a module instead
% python3 -m pip --version
pip 21.0.1 from /usr/local/lib/python3.9/site-packages/pip (python 3.9)
Anyone who's interested , what i had to do is to
CD the project on terminal
run python3 -m venv env (this create virtual environment of python3 inside project)
run source env/bin/activate activate it.
now pip --version or python --version will return the right results.
From my basic knowledge i understand that the mac is using python2 which we don't want to interrupt, so we install python3 and create a 'virtual environment' for it inside our project folder, once its in, we can install all other things.
to deactivate (stop the virtual env) we run deactivate

virtualenv on macOS uses nonexistent python interpreter

When I run virtualenv, I get this:
$ virtualenv
-bash: /usr/local/bin/virtualenv: /usr/local/opt/python/bin/python2.7: bad interpreter: No such file or directory
virtualenv only started behaving this way today. It worked yesterday. It breaks because virtualenv is a Python script using a nonexistent Python interpreter:
$ head -1 $(which virtualenv)
#!/usr/local/opt/python/bin/python2.7
On my machine, /usr/local/opt/python is a symlink to a Python 3.6 directory:
$ ls -l /usr/local/opt/python
lrwxr-xr-x 1 jim admin 24 2 Mar 13:45 /usr/local/opt/python -> ../Cellar/python/3.6.4_3
As expected, the Python 3.6 directory does not contain a bin/python2.7:
$ ls /usr/local/Cellar/python/3.6.4_3/bin/
2to3 idle pip3 pydoc3.6 python3-config python3.6m-config wheel3
2to3-3.6 idle3 pip3.6 python python3.6 pyvenv
easy_install idle3.6 pydoc python-config python3.6-config pyvenv-3.6
easy_install-3.6 pip pydoc3 python3 python3.6m wheel
virtualenv clearly expects /usr/local/opt/python to contain Python 2 material, but it only contains Python 3 material.
My /usr/local/opt/python is managed by Homebrew. I don't know the provenance of my virtualenv. How do I find out where my /usr/local/bin/virtualenv came from?
Which is to blame? My virtualenv or Homebrew?
The blame for this lies with pip, not Homebrew. My /usr/local/bin/virtualenv came from pip install virtualenv, which embeds an absolute link to the Python interpreter at installation time! I have opened an issue about this unidiomatic behavior.
Same problem on my Mac. Maybe it got broken when I updated to Mojave? Who knows.
Resolved with a brew install of Python 2:
brew install python2
This now takes over from my factory-installed Python 2.7 and gives me a new virtualenv that works:
$ which virtualenv
/usr/local/bin/virtualenv
Firstly, sorry for adding a separate comment here -- I lack the reputation to add a comment to #jameshfisher's answer.
I used homebrew to update python2 on macos to the latest version:
~ ❯❯❯ python2 --version
Python 2.7.15
Which creates/updates the python2 symlink in /usr/local/bin to link to that particular brew installed update:
~ ❯❯❯ ls -ahl =python2
lrwxr-xr-x 1 michael admin 39B 3 Jul 17:11 /usr/local/bin/python2 -> ../Cellar/python#2/2.7.15_1/bin/python2
The shebang in my /usr/local/bin/virtualenv was:
~ ❯❯❯ head -1 $(which virtualenv)
#!/usr/local/opt/python/bin/python2.7
Which did not exist:
~ ❯❯❯ ls -l /usr/local/opt/python/bin/python2.7
ls: /usr/local/opt/python/bin/python2.7: No such file or directory
So modifying the shebang to #!/usr/local/bin/python2 to use the brew installed updated python2 version was the most appropriate way to go:
~ ❯❯❯ virtualenv --version
15.1.0
✨🐟✨
A bit of a red herring for me was that I had mistakenly assumed typing which python would give me the path to my version of python2:
~ ❯❯❯ which python
/Users/michael/.pyenv/shims/python
~ ❯❯❯ which python2
/usr/local/bin/python2
~ ❯❯❯ which python3
/Users/michael/.pyenv/shims/python3
~ ❯❯❯ /Users/michael/.pyenv/shims/python --version
Python 3.7.0
I had forgotten that I had set pyenv global to python 3.7.0. Please don't fall into that trap as I did! ✨😄✨

How do I set the most recent Python installation as default version?

python update
I have Python 3.5 installed on my (LinuxMint) computer by:
sudo apt-get install python3.5
However, when I run python -V, it shows that Python 2.7 is being used.
How do I tell the system to use the updated version of Python?
You have python2.7 installed and you already have a link to the python2.7 executable so that when you simply run python, it actually runs python2.7. When you install python3.5, that link still exists.
You should either run python3 (or python3.5) or you should replace the link with a new link like so (assuming python3.5 in /usr/bin):
ln -sf /usr/bin/python3.5 /usr/bin/python
Add sudo before the command if you don't have permissions to create the link and your user is a sudoer.
More dynamically,
ln -sf $(which python3) $(which python)
which forces the creation of symbolic link from python3 to python.

Categories

Resources