Python虚拟环境在VSCode上面进行配置

项目根目录下F:\hjhcos\server\创建虚拟环境python -m venv .\.env
vscode会检测到当前创建的python虚拟环境,并询问你是否修改python的工作区。
查看Python执行器的位置

import sys 
sys.executable

接下来我们配置调试文件,在根目录下创建.vscode\launch.json

{
	"version": "0.0.1",
    "configurations": [
        {
            "name": "Python: 当前文件",
            "type": "python",
            "request": "launch",
            "program": "${file}",
            "console": "integratedTerminal"
        }
    ]
}

进入虚拟环境./.env/Scripts/activate

无法加载文件 F:\hjhcos\server\.env\Scripts\Activate.ps1,因为在此系统上
禁止运行脚本。有关详细信息,请参阅 https:/go.microsoft.com/fwlink/?LinkID=1
35170 中的 about_Execution_Policies。

对于上面的报错是由PowerShell的执行策略引起的,因为其禁止了.ps1脚本的运行。
在PowerShell命令窗口里面修改执行策略Set-ExecutionPolicy RemoteSigned

执行策略更改
执行策略可帮助你防止执行不信任的脚本。更改执行策略可能会产生安全风险,如 https:/go.microsoft.com/fwlink/?LinkID=135170
中的 about_Execution_Policies 帮助主题所述。是否要更改执行策略?
[Y](Y)  [A] 全是(A)  [N](N)  [L] 全否(L)  [S] 暂停(S)  [?] 帮助 (默认值为“N”): Y

在项目里查看第三方库安装路径 python -m site --user-site

C:\Users\19095\AppData\Roaming\Python\Python39\site-packages

可以看到库的路径并不是在项目目录下,需要修改成第三方库的所在地
查看配置文件 python -m site -help

D:\Python\lib\site.py [--user-base] [--user-site]

Without arguments print some useful information
With arguments print the value of USER_BASE and/or USER_SITE separated
by ';'.

Exit codes with --user-base or --user-site:
  0 - user site directory is enabled
  1 - user site directory is disabled by user
  2 - user site directory is disabled by super user
      or for security reasons
 >2 - unknown error

根据上面的提示,打开D:\Python\lib\site.py文件修改相应的配置

# for distutils.commands.install
# These values are initialized by the getuserbase() and getusersitepackages()
# functions, through the main() function when Python starts.
# USER_SITE = None
# USER_BASE = None
# 第三放库的路径
USER_SITE = "F:\hjhcos\server\.env\Lib\site-packages"
# pip python等等脚本路径
USER_BASE = "F:\hjhcos\server\.env\Scripts"

这样使用pip install安装第三方库就会加载到当前项目路径下。当然你也可以使用python -m site --user-site查看位置是否正确。

如果觉得繁琐可以使用pycharm,可以自动配置虚拟环境
如果觉得繁琐可以使用pycharm,可以自动配置虚拟环境
如果觉得繁琐可以使用pycharm,可以自动配置虚拟环境

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

hjhcos

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

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

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

打赏作者

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

抵扣说明:

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

余额充值