Default python path mac

Change Python Version Mac How to set Python3 as a default python version on MacOS?

By default MacOS ships with Python-2.-. But, I guess most of us have long back started to work with Python-3 and it is very irritating to run python3 every time instead of python in terminal. Here is how to do this. Open the terminal (bash or zsh) whatever shell you are using. Install python-3 using Homebrew (https://brew.sh).

lrwxr-xr-x 1 irfan admin 34 Nov 11 16:32 /usr/local/bin/python3 -> ../Cellar/python/3.7.5/bin/python3 lrwxr-xr-x 1 irfan admin 41 Nov 11 16:32 /usr/local/bin/python3-config -> ../Cellar/python/3.7.5/bin/python3-config lrwxr-xr-x 1 irfan admin 36 Nov 11 16:32 /usr/local/bin/python3.7 -> ../Cellar/python/3.7.5/bin/python3.7 lrwxr-xr-x 1 irfan admin 43 Nov 11 16:32 /usr/local/bin/python3.7-config -> ../Cellar/python/3.7.5/bin/python3.7-config lrwxr-xr-x 1 irfan admin 37 Nov 11 16:32 /usr/local/bin/python3.7m -> ../Cellar/python/3.7.5/bin/python3.7m lrwxr-xr-x 1 irfan admin 44 Nov 11 16:32 /usr/local/bin/python3.7m-config -> ../Cellar/python/3.7.5/bin/python3.7m-config 

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 (not tab). Run this:

Читайте также:  Тег INPUT, атрибут value

Top comments (85)

I’m not saying anyone would want to install Python 3 without brew, but if they did, pip3 gets installed:
$ pip3 —version
pip 19.2.3 from /Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/pip (python 3.8)
$ pip3 install —upgrade pip
Collecting pip
Downloading files.pythonhosted.org/packages/54. (1.4MB)
|████████████████████████████████| 1.4MB 1.7MB/s
Installing collected packages: pip
Found existing installation: pip 19.2.3
Uninstalling pip-19.2.3:
Successfully uninstalled pip-19.2.3
Successfully installed pip-20.0.2

$ which pip
/Library/Frameworks/Python.framework/Versions/3.8/bin/pip

Now, Irfan’s handy command:
sudo ln -s -f /usr/local/bin/python3.8 /usr/local/bin/python

$ python —version
Python 3.8.2

7 likes Like Comment button

Hey! I tried following your above-mentioned method .. but I don’t know how the path is different . Although this method worked but will that path create any problem later on?
On my mac
which pip results in
/usr/local/bin/pip
instead of
/Library/Frameworks/Python.framework/Versions/3.8/bin/pip

apoorveesinha@Apoorvees-Air ~ % python —version
Python 2.7.16
apoorveesinha@Apoorvees-Air ~ % python3 —version
Python 3.8.2
apoorveesinha@Apoorvees-Air ~ % pip3 —version
pip 19.2.3 from /Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/pip (python 3.8)
apoorveesinha@Apoorvees-Air ~ % pip3 install —upgrade pip
Collecting pip
Downloading files.pythonhosted.org/packages/54. (1.4MB)Installing collected packages: pip
Found existing installation: pip 19.2.3
Uninstalling pip-19.2.3:
Successfully uninstalled pip-19.2.3
Successfully installed pip-20.0.2
apoorveesinha@Apoorvees-Air ~ % which pip
/usr/local/bin/pip
apoorveesinha@Apoorvees-Air ~ % which pip
/usr/local/bin/pip
apoorveesinha@Apoorvees-Air ~ % sudo ln -s -f /usr/local/bin/python3.8 /usr/local/bin/python
Password:
apoorveesinha@Apoorvees-Air ~ %

apoorveesinha@Apoorvees-Air ~ % python —version
Python 3.8.2
apoorveesinha@Apoorvees-Air ~ % python3 —version
Python 3.8.2

2 likes Like Comment button

Источник

How to Set Up Python 3 as Default Path on Mac: A Comprehensive Guide

Learn how to set up Python 3 as the default path on your Mac operating system with this comprehensive guide. Discover the best practices for Python development and troubleshooting tips for path issues.

Python is an open-source, high-level programming language that is widely used in various fields such as data science, web development, and automation. By default, Mac operating systems come with Python 2 pre-installed, but many developers prefer to use Python 3 for their projects. In this comprehensive guide, we will provide you with step-by-step instructions on how to set up Python 3 as the default version on your Mac operating system.

Installing Python 3 on Mac

Before setting up Python 3 as the default version on your Mac, you need to install it. There are several ways to install Python 3 on your Mac, including using Homebrew or the official Python distribution installer.

Method 1: Using Homebrew

Homebrew is a popular package manager for macOS that can be used to install Python 3. To install Homebrew, open the Terminal app and run the following command:

/bin/bash -c «$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)»

Once Homebrew is installed, you can install Python 3 by running the following command:

This will install the latest version of Python 3 on your Mac.

Method 2: Using the Official Python Distribution Installer

