简体   繁体   中英

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

Given the path is not being utilized, it is being overridden by a shell alias. This can be confirmed by typing set | grep python 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 .

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM