新手笔记:[Window Visual Studio] 使用pip install安装python library 以及如何创建虚拟环境

楼主是个python小白,个人比较喜欢用visual studio作为写python代码的界面,原因是好用好看,F2一键方便查variables。但是在写代码的过程中总是遇到很多library安装了却一直用不到的情况。原因是安装环境和debug的环境对不上。这里我写个笔记,根据Visual Studio,具体操作过程避免这个问题。
声明:全部来源都来自于互联网(Youtube教程,由本人整理并且加上了一些理解。如果信息有误请评论区纠正。另外楼主中文不好,如果有错别字多多见谅。

  1. (建议)首先,在 EXPLORER 导入文件夹。这里的文件夹是你的项目文件夹。个人建议每个新的项目开一个特定的文件夹来保存所有代码源。
  2. 保存以下代码(这里先试着安装一下3个library,后续如果有新的评论区补充,我再编辑过)
    import scipy.io
    import numpy as np
    import matplotlib.pyplot as plt
    运行 numpy 简单的例子:
    import numpy as np
    # create a 2D array
    my_array = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
    # print the array
    print(my_array)
    # get the shape of the array
    print(my_array.shape)
    # get the datatype of the array
    print(my_array.dtype)
    # perform some basic arithmetic on the array
    result = my_array + 2print(result)
    # calculate the mean of the array
    mean = np.mean(my_array)print(mean)

  3. 打开新的Terminal(如图示),然后选择 command promt (因为后续需要安装library在prompt)


  4. 创建新的Virtual Environment.  在Command Promt 打上:
    python -m venv YourVirtualEnvironmentName
    这里你的virtual environment name随便定义。
    为什么需要Virtual Environment 虚拟环境 ? 附上Chatgpt答案:

    By using virtual environments, developers can create a clean and self-contained environment for each project, making it easier to manage dependencies and ensuring a smoother development experience.

    然后你会看到这个提示:

    点击 Yes 就可以了。这时候不管你安装了什么library都是在这个virtual environment的基础上,这样不会影响到其他文件的操作。建议在开始每个新的项目都创建一个新的Virtual Environment. 虚拟环境。
    现在需要先激活Virtual Environment虚拟环境:
     
    YourVirtualEnvironmentName\Scripts\activate.bat
    如果激活成功在文件路径前面会显示 YourVirtualEnvironmentName。这里也可以不需要加.bat。激活文件可以在这里找到:

    我这里的虚拟环境名字是 nntrain 。如果虚拟环境创建成功,这里也会显示 nntrain 的路劲,包含了 Include, Lib, Scripts

    接下来所有活动都将在这个Virtual Environment进行,过后安装好的Library也都会在Scripts被找到。

    注意,如果现在已经有了虚拟环境,就能直接使用 pip install 前面不需要加上 python3 -m ,这是因为虚拟环境已经有默认的python解析器了。

    附上Chatgpt的解释:

    When you set up a virtual environment in Visual Studio Code, you activate the environment, and it becomes the default Python interpreter for that specific project. Once the virtual environment is activated, you don't need to use "python3" explicitly because the system recognizes the active Python interpreter.
     
  5. 安装Library
     
    pip install numpy
    pip install scipy
    pip install matplotlib
    pip install tensorflow[and-cuda]
    
    注意,在这之前这里可能会要求安装python,跟着提示进行安装就可以了。已经安装python的小伙伴,若有提示建议更新python,可按照提示代码进行更新。

    当安装成功的时候,就不会有虚线在代码下面了。
  6. 运行代码可以有几种方式,直接在同一个commnd窗口,最简单粗暴:
    python YourProjectName.py
    或者 按 F5 Debug。但是F5 Debug的话通常会在(base)而不是刚创建的虚拟环境(YourVirtualEnvironmentName),代码一样能跑,可创建虚拟环境的意义就没有了。

  7. Enjoy!~

  • 24
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值