Another way to install Python 3 on your Mac is by downloading the official Python distribution installer from the Python website. Follow these steps to download and install Python 3:

  1. Go to the Python website and download the latest version of python 3 for macOS.
  2. Double-click the downloaded file to start the installation process.
  3. Follow the instructions in the installer to complete the installation process.

Method 3: Using Pyenv

Pyenv is a powerful tool that can be used to manage multiple versions of Python on your Mac. It allows you to easily switch between different Python versions and create virtual environments. Follow these steps to install Pyenv:

/bin/bash -c «$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)»

eval «$(pyenv virtualenv-init -)»

Once Pyenv is installed, you can use it to install and manage different versions of Python. To install Python 3, run the following command:

This will install Python 3.9.0 on your Mac. You can then use Pyenv to switch between different Python versions and create virtual environments.

Setting up Python 3 as Default

Now that you have installed Python 3 on your Mac, you need to set it up as the default version. There are several ways to do this, including changing the PATH variable, using aliases, or creating symbolic links.

Method 1: Changing the PATH Variable

The PATH variable is a system variable that tells the Terminal app where to look for executable files. By default, the system-provided Python 2 is located in /usr/bin/python, which is included in the PATH variable. To set up Python 3 as the default version, you need to add the path to the Python 3 executable file to the PATH variable.

Follow these steps to change the PATH variable:

This will add the path to the Python 3 executable file to the PATH variable.

Method 2: Using Aliases

An alias is a shortcut that allows you to execute a command by typing a shorter command. You can create an alias for Python 3 to use it as the default version.

Follow these steps to create an alias:

This will create an alias for Python 3 to use it as the default version.

A symbolic link is a special type of file that points to another file. You can create a symbolic link for Python 3 to use it as the default version.

Follow these steps to create a symbolic link:

ln -s /usr/local/bin/python3 /usr/local/bin/python

This will create a symbolic link for Python 3 to use it as the default version.

Note: Avoid Modifying the System-Provided Python Version

It is recommended to avoid modifying the system-provided Python version, as it may cause issues with the operating system and other applications that depend on Python 2.

How to Setup Python 3 as Default on Mac

This video will show you how to install Python 3 using Homebrew even with Python 2 and Duration: 4:30

Troubleshooting Python Path Issues

Some users may encounter issues with locating the Python path or setting up the correct version. Here are some tips to troubleshoot Python path issues:

  1. Check the .bash_profile or other configuration files for errors and syntax issues.
  2. Use the which command to check the location of the Python executable file.
  3. Use a third-party tool such as Anaconda or Miniconda to manage Python environments.

Best Practices for Python Development on Mac

Here are some best practices for python development on Mac:

  1. Keep Python and its dependencies up to date to ensure compatibility and security patches.
  2. Use version control tools such as Git to manage different versions of Python environments and projects.
  3. Follow best practices for coding in Python, such as using meaningful variable names and commenting code.

Other simple Python 3 code examples to set as default path on MacOS

ln -sf /usr/local/bin/python3 /usr/local/bin/python

In Python , in particular, use python3 as default mac code sample

ln -s -f /usr/local/bin/python3.7 /usr/local/bin/python 
# it is said not to do but I used it anyway, because of the conveniency $ echo "alias python=/usr/local/bin/python3.7" >> ~/.zshrc 

In Python as proof, change python3 as default for mac code sample

unlink /usr/local/bin/python ln -s /usr/local/bin/python3.3 /usr/local/bin/python

Conclusion

In conclusion, setting up Python 3 as the default version on your Mac can be done through various methods, including changing the PATH variable, using aliases, or creating symbolic links. It is important to use proper syntax, check for errors, and avoid modifying the system-provided Python version. best practices for python development on Mac include using version control tools and following coding best practices. With this comprehensive guide, you should be able to set up Python 3 as the default version on your Mac and start developing your projects.

Источник

How to add Python to the PATH variable in Mac

The PATH variable is a list of directories where each directory​ contains a UNIX executable file (or its alias) for a command/program.

When a command is entered in the terminal, it searches for an executable file with the same name as the entered command in the PATH variable. In the event that the required file is not found, the terminal will respond​ with an error message saying that the command was not found.

One way to overcome this error is to write the complete directory of the executable file (or its alias) instead of just entering the command name. This, however, is not a very user-friendly approach.

An easier way to avoid this error is to add the executable files’ directory to the PATH variable. This often needs to be done after installing Python.

The complete path of the Python (or Python3) UNIX executable can be added (for OS X 10.8 Mountain Lion and up) by:

  1. Opening the Terminal and entering the command: sudo nano /etc/paths . Enter your password when prompted to do so.
  2. A list of directories that are currently a part of the PATH variable will appear. Enter the path of the Python install directory at the end of this list.
  3. Press control + X to quit and then Y to save the changes.

Python can now be used directly from the Terminal without having to write its location every time. Try executing the command python —version to output the default version of Python installed on your system.

Use python3 —version to find out the version of Python3.x.

Источник

Оцените статью