JaviOverflow
JaviOverflow

Reputation: 1480

Python symlink to python3

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

Upvotes: 9

Views: 50376

Answers (4)

James Hirschorn
James Hirschorn

Reputation: 8026

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

sudo apt install python-is-python3

Upvotes: 3

Tan Nguyen
Tan Nguyen

Reputation: 1103

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;

Upvotes: 4

smcjones
smcjones

Reputation: 5600

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.

Upvotes: 5

user10316640
user10316640

Reputation:

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.

Upvotes: 5

Related Questions