900字范文,内容丰富有趣,生活中的好帮手!
900字范文 > 在VSCode中配置Python3开发环境(win10系统)

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

时间:2020-06-13 10:53:32

相关推荐

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

在VSCode中配置Python开发环境

准备工作选择Python解释器代码测试(单独安装Python的环境下)代码测试(Anaconda管理的Python环境)问题:解决办法:

准备工作

下载并安装VSCode下载并安装Python3环境,需要配置环境变量安装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 pltimport numpy as npx = np.linspace(0, 20, 100) # Create a list of evenly-spaced numbers over the rangeplt.plot(x, np.sin(x)) # Plot the sine of each x pointplt.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 : CommandNotFoundExceptionPS 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 /IntelPython/mkl-servicefrom . import _distributor_initTraceback (most recent call last):File "D:\Anaconda3\envs\pytorch\lib\site-packages\numpy\core\__init__.py", line 24, in <module>from . import multiarrayFile "D:\Anaconda3\envs\pytorch\lib\site-packages\numpy\core\multiarray.py", line 14, in <module>from . import overridesFile "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 pltFile "D:\Anaconda3\envs\pytorch\lib\site-packages\matplotlib\__init__.py", line 139, in <module>from . import cbook, rcsetupFile "D:\Anaconda3\envs\pytorch\lib\site-packages\matplotlib\cbook\__init__.py", line 32, in <module>import numpy as npFile "D:\Anaconda3\envs\pytorch\lib\site-packages\numpy\__init__.py", line 142, in <module>from . import coreFile "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 caninterfere 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/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: 找不到指定的模块。

解决办法:

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

code .

然后再运行示例代码,即可得到正常的输出,终端中的效果如下:

2. 虽然上述方法可以解决问题,但是每次需要在命令行中切换到文件目录下,再使用code .打开VSCode,较为麻烦。另一种解决办法是在VSCode中修改默认的终端。

在终端的右侧下三角符号出展开,选择“选择默认配置文件”选项。

选择“Command Prompt”,点击一下即可。这一方法可以不需要在“Anaconda Powershell Prompt”命令行中切换到文件目录。执行效果如下:

至此,VSCode下的python运行环境就配置好了。

主要参考:/docs/python/python-tutorial

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。