VS Code使用教程

vscode免费使用copilot

1、github学生认证copilot,自行百度学生认证过程,提供学历图片的时候拍一个带头像的校园卡可以通过(实测)
2、vscode安装github copilot扩展
3、vscode左下角登陆github账户
4、为copilot扩展释放权限->enable
在这里插入图片描述
tutorial:https://code.visualstudio.com/docs/copilot/getting-started

copilot基本使用方法:
1、直接在编程时会出现一些自动提示,通过’tab’键可以直接使用
2、通过‘command+i’针对文件中的某一行调用chat进行提问(inline chat模式)'command+enter’对建议代码进行选用
3、通过’command+ctrl+i’调用针对当前文件的chat进行全局提问,

VS code使用.ipynb(jupyter notebook)教程

  1. 您可以通过运行 Jupyter 创建 Jupyter Notebooks:从命令面板(Ctrl+Shift+P)选择** Jupyter: Create Blank New Jupyter Notebook **命令,或在工作区创建新文件.ipynb。
  2. 选择运行内核
    点击右上角选择内核,注意可以选择网页版本jupyter notebook已经配置好的内核,或者codda环境。
    在这里插入图片描述
    对于jupyter中已经配置好的内核,会直接使用。而对于conda环境的内核,第一次选择时如果没有安装过ipykernel,会默认帮你安装ipykernel之后才可以使用。
    也可以自行安装pip install ipykernel

vscode调试jupyter

官方文档:
https://code.visualstudio.com/docs/datascience/jupyter-notebooks#_debug-a-jupyter-notebook

方法一:逐行运行
1、To start, select the Run by Line button in the cell toolbar
在这里插入图片描述
2、‘run next line’ button
在这里插入图片描述
依次执行即可。
3、Continue button执行到结束
You can select the cell Stop button to stop early, or the Continue button in the toolbar to continue running to the end of the cell.

在这里插入图片描述
4、stop button结束此次执行过程
在这里插入图片描述
方法二:如果你想使用类似于VScode当中完整的dubug功能,例如设置断点
通过debug cell

If you want to use the full set of debugging features supported in VS Code, such as breakpoints and the ability to step in to other cells and modules, you can use the full VS Code debugger.
Start by setting any breakpoints you need by clicking in the left margin of a notebook cell.
Then select the Debug Cell button in the menu next to the Run button. This will run the cell in a debug session, and will pause on your breakpoints in any code that runs, even if it is in a different cell or a .py file.
You can use the Debug view, Debug Console, and all the buttons in the Debug Toolbar as you normally would in VS Code.
在这里插入图片描述

链接远程服务器

https://blog.csdn.net/zhaxun/article/details/120568402
http://t.csdnimg.cn/hZBvd
添加链接描述

免密登陆服务器

1mac生成客户机(个人PC)密令

ssh-keygen -t rsa

-t代表生成密令的文件名默认也是rsa
生成的文件在主目录的.ssh文件当中。

cd ~
cd .ssh

查看密令并复制到linux系统当中

cat id_rsa.pub 

在这里插入图片描述

2复制到服务器中

echo "xxxx" >> ~/.ssh/authorized_keys

"XXX"代表的是客户机中生成的密令。
也可以通过复制pc上的id_rsa.pub 到服务器端的~/.ssh/文件夹下,然后直接改名authorized_keys,这样出错的概率小一些。

然后重启服务器的ssh服务。

service sshd restart

在终端界面输入:service sshd start。
如果结果显示:ssh:unrecognized service。

输入查看命令: service ssh status
显示也是 unrecognized service。

是因为没有开启ssh服务。
开启ssh服务:
终端界面键入:sudo /etc/init.d/ssh restart
(或sudo /etc/init.d/ssh start)

3测试

commond(ctrl)+shit+P -> Reload Window -> Enter
在这里插入图片描述
不需要密码,流畅进入。

vscode python远程debug和选环境

在这里插入图片描述
运行时报错找不到文件,可能是相对路径错误:
只要你在每次使用 Code Runner 调试时, cd 一下进入对应的文件夹就可以正常运行的。如果需要更高级的修改,可以参考下面这篇文章。
No such file or directory. 解决VSCode相对路径出错问题

远程debug如何插入命令行参数

