Python

安装VS Code

官方教程
首先根据教程安装VS Code、VS Code Python extension和Python3(mac系统里面一般都内置了Python3)。
如果官网的下载链接太慢了,可以百度其他国内的下载源/镜像。
可以在Terminal中用下列语句检验python是否安装:

$ python3 --version

为了能在Terminal中直接用code命令打开VS Code,首先要保证VS Code的安装路径已经被添加到PATH中:

  • 打开VS Code
  • 打开Command Palette (Cmd+Shift+P),然后键入shell command并选择Install 'code' command in PATH command.
  • 重启Terminal让PATH更新,以后就能在任意位置在Terminal中用code .打开VS Code了。

创建项目并在项目目录打开VS Code

$ mkdir hello
$ cd hello
$ code .

创建虚拟环境

$ python3 -m venv .venv
$ source .venv/bin/activate

安装第三方包比如matplotlib

以下的操作都在刚创建的虚拟环境中。

$ pip3 install matplotlib
Collecting matplotlibDownloading https://files.pythonhosted.org/packages/21/37/197e68df384ff694f78d687a49ad39f96c67b8d75718bc61503e1676b617/matplotlib-3.4.3.tar.gz (37.9MB)|████████████████████████████████| 37.9MB 222kB/sERROR: Command errored out with exit status 1:command: /Users/kangyangwu/Study/Coding/Python/hello/.venv/bin/python3 -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'/private/var/folders/5_/7wnzch6s44l8kc2q5dz1gxqw0000gn/T/pip-install-9p2d2613/matplotlib/setup.py'"'"'; __file__='"'"'/private/var/folders/5_/7wnzch6s44l8kc2q5dz1gxqw0000gn/T/pip-install-9p2d2613/matplotlib/setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' egg_info --egg-base pip-egg-infocwd: /private/var/folders/5_/7wnzch6s44l8kc2q5dz1gxqw0000gn/T/pip-install-9p2d2613/matplotlib/Complete output (106 lines):Processing numpy/random/_bounded_integers.pxd.inProcessing numpy/random/_philox.pyxTraceback (most recent call last):File "/var/folders/5_/7wnzch6s44l8kc2q5dz1gxqw0000gn/T/easy_install-o6uaod2w/numpy-1.21.2/tools/cythonize.py", line 59, in process_pyximport CythonModuleNotFoundError: No module named 'Cython'The above exception was the direct cause of the following exception:Traceback (most recent call last):File "/var/folders/5_/7wnzch6s44l8kc2q5dz1gxqw0000gn/T/easy_install-o6uaod2w/numpy-1.21.2/tools/cythonize.py", line 240, in <module>main()File "/var/folders/5_/7wnzch6s44l8kc2q5dz1gxqw0000gn/T/easy_install-o6uaod2w/numpy-1.21.2/tools/cythonize.py", line 236, in mainfind_process_files(root_dir)File "/var/folders/5_/7wnzch6s44l8kc2q5dz1gxqw0000gn/T/easy_install-o6uaod2w/numpy-1.21.2/tools/cythonize.py", line 227, in find_process_filesprocess(root_dir, fromfile, tofile, function, hash_db)File "/var/folders/5_/7wnzch6s44l8kc2q5dz1gxqw0000gn/T/easy_install-o6uaod2w/numpy-1.21.2/tools/cythonize.py", line 193, in processprocessor_function(fromfile, tofile)File "/var/folders/5_/7wnzch6s44l8kc2q5dz1gxqw0000gn/T/easy_install-o6uaod2w/numpy-1.21.2/tools/cythonize.py", line 66, in process_pyxraise OSError(msg) from eOSError: Cython needs to be installed in Python as a moduleRunning from numpy source directory.Traceback (most recent call last):File "/Users/kangyangwu/Study/Coding/Python/hello/.venv/lib/python3.8/site-packages/setuptools/sandbox.py", line 154, in save_modulesyield savedFile "/Users/kangyangwu/Study/Coding/Python/hello/.venv/lib/python3.8/site-packages/setuptools/sandbox.py", line 195, in setup_contextyieldFile "/Users/kangyangwu/Study/Coding/Python/hello/.venv/lib/python3.8/site-packages/setuptools/sandbox.py", line 250, in run_setup_execfile(setup_script, ns)File "/Users/kangyangwu/Study/Coding/Python/hello/.venv/lib/python3.8/site-packages/setuptools/sandbox.py", line 45, in _execfileexec(code, globals, locals)File "/var/folders/5_/7wnzch6s44l8kc2q5dz1gxqw0000gn/T/easy_install-o6uaod2w/numpy-1.21.2/setup.py", line 448, in <module>File "/var/folders/5_/7wnzch6s44l8kc2q5dz1gxqw0000gn/T/easy_install-o6uaod2w/numpy-1.21.2/setup.py", line 430, in setup_packageFile "/var/folders/5_/7wnzch6s44l8kc2q5dz1gxqw0000gn/T/easy_install-o6uaod2w/numpy-1.21.2/setup.py", line 236, in generate_cythonRuntimeError: Running cythonize failed!During handling of the above exception, another exception occurred:Traceback (most recent call last):File "<string>", line 1, in <module>File "/private/var/folders/5_/7wnzch6s44l8kc2q5dz1gxqw0000gn/T/pip-install-9p2d2613/matplotlib/setup.py", line 258, in <module>setup(  # Finally, pass this all along to distutils to do the heavy lifting.File "/Users/kangyangwu/Study/Coding/Python/hello/.venv/lib/python3.8/site-packages/setuptools/__init__.py", line 144, in setup_install_setup_requires(attrs)File "/Users/kangyangwu/Study/Coding/Python/hello/.venv/lib/python3.8/site-packages/setuptools/__init__.py", line 139, in _install_setup_requiresdist.fetch_build_eggs(dist.setup_requires)File "/Users/kangyangwu/Study/Coding/Python/hello/.venv/lib/python3.8/site-packages/setuptools/dist.py", line 716, in fetch_build_eggsresolved_dists = pkg_resources.working_set.resolve(File "/Users/kangyangwu/Study/Coding/Python/hello/.venv/lib/python3.8/site-packages/pkg_resources/__init__.py", line 780, in resolvedist = best[req.key] = env.best_match(File "/Users/kangyangwu/Study/Coding/Python/hello/.venv/lib/python3.8/site-packages/pkg_resources/__init__.py", line 1065, in best_matchreturn self.obtain(req, installer)File "/Users/kangyangwu/Study/Coding/Python/hello/.venv/lib/python3.8/site-packages/pkg_resources/__init__.py", line 1077, in obtainreturn installer(requirement)File "/Users/kangyangwu/Study/Coding/Python/hello/.venv/lib/python3.8/site-packages/setuptools/dist.py", line 786, in fetch_build_eggreturn cmd.easy_install(req)File "/Users/kangyangwu/Study/Coding/Python/hello/.venv/lib/python3.8/site-packages/setuptools/command/easy_install.py", line 679, in easy_installreturn self.install_item(spec, dist.location, tmpdir, deps)File "/Users/kangyangwu/Study/Coding/Python/hello/.venv/lib/python3.8/site-packages/setuptools/command/easy_install.py", line 705, in install_itemdists = self.install_eggs(spec, download, tmpdir)File "/Users/kangyangwu/Study/Coding/Python/hello/.venv/lib/python3.8/site-packages/setuptools/command/easy_install.py", line 890, in install_eggsreturn self.build_and_install(setup_script, setup_base)File "/Users/kangyangwu/Study/Coding/Python/hello/.venv/lib/python3.8/site-packages/setuptools/command/easy_install.py", line 1158, in build_and_installself.run_setup(setup_script, setup_base, args)File "/Users/kangyangwu/Study/Coding/Python/hello/.venv/lib/python3.8/site-packages/setuptools/command/easy_install.py", line 1144, in run_setuprun_setup(setup_script, args)File "/Users/kangyangwu/Study/Coding/Python/hello/.venv/lib/python3.8/site-packages/setuptools/sandbox.py", line 253, in run_setupraiseFile "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.8/lib/python3.8/contextlib.py", line 131, in __exit__self.gen.throw(type, value, traceback)File "/Users/kangyangwu/Study/Coding/Python/hello/.venv/lib/python3.8/site-packages/setuptools/sandbox.py", line 195, in setup_contextyieldFile "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.8/lib/python3.8/contextlib.py", line 131, in __exit__self.gen.throw(type, value, traceback)File "/Users/kangyangwu/Study/Coding/Python/hello/.venv/lib/python3.8/site-packages/setuptools/sandbox.py", line 166, in save_modulessaved_exc.resume()File "/Users/kangyangwu/Study/Coding/Python/hello/.venv/lib/python3.8/site-packages/setuptools/sandbox.py", line 141, in resumesix.reraise(type, exc, self._tb)File "/Users/kangyangwu/Study/Coding/Python/hello/.venv/lib/python3.8/site-packages/setuptools/_vendor/six.py", line 685, in reraiseraise value.with_traceback(tb)File "/Users/kangyangwu/Study/Coding/Python/hello/.venv/lib/python3.8/site-packages/setuptools/sandbox.py", line 154, in save_modulesyield savedFile "/Users/kangyangwu/Study/Coding/Python/hello/.venv/lib/python3.8/site-packages/setuptools/sandbox.py", line 195, in setup_contextyieldFile "/Users/kangyangwu/Study/Coding/Python/hello/.venv/lib/python3.8/site-packages/setuptools/sandbox.py", line 250, in run_setup_execfile(setup_script, ns)File "/Users/kangyangwu/Study/Coding/Python/hello/.venv/lib/python3.8/site-packages/setuptools/sandbox.py", line 45, in _execfileexec(code, globals, locals)File "/var/folders/5_/7wnzch6s44l8kc2q5dz1gxqw0000gn/T/easy_install-o6uaod2w/numpy-1.21.2/setup.py", line 448, in <module>File "/var/folders/5_/7wnzch6s44l8kc2q5dz1gxqw0000gn/T/easy_install-o6uaod2w/numpy-1.21.2/setup.py", line 430, in setup_packageFile "/var/folders/5_/7wnzch6s44l8kc2q5dz1gxqw0000gn/T/easy_install-o6uaod2w/numpy-1.21.2/setup.py", line 236, in generate_cythonRuntimeError: Running cythonize failed!Edit setup.cfg to change the build options; suppress output with --quiet.BUILDING MATPLOTLIBmatplotlib: yes [3.4.3]python: yes [3.8.2 (default, Jun  8 2021, 11:59:35)  [Clang 12.0.5(clang-1205.0.22.11)]]platform: yes [darwin]tests: no  [skipping due to configuration]macosx: yes [installing]Cythonizing sources----------------------------------------
ERROR: Command errored out with exit status 1: python setup.py egg_info Check the logs for full command output.
WARNING: You are using pip version 19.2.3, however version 21.2.4 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.

