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

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 matplotlib
  Downloading https://files.pythonhosted.org/packages/21/37/197e68df384ff694f78d687a49ad39f96c67b8d75718bc61503e1676b617/matplotlib-3.4.3.tar.gz (37.9MB)
     |████████████████████████████████| 37.9MB 222kB/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-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-info
         cwd: /private/var/folders/5_/7wnzch6s44l8kc2q5dz1gxqw0000gn/T/pip-install-9p2d2613/matplotlib/
    Complete output (106 lines):
    Processing numpy/random/_bounded_integers.pxd.in
    Processing numpy/random/_philox.pyx
    Traceback (most recent call last):
      File "/var/folders/5_/7wnzch6s44l8kc2q5dz1gxqw0000gn/T/easy_install-o6uaod2w/numpy-1.21.2/tools/cythonize.py", line 59, in process_pyx
        import Cython
    ModuleNotFoundError: 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 main
        find_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_files
        process(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 process
        processor_function(fromfile, tofile)
      File "/var/folders/5_/7wnzch6s44l8kc2q5dz1gxqw0000gn/T/easy_install-o6uaod2w/numpy-1.21.2/tools/cythonize.py", line 66, in process_pyx
        raise OSError(msg) from e
    OSError: Cython needs to be installed in Python as a module
    Running 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_modules
        yield saved
      File "/Users/kangyangwu/Study/Coding/Python/hello/.venv/lib/python3.8/site-packages/setuptools/sandbox.py", line 195, in setup_context
        yield
      File "/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 _execfile
        exec(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_package
      File "/var/folders/5_/7wnzch6s44l8kc2q5dz1gxqw0000gn/T/easy_install-o6uaod2w/numpy-1.21.2/setup.py", line 236, in generate_cython
    
    RuntimeError: 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_requires
        dist.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_eggs
        resolved_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 resolve
        dist = 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_match
        return self.obtain(req, installer)
      File "/Users/kangyangwu/Study/Coding/Python/hello/.venv/lib/python3.8/site-packages/pkg_resources/__init__.py", line 1077, in obtain
        return installer(requirement)
      File "/Users/kangyangwu/Study/Coding/Python/hello/.venv/lib/python3.8/site-packages/setuptools/dist.py", line 786, in fetch_build_egg
        return 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_install
        return 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_item
        dists = 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_eggs
        return 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_install
        self.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_setup
        run_setup(setup_script, args)
      File "/Users/kangyangwu/Study/Coding/Python/hello/.venv/lib/python3.8/site-packages/setuptools/sandbox.py", line 253, in run_setup
        raise
      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/setuptools/sandbox.py", line 195, in setup_context
        yield
      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 
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值