通常,我们直接运行vscode F5对代码进行调试的时候,会出现无法插入命令行参数的问题,这对于很多设计了命令行参数的代码不友好。因此可以通过python debug工具,通过launch.json配置对程序进行调试。

第一步、安装python debugger工具

应用程序中搜索并安装

添加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: Current File",
            "type": "python",
            "request": "launch",
            "program": "${file}",
            "console": "integratedTerminal",
            "justMyCode": true
        },
        {
            "name": "Python: Debug",
            "type": "python",
            "request": "launch",
            "program": "${workspaceFolder}/run_batch.py",
            "args": ["-s", "-l", "chatgpt", "-r", "test", "example", "debatrix", "0", "1", "3"],
            "console": "integratedTerminal"
        }
    ]
}

配置完成后,在运行的部分就会显示你配置的调试工具
在这里插入图片描述

在 launch.json 文件中,每个配置项都有不同的作用和配置选项。以下是常见的配置项及其说明:
version:配置文件的版本号,目前为 "0.2.0"。
configurations:调试器的配置列表,包含多个调试配置项。
name:调试配置的名称,用于在 VS Code 中标识该配置。
type:调试器的类型,通常为 "python",表示使用 Python 调试器。
"request":调试请求的类型,常用的值有 "launch" 和 "attach",表示启动调试器或附加到已运行的进程。
"program":要运行的程序的路径,可以是相对路径或绝对路径。

args:传递给程序的命令行参数。可以是一个数组,也可以使用特殊的值,如 "input",表示从文件中读取参数。

console:控制台类型,指定调试器输出的显示位置。常见的值有:
"internalConsole":在 VS Code 的内部控制台中显示输出。
"integratedTerminal":在 VS Code 的集成终端中显示输出。
"externalTerminal":在外部终端中显示输出。
"none":不显示任何输出。

其他配置项:根据需要,还可以配置其他选项,如调试器端口、环境变量等。
通过配置这些选项,你可以定制调试器的行为,以满足你的项目需求。
这些变量使得调试配置更加灵活和通用,因为它们可以根据当前的编辑上下文动态地引用不同的文件和文件夹路径。
在 launch.json 文件中,${file} 和 ${workspaceFolder} 是 VS Code 提供的内置变量,用于在"program"配置调试时引用当前文件和工作区文件夹的路径。
${file}:表示当前打开的文件的完整路径。在调试配置中使用 ${file} 将会引用当前正在编辑的文件的路径。

${workspaceFolder}:表示工作区文件夹的路径。如果你在一个工作区中打开了多个文件夹,${workspaceFolder} 将会是主文件夹的路径。

"justMyCode": true 是调试配置中的一个选项,用于控制调试器在调试会话中仅显示与你自己编写的代码相关的调用栈和异常信息。具体来说,当这个选项设置为 true 时,调试器会忽略第三方库、框架等代码的调用栈,只显示你自己编写的代码的调用栈信息。

这个选项在调试大型项目时特别有用,因为它可以帮助你更快地定位和解决与你自己编写的代码相关的问题,而不会被大量的第三方代码淹没。

vscode debug的时候 相对路径问题

在这里插入图片描述
假设我要debug当前文件目录下DataPreprocess当中的extract…py文件
文件当中的…/ 相对路径无法使用
方法就是json中通过cwd指定运行文件的目录为当前文件的运行目录
${workspaceFolder}指的就是vscdoe当前远程连接的这个文件夹的目录。

{
    // 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 Debugger: extract_logical_expressions_v2.py",
            "type": "debugpy",
            "request": "launch",
            "program": "${workspaceFolder}/DataPreprocess/extract_logical_expressions_v2.py",
            "console": "integratedTerminal",
            "cwd": "${fileDirname}", //用于指定运行文件的目录
            // "cwd": "${workspaceFolder}",
            // "env": { "PYTHONPATH": "${workspaceFolder}" }, //指的是运行文件的根目录
        }
    ]
}

unable to watch for file changes in this large workspace问题

在这里插入图片描述
官方解决方案如下:
官方方案

“Visual Studio Code is unable to watch for file changes in this large workspace” (error ENOSPC)
When you see this notification, it indicates that the VS Code file watcher is running out of handles because the workspace is large and contains many files. Before adjusting platform limits, make sure that potentially large folders, such as Python .venv, are added to the files.watcherExclude setting (more details below). The current limit can be viewed by running:

