在mac上使用vscode创建第一个Python项目

本文介绍了如何在Mac上使用VSCode创建第一个Python项目。首先确保安装了VSCode和Python插件,然后创建一个Python文件夹,配置Python解释器路径。接着在VSCode中设置工作目录,并通过配置launch.json文件来实现Python程序的调试。最后,通过F5键即可启动调试模式,开始在VSCode中愉快地进行Python开发。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

准备工作:

  • 安装好vscode
  • 安装插件『Python』

正式开始:

首先是创建一个空的文件夹(比如文件夹为test),然后在其中新建一个.py文件(比如文件为hello.py),接着在vscode中打开test文件夹作为工作目录,首先需要配置一些python解释器的路径:commond+,进入设置界面,输入[python.pythonPath],然后讲路径修改为自定义的Python路径,比如[/usr/local/anaconda3/bin/python](我是使用的anaconda),到此为止,我们已经可以运行python程序了,但是如果需要调制Python程序的话,还需要接下来的一步配置:

首先点击右侧调试图标

接着点击齿轮符号,选择python file,会自动在.vscode目录下生成一个luanch.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 (Integrated Terminal)",
            "type": "python",
            "request": "launch",
            "program": "${file}",
            "console": "integratedTerminal",
            "stopOnEntry": true
        },
        {
            "name": "Python: Remote Attach",
            "type": "python",
            "request": "attach",
            "port": 5678,
            "host": "localhost",
            "pathMappings": [
                {
                    "localRoot": "${workspaceFolder}",
                    "remoteRoot": "."
                }
            ]
        },
        {
            "name": "Python: Module",
            "type": "python",
            "request": "launch",
            "module": "enter-your-module-name-here",
            "console": "integratedTerminal"
        },
        {
            "name": "Python: Django",
            "type": "python",
            "request": "launch",
            "program": "${workspaceFolder}/manage.py",
            "console": "integratedTerminal",
            "args": [
                "runserver",
                "--noreload",
                "--nothreading"
            ],
            "django": true
        },
        {
            "name": "Python: Flask",
            "type": "python",
            "request": "launch",
            "module": "flask",
            "env": {
                "FLASK_APP": "app.py"
            },
            "args": [
                "run",
                "--no-debugger",
                "--no-reload"
            ],
            "jinja": true
        },
        {
            "name": "Python: Current File (External Terminal)",
            "type": "python",
            "request": "launch",
            "program": "${file}",
            "console": "externalTerminal"
        }
    ]
}

如果需要调试,那就按F5,进入调试模式即可

好了,这样就可以顺利地在vscode上进行Python发啦,赶快行动起来吧~

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值