我们可以看到出现了第一个错误,找不到Cython模块,所以我们要先安装下Cython模块。

$ pip3 install cython
Collecting cythonDownloading https://files.pythonhosted.org/packages/ec/30/8707699ea6e1c1cbe79c37e91f5b06a6266de24f699a5e19b8c0a63c4b65/Cython-0.29.24-py2.py3-none-any.whl (979kB)|████████████████████████████████| 983kB 734kB/s
Installing collected packages: cython
Successfully installed cython-0.29.24
WARNING: You are using pip version 19.2.3, however version 21.2.4 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.

Cython安装完毕,现在尝试重新安装matplotlib。

$ pip3 install matplotlib
Collecting matplotlibUsing cached https://files.pythonhosted.org/packages/21/37/197e68df384ff694f78d687a49ad39f96c67b8d75718bc61503e1676b617/matplotlib-3.4.3.tar.gz
Collecting cycler>=0.10 (from matplotlib)WARNING: Retrying (Retry(total=4, connect=None, read=None, redirect=None, status=None)) after connection broken by 'ReadTimeoutError("HTTPSConnectionPool(host='pypi.org', port=443): Read timed out. (read timeout=15)")': /simple/cycler/Downloading https://files.pythonhosted.org/packages/f7/d2/e07d3ebb2bd7af696440ce7e754c59dd546ffe1bbe732c8ab68b9c834e61/cycler-0.10.0-py2.py3-none-any.whl
Collecting kiwisolver>=1.0.1 (from matplotlib)Downloading https://files.pythonhosted.org/packages/8e/87/259fde8cf07d06677f0a749cb157d079ebd00d40fe52faaab1a882a66159/kiwisolver-1.3.2.tar.gz (54kB)|████████████████████████████████| 61kB 43kB/s ERROR: Command errored out with exit status 1:command: /Users/kangyangwu/Study/Coding/Python/hello/.venv/bin/python3 -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'/private/var/folders/5_/7wnzch6s44l8kc2q5dz1gxqw0000gn/T/pip-install-342me19r/kiwisolver/setup.py'"'"'; __file__='"'"'/private/var/folders/5_/7wnzch6s44l8kc2q5dz1gxqw0000gn/T/pip-install-342me19r/kiwisolver/setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' egg_info --egg-base pip-egg-infocwd: /private/var/folders/5_/7wnzch6s44l8kc2q5dz1gxqw0000gn/T/pip-install-342me19r/kiwisolver/Complete output (166 lines):warning: no files found matching '*.png' under directory 'docs/source'no previously-included directories found matching '.git'no previously-included directories found matching 'dist'no previously-included directories found matching 'build'no previously-included directories found matching 'docs/build'Traceback (most recent call last):File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.8/lib/python3.8/distutils/core.py", line 148, in setupdist.run_commands()File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.8/lib/python3.8/distutils/dist.py", line 966, in run_commandsself.run_command(cmd)File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.8/lib/python3.8/distutils/dist.py", line 985, in run_commandcmd_obj.run()File "/Users/kangyangwu/Study/Coding/Python/hello/.venv/lib/python3.8/site-packages/setuptools/command/bdist_egg.py", line 172, in runcmd = self.call_command('install_lib', warn_dir=0)File "/Users/kangyangwu/Study/Coding/Python/hello/.venv/lib/python3.8/site-packages/setuptools/command/bdist_egg.py", line 158, in call_commandself.run_command(cmdname)File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.8/lib/python3.8/distutils/cmd.py", line 313, in run_commandself.distribution.run_command(command)File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.8/lib/python3.8/distutils/dist.py", line 985, in run_commandcmd_obj.run()File "/Users/kangyangwu/Study/Coding/Python/hello/.venv/lib/python3.8/site-packages/setuptools/command/install_lib.py", line 15, in runself.byte_compile(outfiles)File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.8/lib/python3.8/distutils/command/install_lib.py", line 132, in byte_compilebyte_compile(files, optimize=0,File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.8/lib/python3.8/distutils/util.py", line 466, in byte_compilecompile(file, cfile, dfile)File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.8/lib/python3.8/py_compile.py", line 157, in compileos.makedirs(dirname)File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.8/lib/python3.8/os.py", line 213, in makedirsmakedirs(head, exist_ok=exist_ok)File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.8/lib/python3.8/os.py", line 213, in makedirsmakedirs(head, exist_ok=exist_ok)File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.8/lib/python3.8/os.py", line 213, in makedirsmakedirs(head, exist_ok=exist_ok)[Previous line repeated 2 more times]File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.8/lib/python3.8/os.py", line 223, in makedirsmkdir(name, mode)File "/Users/kangyangwu/Study/Coding/Python/hello/.venv/lib/python3.8/site-packages/setuptools/sandbox.py", line 310, in wrappath = self._remap_input(name, path, *args, **kw)File "/Users/kangyangwu/Study/Coding/Python/hello/.venv/lib/python3.8/site-packages/setuptools/sandbox.py", line 452, in _remap_inputself._violation(operation, os.path.realpath(path), *args, **kw)File "/Users/kangyangwu/Study/Coding/Python/hello/.venv/lib/python3.8/site-packages/setuptools/sandbox.py", line 407, in _violationraise SandboxViolation(operation, args, kw)setuptools.sandbox.SandboxViolation: SandboxViolation: mkdir('/Users/kangyangwu/Library/Caches/com.apple.python/private/var/folders/5_/7wnzch6s44l8kc2q5dz1gxqw0000gn/T/easy_install-q_4zd0xh', 511) {}The package setup script has attempted to modify files on your systemthat are not within the EasyInstall build area, and has been aborted.This package cannot be safely installed by EasyInstall, and may notsupport alternate installation locations even if you run its setupscript by hand.  Please inform the package's author and the EasyInstallmaintainers to find out if a fix or workaround is available.During handling of the above exception, another exception occurred:Traceback (most recent call last):File "/Users/kangyangwu/Study/Coding/Python/hello/.venv/lib/python3.8/site-packages/setuptools/sandbox.py", line 154, in save_modulesyield savedFile "/Users/kangyangwu/Study/Coding/Python/hello/.venv/lib/python3.8/site-packages/setuptools/sandbox.py", line 195, in setup_contextyieldFile "/Users/kangyangwu/Study/Coding/Python/hello/.venv/lib/python3.8/site-packages/setuptools/sandbox.py", line 250, in run_setup_execfile(setup_script, ns)File "/Users/kangyangwu/Study/Coding/Python/hello/.venv/lib/python3.8/site-packages/setuptools/sandbox.py", line 45, in _execfileexec(code, globals, locals)File "/var/folders/5_/7wnzch6s44l8kc2q5dz1gxqw0000gn/T/easy_install-q_4zd0xh/cppy-1.1.0/setup.py", line 15, in <module># - py/kiwisolver.cppFile "/Users/kangyangwu/Study/Coding/Python/hello/.venv/lib/python3.8/site-packages/setuptools/__init__.py", line 145, in setupreturn distutils.core.setup(**attrs)File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.8/lib/python3.8/distutils/core.py", line 163, in setupraise SystemExit("error: " + str(msg))SystemExit: error: SandboxViolation: mkdir('/Users/kangyangwu/Library/Caches/com.apple.python/private/var/folders/5_/7wnzch6s44l8kc2q5dz1gxqw0000gn/T/easy_install-q_4zd0xh', 511) {}The package setup script has attempted to modify files on your systemthat are not within the EasyInstall build area, and has been aborted.This package cannot be safely installed by EasyInstall, and may notsupport alternate installation locations even if you run its setupscript by hand.  Please inform the package's author and the EasyInstallmaintainers to find out if a fix or workaround is available.During handling of the above exception, another exception occurred:Traceback (most recent call last):File "/Users/kangyangwu/Study/Coding/Python/hello/.venv/lib/python3.8/site-packages/setuptools/command/easy_install.py", line 1144, in run_setuprun_setup(setup_script, args)File "/Users/kangyangwu/Study/Coding/Python/hello/.venv/lib/python3.8/site-packages/setuptools/sandbox.py", line 253, in run_setupraiseFile "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.8/lib/python3.8/contextlib.py", line 131, in __exit__self.gen.throw(type, value, traceback)File "/Users/kangyangwu/Study/Coding/Python/hello/.venv/lib/python3.8/site-packages/setuptools/sandbox.py", line 195, in setup_contextyieldFile "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.8/lib/python3.8/contextlib.py", line 131, in __exit__self.gen.throw(type, value, traceback)File "/Users/kangyangwu/Study/Coding/Python/hello/.venv/lib/python3.8/site-packages/setuptools/sandbox.py", line 166, in save_modulessaved_exc.resume()File "/Users/kangyangwu/Study/Coding/Python/hello/.venv/lib/python3.8/site-packages/setuptools/sandbox.py", line 141, in resumesix.reraise(type, exc, self._tb)File "/Users/kangyangwu/Study/Coding/Python/hello/.venv/lib/python3.8/site-packages/setuptools/_vendor/six.py", line 685, in reraiseraise value.with_traceback(tb)File "/Users/kangyangwu/Study/Coding/Python/hello/.venv/lib/python3.8/site-packages/setuptools/sandbox.py", line 154, in save_modulesyield savedFile "/Users/kangyangwu/Study/Coding/Python/hello/.venv/lib/python3.8/site-packages/setuptools/sandbox.py", line 195, in setup_contextyieldFile "/Users/kangyangwu/Study/Coding/Python/hello/.venv/lib/python3.8/site-packages/setuptools/sandbox.py", line 250, in run_setup_execfile(setup_script, ns)File "/Users/kangyangwu/Study/Coding/Python/hello/.venv/lib/python3.8/site-packages/setuptools/sandbox.py", line 45, in _execfileexec(code, globals, locals)File "/var/folders/5_/7wnzch6s44l8kc2q5dz1gxqw0000gn/T/easy_install-q_4zd0xh/cppy-1.1.0/setup.py", line 15, in <module># - py/kiwisolver.cppFile "/Users/kangyangwu/Study/Coding/Python/hello/.venv/lib/python3.8/site-packages/setuptools/__init__.py", line 145, in setupreturn distutils.core.setup(**attrs)File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.8/lib/python3.8/distutils/core.py", line 163, in setupraise SystemExit("error: " + str(msg))SystemExit: error: SandboxViolation: mkdir('/Users/kangyangwu/Library/Caches/com.apple.python/private/var/folders/5_/7wnzch6s44l8kc2q5dz1gxqw0000gn/T/easy_install-q_4zd0xh', 511) {}The package setup script has attempted to modify files on your systemthat are not within the EasyInstall build area, and has been aborted.This package cannot be safely installed by EasyInstall, and may notsupport alternate installation locations even if you run its setupscript by hand.  Please inform the package's author and the EasyInstallmaintainers to find out if a fix or workaround is available.During handling of the above exception, another exception occurred:Traceback (most recent call last):File "<string>", line 1, in <module>File "/private/var/folders/5_/7wnzch6s44l8kc2q5dz1gxqw0000gn/T/pip-install-342me19r/kiwisolver/setup.py", line 69, in <module>setup(File "/Users/kangyangwu/Study/Coding/Python/hello/.venv/lib/python3.8/site-packages/setuptools/__init__.py", line 144, in setup_install_setup_requires(attrs)File "/Users/kangyangwu/Study/Coding/Python/hello/.venv/lib/python3.8/site-packages/setuptools/__init__.py", line 139, in _install_setup_requiresdist.fetch_build_eggs(dist.setup_requires)File "/Users/kangyangwu/Study/Coding/Python/hello/.venv/lib/python3.8/site-packages/setuptools/dist.py", line 716, in fetch_build_eggsresolved_dists = pkg_resources.working_set.resolve(File "/Users/kangyangwu/Study/Coding/Python/hello/.venv/lib/python3.8/site-packages/pkg_resources/__init__.py", line 780, in resolvedist = best[req.key] = env.best_match(File "/Users/kangyangwu/Study/Coding/Python/hello/.venv/lib/python3.8/site-packages/pkg_resources/__init__.py", line 1065, in best_matchreturn self.obtain(req, installer)File "/Users/kangyangwu/Study/Coding/Python/hello/.venv/lib/python3.8/site-packages/pkg_resources/__init__.py", line 1077, in obtainreturn installer(requirement)File "/Users/kangyangwu/Study/Coding/Python/hello/.venv/lib/python3.8/site-packages/setuptools/dist.py", line 786, in fetch_build_eggreturn cmd.easy_install(req)File "/Users/kangyangwu/Study/Coding/Python/hello/.venv/lib/python3.8/site-packages/setuptools/command/easy_install.py", line 679, in easy_installreturn self.install_item(spec, dist.location, tmpdir, deps)File "/Users/kangyangwu/Study/Coding/Python/hello/.venv/lib/python3.8/site-packages/setuptools/command/easy_install.py", line 705, in install_itemdists = self.install_eggs(spec, download, tmpdir)File "/Users/kangyangwu/Study/Coding/Python/hello/.venv/lib/python3.8/site-packages/setuptools/command/easy_install.py", line 890, in install_eggsreturn self.build_and_install(setup_script, setup_base)File "/Users/kangyangwu/Study/Coding/Python/hello/.venv/lib/python3.8/site-packages/setuptools/command/easy_install.py", line 1158, in build_and_installself.run_setup(setup_script, setup_base, args)File "/Users/kangyangwu/Study/Coding/Python/hello/.venv/lib/python3.8/site-packages/setuptools/command/easy_install.py", line 1146, in run_setupraise DistutilsError("Setup script exited with %s" % (v.args[0],))distutils.errors.DistutilsError: Setup script exited with error: SandboxViolation: mkdir('/Users/kangyangwu/Library/Caches/com.apple.python/private/var/folders/5_/7wnzch6s44l8kc2q5dz1gxqw0000gn/T/easy_install-q_4zd0xh', 511) {}The package setup script has attempted to modify files on your systemthat are not within the EasyInstall build area, and has been aborted.This package cannot be safely installed by EasyInstall, and may notsupport alternate installation locations even if you run its setupscript by hand.  Please inform the package's author and the EasyInstallmaintainers to find out if a fix or workaround is available.----------------------------------------
ERROR: Command errored out with exit status 1: python setup.py egg_info Check the logs for full command output.
WARNING: You are using pip version 19.2.3, however version 21.2.4 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.