cat /proc/sys/fs/inotify/max_user_watches

The limit can be increased to its maximum by editing /etc/sysctl.conf (except on Arch Linux, read below) and adding this line to the end of the file:

sudo vim /etc/sysctl.conf

在最下面加上这句话。

fs.inotify.max_user_watches=524288

The new value can then be loaded in by running sudo sysctl -p. #加载新的值

While 524,288 is the maximum number of files that can be watched, if you’re in an environment that is particularly memory constrained, you may want to lower the number. Each file watch takes up 1080 bytes, so assuming that all 524,288 watches are consumed, that results in an upper bound of around 540 MiB.

最新vscode1.86无法远程SSH连接服务器解决方案

20240203做实验的时候发现VS无法连接远程服务器,查询官方文档得知,最新版本的vs远程连接对运行库提出了新的要求。

官方的解决方案有两种:https://code.visualstudio.com/docs/remote/faq#_can-i-run-vs-code-server-on-older-linux-distributions

方案一:

从 VS Code 1.86 版开始,远程服务器构建工具链的最低要求提高了。VS Code 发布的预构建服务器兼容基于 glibc 2.28 或更高版本的 Linux 发行版,例如 Debian 10、RHEL 8 和 Ubuntu 20.04。

方案二:

如果您的设置不符合这些要求,并且无法升级 Linux 发行版,您可以将 VS Code 客户端降级到 1.85 版,以继续使用远程开发功能。您可以在桌面和网络上降级 VS Code 客户端。

举例:对于Visual Studio Code(VSCode),在macOS上安装通常是相对简单的过程,可以按照以下步骤进行:

打开浏览器,访问Visual Studio Code的官方网站:https://code.visualstudio.com/。

在网站上找到并点击 “Download for Mac”,这将会下载一个.zip或.dmg文件。

下载完成后,打开下载的文件。如果是.dmg文件,通常会弹出一个包含Visual Studio Code图标的窗口。

将Visual Studio Code的图标拖拽到Applications(应用程序)文件夹中,完成安装。

如果已经安装过旧版本的VSCode,新版本的安装会直接覆盖旧版本,不需要手动卸载旧版本。

打开Applications文件夹,找到Visual Studio Code并打开。

这样,你就成功安装并启动了Visual Studio Code。VSCode通常会定期发布更新,你可以在官方网站上下载最新版本进行安装,安装过程基本相同。如果你有特定的插件或配置,可能需要在升级前备份它们,以免丢失。

vscode如何设置字体大小

settings -> font

配置python文件的头信息

https://blog.csdn.net/weixin_43804047/article/details/119779655

https://blog.csdn.net/weixin_49927493/article/details/118880460?spm=1001.2101.3001.6650.1&utm_medium=distribute.pc_relevant.none-task-blog-2%7Edefault%7EBlogCommendFromBaidu%7EPaidSort-1-118880460-blog-119779655.235%5Ev43%5Epc_blog_bottom_relevance_base4&depth_1-utm_source=distribute.pc_relevant.none-task-blog-2%7Edefault%7EBlogCommendFromBaidu%7EPaidSort-1-118880460-blog-119779655.235%5Ev43%5Epc_blog_bottom_relevance_base4&utm_relevant_index=2

vscode无法选择conda环境

去应用商店中,搜索python,然后选择切换python版本即可。或者重新安装python
在这里插入图片描述
https://developer.baidu.com/article/details/2800569

如果jupyter无法选择环境,用同样的方法,重新配置jupyter插件就可以了。

pydevd warning:setting the PYDEVD_WARM。。。

https://stackoverflow.com/questions/71695716/pydevd-warnings-in-visual-studio-code-debug-console
修改launch.json文件,设置debug提醒时间为2seconds

"launch": {
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Python: Current File",
            "type": "python",
            "request": "launch",
            "program": "${file}",
            "console": "integratedTerminal",
            "env": {"PYDEVD_WARN_SLOW_RESOLVE_TIMEOUT": "2"}
        }
    ]
}

ssh无法连接

如果多次尝试密码都是是vscode的问题,您可以将实例用户目录下的vscode环境文件删除,再重新连接看看,文件名应该是.vscode-server

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值