VScode搭建Python虚拟环境,并且使用两种方法进行(code runner插件VSPython插件)编译

6 篇文章 0 订阅
2 篇文章 0 订阅

之前用的是PyCharm,但是这段时间注册码过期了,又找不到新的注册码,干脆暂时弃坑,转而投入VScode的大家庭。

下载安装VScode,python以及在VScode中安装python的插件,codeRunner插件这里就不赘述了,百度一下都有。关于虚拟环境这块,有兴趣可以翻一下我之前的博客,里面有简短的描述。

这里我在E盘下新建一个文件夹:mypython,在mypython下新建一个py空文件,main.py,并建立一个空文件venv,

打开cmd,进入安装python 的路径下,我这里装的3.7版本,

cd C:\Users\你的用户名\AppData\Local\Programs\Python\Python37\Scripts

安装virtulenv

pip install virtualenv

创建虚拟环境

C:\Users\你的用户名\AppData\Local\Programs\Python\Python37\Scripts\virtualenv  E:\mypython\venv

其实这后面还可以加一些命令,感兴趣的可以 试一下命令,virtualenv  -h

接下来 VScode中打开设置(ctrl+,),输入python:env

如上图所示,选择workspace,python:venv path 就填写存放虚拟环境的文件夹,最后一步填写Add Item,就填写虚拟环境venv

然后此时我们点VScode左下角,选择我们的虚拟环境

如上图,选择第二个也就是我们刚搭建的虚拟环境。

后面还需要配置一下文件,win7和win10 是有区别的,

在.vscode文件夹下,launch.json

{    
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        
        {"name":"Python: 当前文件","type":"python","request":"launch","program":"${file}","console":"integratedTerminal"},
        {
            "name": "Python: 当前文件",
            "type": "python",
            "request": "launch",
            "program": "${file}",
            "console": "externalTerminal",//integratedTerminal" 选择内外置终端
        }
    ]
}

win10和win7下的settings.json文件有所区别

win10版

{
   "python.venvPath": "E:\\mypython",
    "python.venvFolders": [
        "venv"
    ],
    "python.pythonPath": "E:\\mypython\\venv\\Scripts\\python.exe",
    //"terminal.integrated.shell.windows": "C:\\Windows\\System32\\cmd.exe",
    
        "files.defaultLanguage": "python", // ctrl+N新建文件后默认的语言
        "editor.formatOnType": false, // 输入分号(C/C++的语句结束标识)后自动格式化当前这一行的代码
        "editor.suggest.snippetsPreventQuickSuggestions": false, // clangd的snippets有很多的跳转点,不用这个就必须手动触发Intellisense了
        "editor.acceptSuggestionOnEnter": "off", // 我个人的习惯,按回车时一定是真正的换行,只有tab才会接受Intellisense
        // "editor.snippetSuggestions": "top", // (可选)snippets显示在补全列表顶端,默认是inline
        "code-runner.runInTerminal": true, // 设置成false会在“输出”中输出,无法输入
        "code-runner.executorMap": {
            // "c": "cd $dir && gcc '$fileName' -o '$fileNameWithoutExt.exe' -Wall -g -O2 -static-libgcc -std=c11 -fexec-charset=GBK && &'$dir$fileNameWithoutExt'",
            //"cpp": "cd $dir && g++ '$fileName' -o '$fileNameWithoutExt.exe' -Wall -g -O2 -static-libgcc -std=c++17 -fexec-charset=GBK && &'$dir$fileNameWithoutExt'"
           "python":"python -u",
        }, // 右键run code时运行的命令;未注释的仅适用于PowerShell(Win10默认),文件名中有空格也可以编译运行;注释掉的适用于cmd(win7默认),PS和bash也能用,但文件名中有空格时无法运行
        "code-runner.saveFileBeforeRun": true, // run code前保存
        "code-runner.preserveFocus": true, // 若为false,run code后光标会聚焦到终端上。如果需要频繁输入数据可设为false
        "code-runner.clearPreviousOutput": false, // 每次run code前清空属于code runner的终端消息,默认false
        "code-runner.ignoreSelection": true, // 默认为false,效果是鼠标选中一块代码后可以单独执行,但C是编译型语言,不适合这样用
        // "terminal.integrated.shell.windows": "C:\\Windows\\System32\\cmd.exe",
        //"terminal.integrated.shell.windows": "powershell.exe",
}

win7版

{
 "python.venvPath": "E:\\mypython",
    "python.venvFolders": [
        "venv"
    ],
    "python.pythonPath": "E:\\mypython\\venv\\Scripts\\python.exe",
    //"terminal.integrated.shell.windows": "C:\\Windows\\System32\\cmd.exe",
    
        "files.defaultLanguage": "python", // ctrl+N新建文件后默认的语言
        "editor.formatOnType": false, // 输入分号(C/C++的语句结束标识)后自动格式化当前这一行的代码
        "editor.suggest.snippetsPreventQuickSuggestions": false, // clangd的snippets有很多的跳转点,不用这个就必须手动触发Intellisense了
        "editor.acceptSuggestionOnEnter": "off", // 我个人的习惯,按回车时一定是真正的换行,只有tab才会接受Intellisense
        // "editor.snippetSuggestions": "top", // (可选)snippets显示在补全列表顶端,默认是inline
        "code-runner.runInTerminal": true, // 设置成false会在“输出”中输出,无法输入
        "code-runner.executorMap": {
            // "c": "cd $dir && gcc '$fileName' -o '$fileNameWithoutExt.exe' -Wall -g -O2 -static-libgcc -std=c11 -fexec-charset=GBK && &'$dir$fileNameWithoutExt'",
            //"cpp": "cd $dir && g++ '$fileName' -o '$fileNameWithoutExt.exe' -Wall -g -O2 -static-libgcc -std=c++17 -fexec-charset=GBK && &'$dir$fileNameWithoutExt'"
           "python":"python -u",
        }, // 右键run code时运行的命令;未注释的仅适用于PowerShell(Win10默认),文件名中有空格也可以编译运行;注释掉的适用于cmd(win7默认),PS和bash也能用,但文件名中有空格时无法运行
        "code-runner.saveFileBeforeRun": true, // run code前保存
        "code-runner.preserveFocus": true, // 若为false,run code后光标会聚焦到终端上。如果需要频繁输入数据可设为false
        "code-runner.clearPreviousOutput": false, // 每次run code前清空属于code runner的终端消息,默认false
        "code-runner.ignoreSelection": true, // 默认为false,效果是鼠标选中一块代码后可以单独执行,但C是编译型语言,不适合这样用
       //  "terminal.integrated.shell.windows": "C:\\Windows\\System32\\cmd.exe",
        //"terminal.integrated.shell.windows": "powershell.exe", 
}

最后,win10版本可能会遇到activate.ps1文件无法执行,可以参考前面的连接修改,有可能需要以管理员身份运行命令

set-executionpolicy remotesigned 

当然你要想改回来,命令改一下就行

set-executionpolicy Restricted  

参考链接:

1.https://blog.csdn.net/lumping/article/details/101547715

2.https://blog.csdn.net/ebzxw/article/details/85019887

3.https://www.zhihu.com/question/30315894/answer/154979413

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值