在VSCode中配置Python3开发环境(win10系统)

准备工作

  1. 下载并安装VSCode
  2. 下载并安装Python3环境,需要配置环境变量
  3. 安装VSCode中的python扩展
    在这里插入图片描述

选择Python解释器

在VSCode中使用Ctrl+Shift+P快捷键打开命令窗口,输入“Python”,选择“Python: Select Interpreter”,然后选择一个合适的python解释器。
在这里插入图片描述

代码测试(单独安装Python的环境下)

选择/新建一个文件夹,用于存放python代码。然后在VSCode中通过File->Open Folder…打开此文件夹。最后在打开的资源管理器界面,点击刚刚打开的文件夹,新建文件test.py文件,如下图。
在这里插入图片描述
在新建的test.py文件中写入以下测试代码:

import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0, 20, 100)  # Create a list of evenly-spaced numbers over the range
plt.plot(x, np.sin(x))       # Plot the sine of each x point
plt.show()                   # Display the plot

点击文件右边的运行按键运行代码,或者在py文件右击,选择“在终端中运行python文件”。
输出结果:
在这里插入图片描述

代码测试(Anaconda管理的Python环境)

问题:

若电脑在配置python环境时没有单独安装python,而是通过Anaconda进行的python安装和包管理,那么在执行上述代码时会报错,错误示例如下:

conda : 无法将“conda”项识别为 cmdlet、函数、脚本文件或可运行程序的名    
称。请检查名称的拼写,如果包括路径,请确保路径正确,然后再试一次。      
所在位置 行:1 字符: 1
+ conda activate pytorch
+ ~~~~~
    + CategoryInfo          : ObjectNotFound: (conda:String) [], Comma  
   ndNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException
 
PS E:\hello> & D:/Anaconda3/envs/pytorch/python.exe e:/hello/test.py    
D:\Anaconda3\envs\pytorch\lib\site-packages\numpy\__init__.py:140: UserWarning: mkl-service package failed to import, therefore Intel(R) MKL initialization ensuring its correct out-of-the box operation under condition when Gnu OpenMP had already been loaded by Python process is not assured. Please install mkl-service package, see http://github.com/IntelPython/mkl-service
  from . import _distributor_init
Traceback (most recent call last):
  File "D:\Anaconda3\envs\pytorch\lib\site-packages\numpy\core\__init__.py", line 24, in <module>
    from . import multiarray
  File "D:\Anaconda3\envs\pytorch\lib\site-packages\numpy\core\multiarray.py", line 14, in <module>
    from . import overrides
  File "D:\Anaconda3\envs\pytorch\lib\site-packages\numpy\core\overrides.py", line 7, in <module>
    from numpy.core._multiarray_umath import (
ImportError: DLL load failed: 找不到指定的模块。

During handling of the above exception, another exception occurred:     

Traceback (most recent call last):
  File "e:/hello/test.py", line 1, in <module>
    import matplotlib.pyplot as plt
  File "D:\Anaconda3\envs\pytorch\lib\site-packages\matplotlib\__init__.py", line 139, in <module>
    from . import cbook, rcsetup
  File "D:\Anaconda3\envs\pytorch\lib\site-packages\matplotlib\cbook\__init__.py", line 32, in <module>
    import numpy as np
  File "D:\Anaconda3\envs\pytorch\lib\site-packages\numpy\__init__.py", 
line 142, in <module>
    from . import core
  File "D:\Anaconda3\envs\pytorch\lib\site-packages\numpy\core\__init__.py", line 54, in <module>
    raise ImportError(msg)
ImportError:

IMPORTANT: PLEASE READ THIS FOR ADVICE ON HOW TO SOLVE THIS ISSUE!      

Importing the numpy c-extensions failed.
- Try uninstalling and reinstalling numpy.
- If you have already done that, then:
  1. Check that you expected to use Python3.7 from "D:\Anaconda3\envs\pytorch\python.exe",
     and that you have no directories in your PATH or PYTHONPATH that can
     interfere with the Python and numpy version "1.18.1" you're trying 
to use.
  2. If (1) looks fine, you can open a new issue at
     https://github.com/numpy/numpy/issues.  Please include details on: 
     - how you installed Python
     - how you installed numpy
     - your operating system
     - whether or not you have multiple versions of Python installed    
     - if you built from source, your compiler versions and ideally a build log

- If you're working with a numpy git repository, try `git clean -xdf`   
  (removes all files not under version control) and rebuild numpy.      

Note: this error has many possible causes, so please don't comment on   
an existing issue about this - open a new one instead.

Original error was: DLL load failed: 找不到指定的模块。

解决办法:

  1. 在“Anaconda Powershell Prompt”命令行中使用如下命令来打开VSCode(需要在命令行中先cd到示例代码所在的文件夹)。
code .

在这里插入图片描述
然后再运行示例代码,即可得到正常的输出,终端中的效果如下:
在这里插入图片描述
2. 虽然上述方法可以解决问题,但是每次需要在命令行中切换到文件目录下,再使用code .打开VSCode,较为麻烦。另一种解决办法是在VSCode中修改默认的终端。
在这里插入图片描述
在终端的右侧下三角符号出展开,选择“选择默认配置文件”选项。
在这里插入图片描述
选择“Command Prompt”,点击一下即可。这一方法可以不需要在“Anaconda Powershell Prompt”命令行中切换到文件目录。执行效果如下:
在这里插入图片描述
至此,VSCode下的python运行环境就配置好了。
主要参考:https://code.visualstudio.com/docs/python/python-tutorial

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值