Python symlink to python3 [closed]
Asked Answered
H

4

9

So I'm setting up my default variables in a new MacBook M1 and for some reason, my symlink doesn't seem to work.

Why is the following behaviour happening? The symlink from python to python3 gets lost somehow. /Users/overflow/Documents/tools is part of my PATH variable.

$ type python
python is /Users/overflow/Documents/tools/python

$ python -V
Python 2.7.16

$ ls -lah /Users/overflow/Documents/tools/python
lrwxr-xr-x  1 overflow  staff    16B  6 Oct 18:48 /Users/overflow/Documents/tools/python -> /usr/bin/python3

$ /usr/bin/python3 -V
Python 3.8.9

$ echo $PATH | sed 's/:/\n/g'
/Users/overflow/Documents/tools
/Users/overflow/Documents/Dropbox/productivity/bin
/Users/overflow/Documents/tools/confluent-6.1.0/bin
/Users/overflow/.sdkman/candidates/java/current/bin
/Users/overflow/.nvm/versions/node/v16.10.0/bin
/Users/overflow/bin
/usr/local/bin
/opt/homebrew/bin
/opt/homebrew/sbin
/usr/local/bin
/usr/bin
/bin
/usr/sbin
/sbin
Hammurabi answered 6/10, 2021 at 17:58 Comment(12)
some system may still use Python 2 to run some functions and sometimes changing symlink may crush system. Better use command python3 or python3.8 to run Python 3Agriculture
maybe you have other command python in current folder and it may run this command instead of /Users/overflow/Documents/tools/python . Maybe you have path . on PATH so it may run command python like ./pythonAgriculture
Maybe it's an alias or function... run 'type python'.. it will give you a better answer than 'which' does.Gerous
I didn't know about type command, it's awesome! - I've just replaced the which statement with a type one. Also added my PATH thereHammurabi
you did not show the result of : which pythonSusurrate
The link you made is on python is on the directory which contains python or on python3 exe : what is important is the PATH contents which should give directory containing exe. Also check that there is not a alias python.Susurrate
I removed the result of which but it was showing the same as type. Now, the PATH variable contains /Users/overflow/Documents/tools, which is the directory containing the "exe" you mention that is the link I've just created. I'm not sure I get what you mean by that tbh.Hammurabi
Can we know the type of the shell with version? bash or zsh. I'm not sure if that would help in particular but it can help in narrowing down the search.Cheyney
Run hash -r and try again. It clears a cache in shell.Marocain
I noticed you have home-brew, so you can home-brew pyenv and set your global pyenv to whatever version you need. I use it to run 3.10.0 at the moment on my M1Philana
hash -r didn't solve the issue. I would like to avoid using python managers and use the default one.Hammurabi
While I'm not sure why this is happening, might I suggest using an alias instead of a symlink? Personally, I use alias python=python3.9 and alias pip=pip3.9 in my .zshrc. This question may be of interest.Prior
S
5

Given the path is not being utilized, it is being overridden by a shell alias. This can be confirmed by typing set | grep python

If you are using virtualenv:

Try: /usr/bin/python3 -m venv python3.8.9

The common MacOS python managers are:

  1. pythonz

List installs using pythonz list

Change using: /usr/bin/python3 -m venv python3.8.9

  1. pythonbrew

List installs pythonbrew list

Use pythonbrew switch 3.8.9 to move to the new version.

  1. pyenv

Claims to use only $PATH. Included for completeness, but just in case

List installs pyenv versions

Use pyenv global 3.8.9 to move to the new version.


To remove any of these remove the appropriate line from your ~/.bashrc or ~/.zshrc file.

You can locate the line with grep ~/.bashrc python.

Subdiaconate answered 13/10, 2021 at 22:56 Comment(3)
set | grep python doesn't return anything and neither of those three python managers is installed.Hammurabi
type python would print python is an alias for ... if OP had an alias set. Since they supplied type python output in the question, I think we can safely say they do not have an alias set.Prior
@thisisrandy, That is correct. The type output was not there when I started the answer. In that case it, is most likely the python virtual environment.Subdiaconate
P
5

I can give you some general troubleshooting tips and a band-aid that can replace a symlink.

$(which python) -V

This should get you the version for python3.

Other thing worth checking:

which python2

This should point to some standard location, /usr/bin/python2 for example. If it isn't pointed there, might be worth understanding why.

In general, Macs have some quirks. The fact that a new MacBook M4 ships with Python2 as the default is a smell that you probably shouldn't try to overwrite the default value, and for whatever reason it doesn't seem to want you to.

Simple answer: set an alias.

echo 'alias python="python3"' >> ~/.zshrc && source;

This will work for you and should not interfere with any potential other systems.

Better answer:

As others have described, create a virtual environment and source venv/bin/activate.

Pricilla answered 16/10, 2021 at 2:45 Comment(1)
+1 to 'alias python="python3"` if args[0] ends with 3, you will get a Python3 interpreter no matter what venv is set to.Subdiaconate
E
4

For macOs 13 (Ventura)

Required install python3 first then set an alias for python that link to python3.

echo 'alias python="python3"' >> ~/.zshrc && source ~/.zshrc;
Esra answered 9/12, 2022 at 7:25 Comment(0)
P
3

This solution is not specific to Mac, but on a .deb based system you want:

sudo apt install python-is-python3
Puppis answered 19/8, 2023 at 7:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.