又出现了莫名其妙的错误,网上找了一会解决办法,都感觉不靠谱,然后我注意到其实一直最后都有个警告信息,让我升级pip,我就准备先把pip升级了看看。

$ pip3 install --upgrade pip
Collecting pipDownloading https://files.pythonhosted.org/packages/ca/31/b88ef447d595963c01060998cb329251648acf4a067721b0452c45527eb8/pip-21.2.4-py3-none-any.whl (1.6MB)|████████████████████████████████| 1.6MB 19kB/s
Installing collected packages: pipFound existing installation: pip 19.2.3Uninstalling pip-19.2.3:Successfully uninstalled pip-19.2.3
Successfully installed pip-21.2.4

好,pip升级完毕,然后鬼使神差地又试了下安装matplotlib。

$ pip3 install matplotlib
Collecting matplotlibDownloading matplotlib-3.4.3-cp38-cp38-macosx_10_9_x86_64.whl (7.2 MB)|████████████████████████████████| 7.2 MB 26 kB/s
WARNING: Retrying (Retry(total=4, connect=None, read=None, redirect=None, status=None)) after connection broken by 'ReadTimeoutError("HTTPSConnectionPool(host='pypi.org', port=443): Read timed out. (read timeout=15)")': /simple/pillow/
Collecting pillow>=6.2.0Downloading Pillow-8.3.2-cp38-cp38-macosx_10_10_x86_64.whl (3.0 MB)|████████████████████████████████| 3.0 MB 17 kB/s
Collecting cycler>=0.10Using cached cycler-0.10.0-py2.py3-none-any.whl (6.5 kB)
Collecting python-dateutil>=2.7Downloading python_dateutil-2.8.2-py2.py3-none-any.whl (247 kB)|████████████████████████████████| 247 kB 13 kB/s
Collecting numpy>=1.16Downloading numpy-1.21.2-cp38-cp38-macosx_10_9_x86_64.whl (16.9 MB)|█████                           | 2.7 MB 22 kB/s eta 0:10:47ERROR: Exception:
Traceback (most recent call last):File "/Users/kangyangwu/Study/Coding/Python/hello/.venv/lib/python3.8/site-packages/pip/_vendor/urllib3/response.py", line 438, in _error_catcheryieldFile "/Users/kangyangwu/Study/Coding/Python/hello/.venv/lib/python3.8/site-packages/pip/_vendor/urllib3/response.py", line 519, in readdata = self._fp.read(amt) if not fp_closed else b""File "/Users/kangyangwu/Study/Coding/Python/hello/.venv/lib/python3.8/site-packages/pip/_vendor/cachecontrol/filewrapper.py", line 62, in readdata = self.__fp.read(amt)File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.8/lib/python3.8/http/client.py", line 458, in readn = self.readinto(b)File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.8/lib/python3.8/http/client.py", line 502, in readinton = self.fp.readinto(b)File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.8/lib/python3.8/socket.py", line 669, in readintoreturn self._sock.recv_into(b)File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.8/lib/python3.8/ssl.py", line 1241, in recv_intoreturn self.read(nbytes, buffer)File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.8/lib/python3.8/ssl.py", line 1099, in readreturn self._sslobj.read(len, buffer)
socket.timeout: The read operation timed outDuring handling of the above exception, another exception occurred:Traceback (most recent call last):File "/Users/kangyangwu/Study/Coding/Python/hello/.venv/lib/python3.8/site-packages/pip/_internal/cli/base_command.py", line 173, in _mainstatus = self.run(options, args)File "/Users/kangyangwu/Study/Coding/Python/hello/.venv/lib/python3.8/site-packages/pip/_internal/cli/req_command.py", line 203, in wrapperreturn func(self, options, args)File "/Users/kangyangwu/Study/Coding/Python/hello/.venv/lib/python3.8/site-packages/pip/_internal/commands/install.py", line 315, in runrequirement_set = resolver.resolve(File "/Users/kangyangwu/Study/Coding/Python/hello/.venv/lib/python3.8/site-packages/pip/_internal/resolution/resolvelib/resolver.py", line 94, in resolveresult = self._result = resolver.resolve(File "/Users/kangyangwu/Study/Coding/Python/hello/.venv/lib/python3.8/site-packages/pip/_vendor/resolvelib/resolvers.py", line 472, in resolvestate = resolution.resolve(requirements, max_rounds=max_rounds)File "/Users/kangyangwu/Study/Coding/Python/hello/.venv/lib/python3.8/site-packages/pip/_vendor/resolvelib/resolvers.py", line 366, in resolvefailure_causes = self._attempt_to_pin_criterion(name)File "/Users/kangyangwu/Study/Coding/Python/hello/.venv/lib/python3.8/site-packages/pip/_vendor/resolvelib/resolvers.py", line 212, in _attempt_to_pin_criterioncriteria = self._get_updated_criteria(candidate)File "/Users/kangyangwu/Study/Coding/Python/hello/.venv/lib/python3.8/site-packages/pip/_vendor/resolvelib/resolvers.py", line 203, in _get_updated_criteriaself._add_to_criteria(criteria, requirement, parent=candidate)File "/Users/kangyangwu/Study/Coding/Python/hello/.venv/lib/python3.8/site-packages/pip/_vendor/resolvelib/resolvers.py", line 172, in _add_to_criteriaif not criterion.candidates:File "/Users/kangyangwu/Study/Coding/Python/hello/.venv/lib/python3.8/site-packages/pip/_vendor/resolvelib/structs.py", line 151, in __bool__return bool(self._sequence)File "/Users/kangyangwu/Study/Coding/Python/hello/.venv/lib/python3.8/site-packages/pip/_internal/resolution/resolvelib/found_candidates.py", line 140, in __bool__return any(self)File "/Users/kangyangwu/Study/Coding/Python/hello/.venv/lib/python3.8/site-packages/pip/_internal/resolution/resolvelib/found_candidates.py", line 128, in <genexpr>return (c for c in iterator if id(c) not in self._incompatible_ids)File "/Users/kangyangwu/Study/Coding/Python/hello/.venv/lib/python3.8/site-packages/pip/_internal/resolution/resolvelib/found_candidates.py", line 32, in _iter_builtcandidate = func()File "/Users/kangyangwu/Study/Coding/Python/hello/.venv/lib/python3.8/site-packages/pip/_internal/resolution/resolvelib/factory.py", line 204, in _make_candidate_from_linkself._link_candidate_cache[link] = LinkCandidate(File "/Users/kangyangwu/Study/Coding/Python/hello/.venv/lib/python3.8/site-packages/pip/_internal/resolution/resolvelib/candidates.py", line 295, in __init__super().__init__(File "/Users/kangyangwu/Study/Coding/Python/hello/.venv/lib/python3.8/site-packages/pip/_internal/resolution/resolvelib/candidates.py", line 156, in __init__self.dist = self._prepare()File "/Users/kangyangwu/Study/Coding/Python/hello/.venv/lib/python3.8/site-packages/pip/_internal/resolution/resolvelib/candidates.py", line 227, in _preparedist = self._prepare_distribution()File "/Users/kangyangwu/Study/Coding/Python/hello/.venv/lib/python3.8/site-packages/pip/_internal/resolution/resolvelib/candidates.py", line 305, in _prepare_distributionreturn self._factory.preparer.prepare_linked_requirement(File "/Users/kangyangwu/Study/Coding/Python/hello/.venv/lib/python3.8/site-packages/pip/_internal/operations/prepare.py", line 508, in prepare_linked_requirementreturn self._prepare_linked_requirement(req, parallel_builds)File "/Users/kangyangwu/Study/Coding/Python/hello/.venv/lib/python3.8/site-packages/pip/_internal/operations/prepare.py", line 550, in _prepare_linked_requirementlocal_file = unpack_url(File "/Users/kangyangwu/Study/Coding/Python/hello/.venv/lib/python3.8/site-packages/pip/_internal/operations/prepare.py", line 239, in unpack_urlfile = get_http_url(File "/Users/kangyangwu/Study/Coding/Python/hello/.venv/lib/python3.8/site-packages/pip/_internal/operations/prepare.py", line 102, in get_http_urlfrom_path, content_type = download(link, temp_dir.path)File "/Users/kangyangwu/Study/Coding/Python/hello/.venv/lib/python3.8/site-packages/pip/_internal/network/download.py", line 145, in __call__for chunk in chunks:File "/Users/kangyangwu/Study/Coding/Python/hello/.venv/lib/python3.8/site-packages/pip/_internal/cli/progress_bars.py", line 144, in iterfor x in it:File "/Users/kangyangwu/Study/Coding/Python/hello/.venv/lib/python3.8/site-packages/pip/_internal/network/utils.py", line 63, in response_chunksfor chunk in response.raw.stream(File "/Users/kangyangwu/Study/Coding/Python/hello/.venv/lib/python3.8/site-packages/pip/_vendor/urllib3/response.py", line 576, in streamdata = self.read(amt=amt, decode_content=decode_content)File "/Users/kangyangwu/Study/Coding/Python/hello/.venv/lib/python3.8/site-packages/pip/_vendor/urllib3/response.py", line 541, in readraise IncompleteRead(self._fp_bytes_read, self.length_remaining)File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.8/lib/python3.8/contextlib.py", line 131, in __exit__self.gen.throw(type, value, traceback)File "/Users/kangyangwu/Study/Coding/Python/hello/.venv/lib/python3.8/site-packages/pip/_vendor/urllib3/response.py", line 443, in _error_catcherraise ReadTimeoutError(self._pool, None, "Read timed out.")
pip._vendor.urllib3.exceptions.ReadTimeoutError: HTTPSConnectionPool(host='files.pythonhosted.org', port=443): Read timed out.

虽然又出现了错误,但是能看到这次下载的matplotlib的包的名称更靠谱了,然后看了下错误,只是个timeout错误,应该是因为国内下载比较慢导致的,所以我准备再给它个机会。

$ pip3 install matplotlib
Collecting matplotlibUsing cached matplotlib-3.4.3-cp38-cp38-macosx_10_9_x86_64.whl (7.2 MB)
Collecting python-dateutil>=2.7Using cached python_dateutil-2.8.2-py2.py3-none-any.whl (247 kB)
Collecting kiwisolver>=1.0.1Downloading kiwisolver-1.3.2-cp38-cp38-macosx_10_9_x86_64.whl (61 kB)|████████████████████████████████| 61 kB 308 kB/s
Collecting numpy>=1.16Downloading numpy-1.21.2-cp38-cp38-macosx_10_9_x86_64.whl (16.9 MB)|████████████████████████████████| 16.9 MB 99 kB/s
Collecting pillow>=6.2.0Using cached Pillow-8.3.2-cp38-cp38-macosx_10_10_x86_64.whl (3.0 MB)
Collecting cycler>=0.10Using cached cycler-0.10.0-py2.py3-none-any.whl (6.5 kB)
Collecting pyparsing>=2.2.1Downloading pyparsing-2.4.7-py2.py3-none-any.whl (67 kB)|████████████████████████████████| 67 kB 112 kB/s
Collecting sixDownloading six-1.16.0-py2.py3-none-any.whl (11 kB)
Installing collected packages: six, python-dateutil, pyparsing, pillow, numpy, kiwisolver, cycler, matplotlib
Successfully installed cycler-0.10.0 kiwisolver-1.3.2 matplotlib-3.4.3 numpy-1.21.2 pillow-8.3.2 pyparsing-2.4.7 python-dateutil-2.8.2 six-1.16.0

Ohhhhh,竟然成功了,太不容易了。。。

matplotlib画图示例

最近在酒店隔离了14天,刚好记录了最近13天的体温记录,刚好用matplotlib可视化出来。
temperatures.txt:

36.4,36.4
36.0,36.8
36.5,36.6
36.5,36.7
36.4,36.8
36.4,36.7
36.3,36.8
36.4,36.5
36.1,36.3
36.0,36.4
36.1,36.3
35.9,36.5
36.1,36.6

代码:

import numpy as np
import matplotlib.pyplot as plt
# 为了在mac里正常显示中文需要设置下字体
plt.rcParams['font.sans-serif'] = ['Songti SC']"""
import matplotlib.font_manager# 展示 matplotlib 已经安装的字体
a = sorted([f.name for f in matplotlib.font_manager.fontManager.ttflist])for i in a:print(i)
"""# 加载温度信息
temperatures = []
with open('temperatures.txt', 'r') as f:for l in f:temperatures.append(list(map(float, l.split(','))))temperatures = np.array(temperatures)
print(temperatures)temp_mean = np.mean(temperatures, axis=0)
print(temp_mean)# 画温度信息
plt.plot(range(1, 14), temperatures[:, 0], color='g', label='上午')
plt.plot(range(1, 14), temperatures[:, 1], color='r', label='下午')plt.plot([0, 14], [temp_mean[0]] * 2, '--', color='g')
plt.plot([0, 14], [temp_mean[1]] * 2, '--', color='r')plt.title('隔离期间体温记录')
plt.ylabel('温度(摄氏度)')
plt.xlabel('天数')
plt.xlim(0, 14)
plt.legend()
plt.show()


从图标里可以看出来一般下午的体温会比上午高,还有前一天下午的体温走势可以预示着第二天早上的体温变化,前面一点从单纯的数据还能看出来,但是第二点只能从图中看出来了,也许这就是数据及其可视化的魅力吧。

Mac上为VS Code配置Python运行环境及matplotlib画图示例相关推荐

  1. python网页运行环境_Python小牛叔Web开发1:安装Visual Studio Code配置Python运行环境...

    本系列是Python小牛叔Web开发系列,以Django为框架介绍如何使用Python来开发Web应用,本教程适合对于Web开发有基本知识的入门者. 1.安装Visual Studio Code编辑器 ...

  2. VS Code配置python运行环境

    安装环境:windows 10 家庭版 x86_64(64位) 1.安装python win 10是直接使用安装包安装的,下载地址 切记在下载安装包后双击安装时一定要选中最底下的一个选项:add pa ...

  3. vs code 开发企业级python_基于VS Code配置Python开发环境

    学习Python也有一段时间了,总感觉学的比较零碎,不成系统,我想应该是因为我没有及时对学习过程进行记录,主要是懒,呵呵~~~那好,以后就在这个平台记录下python学习的点滴吧. 所谓"工 ...

  4. VScode配置Python运行环境

    最近打算用python做一个东西,但是感觉用Pycharm不是特别舒服把,于是自己就打算用万能的VScode配置下环境,然后去找帖子,发现大部分网上的帖子真的水的一批,找了半天配置Python运行环境 ...

  5. Notepad++配置Python运行环境

    转自:http://www.cnblogs.com/zhcncn/p/3969419.html Notepad++配置Python开发环境 1. 安装Python 1 下载 我选择了32位的2.7版本 ...

  6. VS code配置Python开发环境

    目录 前言 Python下载及安装 VS code下载及安装 配置VS code 一.安装python插件 二.创建工作项目 总结 前言 VS code安装简单,扩展性强,来编写Python,也是相当 ...

  7. sublime配置python运行环境

    1.sublime下载与插件管理 1.1 下载 官网地址:https://www.sublimetext.com/3 1.2 安装Package Control管理插件 使用ctrl + ` (感叹后 ...

  8. sublime说python找不到_sublime配置python运行环境

    1.sublime下载与插件管理 1.1 下载 1.2 安装Package Control管理插件 使用ctrl + ` (感叹后左边的那个键位),输入以下内容 import urllib.reque ...

  9. vscode怎么配置python运行环境f5_VSCode下配置python调试运行环境的方法_python

    这篇文章主要介绍了VSCode下配置python调试运行环境的方法,需要的朋友可以参考下 VSCode配置python调试环境 很久之前的一个东东,翻出来看看VSCode配置python调试环境 * ...

最新文章

  1. 用Flutter + Dart快速构建一款绝美移动App
  2. 猜一个介于1-10之间的数字,反复执行直到猜中为止
  3. Trie树详解及其应用
  4. ASP.NET WebForm中用async/await实现异步
  5. java retry(重试) spring retry, guava retrying 详解
  6. CCAI 2017 | 小数据学习对人工智能究竟有着怎样的影响?
  7. linuxz指令大全
  8. 2019windows上安装Mac OS 10.14过程详细截图
  9. ASP.NET MVC 3 Preview1发布
  10. xp系统安装ftp服务器,xp系统安装ftp服务器
  11. Mybiosource丨Mybiosource玻连蛋白 (VTN),ELISA 试剂盒原理
  12. 如何删除桌面右键菜单中的英特尔核心显卡选项
  13. php对接石基crs系统,IDeaS 收益管理系统与石基昆仑CRS 3.10成功对接
  14. 由于找不到 MSVCR120.dll,无法继续执行代码终极解决方法
  15. android 锁屏代码分析,Android锁屏状态下的安全分析
  16. 中山大学3D游戏设计读书笔记 unity3D Note2
  17. 应用于高速计数器的编码器基础知识介绍
  18. 乐创杯c语言大赛形式,【连载】(学了这么多年C语言,你真的了解static关键词吗)乐创DIY C语言讲义​——4.4节...
  19. uniapp微信小程序引入第三方字体库
  20. 和你说说程序员的那点事

热门文章

  1. linux系统如何调屏幕亮度,Linux系统怎样调整屏幕亮度
  2. 谁是卧底在线游戏实战开发thinkphp5+socketio+vue全家桶
  3. 放狗屁、狗放屁和放屁狗
  4. leetcode LCP 30. 魔塔游戏
  5. Android 设置布局背景渐变色
  6. Linux那些事儿之我是U盘(29)将控制传输进行到底
  7. 预计24小时删 | 最后一次分享这套课程资源,得此资源英语突飞猛涨!
  8. php js转码解码函数,javascript如何实现URL的转码与解码?
  9. HTTP代理的应用场景
  10. 一体化低压伺服电机在写真机上的应用