This is the first day of my participation in the Gwen Challenge in November. Check out the details: the last Gwen Challenge in 2021.

inMacUnder the default installationPythonisPython 2.7.16.

This is theMarch 4, 2019Released version.

It has been more than two years.

The new version of thePythonBasically, it is released regularly on a monthly update schedule.

So far,PythonIt’s been releasedPython 3.8.9Version.

It means to be in theMacExperience the new versionPython3Features. You need toMacInstalled on thePython3And keep updated.

Here we useHomebrewInstall and perform updates.

I. Environment configuration

Check the Mac environment preparations.

1. CheckHomebrew

Because we use Homebrew to install and keep Python up to date. Therefore, we first examine the Homebrew environment.

If the content is not displayed, instead it is displayedcommand not found: brew. Installation requiredHomebrew.

$ ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
Copy the code

2. View the installed softwarePython

If you have previously installed using Python downloaded from python.org. So it installs and updates differently than Homebrew. If you don’t need multiple old Python versions to exist at the same time, you need to remove the excess Python versions.


Note ⚠️ : inMacOn, the operating system needs to use/usr/bin/pythonUnder thePython. So in that directoryPythonDo not delete.However, at/usr/local/binOr by symbolic link herePythonVersion can be deleted.If the position is../Cellar/pythonAt the beginning, the instructions are usedHomebrewThe installation.

Unneeded Python versions are simply removed here, and then the original file is deleted.

3. The installationPython

brew install python3
Copy the code

4. UpgradePython

brew upgrade python3
Copy the code

If the latest version is already installedPython.HomebrewWill display:

About 5.PYTHONPATHThe environment variable

The PYTHONPATH environment variable is the Python search path, and by default all modules we import will be found from PYTHONPATH.

PYTHONPATH can be printed using the following code:

print(os.sys.path)
Copy the code

I. the configurationLLDB PythonThe module

Append the LLDB Python module path to the sys.path list for easy import.

  1. forshandbash:
export PYTHONPATH=`lldb -P`
Copy the code
  1. forcshandtcsh:
setenv PYTHONPATH `lldb -P`
Copy the code

The default Shell for the new macOS is ZSH. So, first enter terminal execution:

echo 'export PYTHONPATH=`lldb -P`' >> ~/.zshrc
Copy the code

Then execute:

source ~/.zshrc
Copy the code

In 6.VS CodeIn the configurationPython

I. inVS CodeInstalled in thePythonThe plug-in

inVSCodeSearch extension inPython, as shown below:



After the installation is complete, you need to load it againVSCodeEnable the plug-in.

Ii. InVS CodeIn the configurationPythonThe environment

inVS CodeIn the⌘ + NCreate a new file and select it in the lower left cornerPythonInterpreter, select in the lower right cornerPlain TextforPython:⚠ ️ note:usePython3.0Previous versions of the interpreter.

Second,pipInstallation and use of

Python has many third-party libraries, or modules. These libraries play different roles for different applications. We will definitely use these modules in real projects. So how do you import these modules into your own projects?

Python’s official PyPi repository provides a unified repository where all third-party libraries, even your own open source modules, can be published and downloaded by the world.

Python has two well-known package management tools:

  • easy_install
  • pip

inpython 2In theeasy_installIs installed by default, andpipWe need to install it manually. As thePythonVersion improvement,easy_installHas been gradually phased out, but some of the older third-party libraries, in the current can only passeasy_installInstall. At present,pipHas become a mainstream installation tool since2 > = 2.7.9 PythonorPython 3.4They are installed by defaultpip.

Use instructionpip3 -vFor details:

usepip3Installation module:

pip install requests  
pip install ipython
Copy the code

Install modules with piP3 specified version:

PIP install pygame = = 1.9.6Copy the code

Install modules with piP3 specified version:

pip uninstall pygame
Copy the code

List the installed libraries:

pip list
pip freeze
Copy the code

Save the list of installed libraries to a text file:

pip freeze > ./requirements.txt
Copy the code

Batch install libraries based on dependent files:

pip install -r install.txt
Copy the code

Displays information about installed packages:

pip show package
pip show -f package
Copy the code

Upgrade the specified package:

pip install -U package
Copy the code