【python】搭配Miniconda使用VSCode

102 篇文章 0 订阅
76 篇文章 0 订阅

现在的spyder总是运行出错,启动不了,尝试使用VSCode。

一、在VSCode中使用Miniconda管理的Python环境,可以按照以下步骤进行:

a. 确保Miniconda环境已经安装并且正确配置。

b. 打开VSCode,安装Python扩展。

打开VSCode,进入扩展市场(Extensions)(视觉上通常用四个平行的正方形表示),搜索“Python”,然后选择由Microsoft发布的那个扩展。点击安装。

c. 创建或激活一个Conda环境

d. 在VSCode中设置Python解释器,按`Ctrl+Shift+P`打开命令面板,然后输入`Python: Select Interpreter`,选择Conda环境。也可能会看到在VSCode左下角可以选择和激活不同的Python解释器。

VSCode将会使用在第3步中选择的Miniconda环境下的Python解释器。

二、运行一段2.4. 微积分 — 动手学深度学习 2.0.0 documentation 中的代码

import torch
import numpy as np
from matplotlib_inline import backend_inline
from d2l import torch as d2l
def f(x):
    return 3 * x ** 2 - 4 * x
def numerical_lim(f, x, h):
    return (f(x + h) - f(x)) / h
h = 0.1
for i in range(5):
    print(f'h={h:.5f}, numerical limit={numerical_lim(f, 1, h):.5f}')
    h *= 0.1
def use_svg_display():  #@save
    """使用svg格式在Jupyter中显示绘图"""
    backend_inline.set_matplotlib_formats('svg')
def set_figsize(figsize=(3.5, 2.5)):  #@save
    """设置matplotlib的图表大小"""
    use_svg_display()
    d2l.plt.rcParams['figure.figsize'] = figsize
#@save
def set_axes(axes, xlabel, ylabel, xlim, ylim, xscale, yscale, legend):
    """设置matplotlib的轴"""
    axes.set_xlabel(xlabel)
    axes.set_ylabel(ylabel)
    axes.set_xscale(xscale)
    axes.set_yscale(yscale)
    axes.set_xlim(xlim)
    axes.set_ylim(ylim)
    if legend:
        axes.legend(legend)
    axes.grid()
#@save
def plot(X, Y=None, xlabel=None, ylabel=None, legend=None, xlim=None,
         ylim=None, xscale='linear', yscale='linear',
         fmts=('-', 'm--', 'g-.', 'r:'), figsize=(3.5, 2.5), axes=None):
    """绘制数据点"""
    if legend is None:
        legend = []
    set_figsize(figsize)
    axes = axes if axes else d2l.plt.gca()
    # 如果X有一个轴,输出True
    def has_one_axis(X):
        return (hasattr(X, "ndim") and X.ndim == 1 or isinstance(X, list)
                and not hasattr(X[0], "__len__"))
    if has_one_axis(X):
        X = [X]
    if Y is None:
        X, Y = [[]] * len(X), X
    elif has_one_axis(Y):
        Y = [Y]
    if len(X) != len(Y):
        X = X * len(Y)
    axes.cla()
    for x, y, fmt in zip(X, Y, fmts):
        if len(x):
            axes.plot(x, y, fmt)
        else:
            axes.plot(y, fmt)
    set_axes(axes, xlabel, ylabel, xlim, ylim, xscale, yscale, legend)
x = np.arange(0, 3, 0.1)
plot(x, [f(x), 2 * x - 3], 'x', 'f(x)', legend=['f(x)', 'Tangent line (x=1)'])

这段代码是为了在Jupyter Notebook环境中展示用matplotlib绘制的图形,其中使用了Jupyter Notebook的特定功能,例如使用`matplotlib_inline`后端。如果在VSCode中执行这段代码,可能没有设置正确的环境来显示图形。

在VSCode中查看图形输出通常需要以下步骤:

1. 安装Jupyter扩展:

确保安装了VSCode中的Jupyter扩展,它可以执行Jupyter Notebook中的单元格并查看图形输出。

2. 打开或创建一个Jupyter Notebook:

在VSCode中,可以通过创建`.ipynb`文件来开始,或者通过直接以# %%注释开始一个新的单元格来在普通的.py文件中使用Jupyter的功能。

3. 运行代码:

将上述代码放入一个Jupyter单元格中,然后运行这个单元格,应该能看到图形输出。

如果在普通Python脚本中而非Jupyter Notebook环境中运行这段代码,matplotlib的图形可能不会自动显示。在这种情况下,通常需要在代码末尾添加`d2l.plt.show()`来显式地展示图形:

... # 之前的代码
plot(x, [f(x), 2 * x - 3], 'x', 'f(x)', legend=['f(x)', 'Tangent line (x=1)'])

# 在末尾添加显示图形的命令
d2l.plt.show()

确保已经安装了所有必要的包,例如`matplotlib`,`numpy`以及`d2l`。

  • 17
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

109702008

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值