V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
Tianny
V2EX  ›  Python

请教一个 vscode 关于 Python 的配置问题

  •  
  •   Tianny · 2017-08-08 11:02:58 +08:00 · 4385 次点击
    这是一个创建于 2466 天前的主题,其中的信息可能已经有所发展或是发生改变。

    我有个 hello.py 文件,我要直接运行这个文件,但是引用的 python 是系统自带的,不是我虚拟环境中的。网上资料说需要设置 tasks.json,贴下我 tasks.json 的配置,

    {
        "version": "0.1.0",
        "command": "/Users/apple/github/flasky-blog/venv/bin/python",
        "isShellCommand": true,
        "args": ["${file}"],
        "showOutput": "always"
    }
    

    我已经把 command 的值也就是 python 路径给写死了,但是直接 run hello.py 还是用的是系统的 python。

    不过最让我困惑的是,我已经在 setting.json 中指定了"python.pythonPath": "${workspaceRoot}/venv/bin/python3",而且 debug 是没有问题,再贴下我的 debug.json 配置,

    {
        "version": "0.2.0",
        "configurations": [
            
            {
                "name": "Python",
                "type": "python",
                "request": "launch",
                "stopOnEntry": true,
                "pythonPath": "${config:python.pythonPath}",
                "program": "${file}",
                "cwd": "${workspaceRoot}",
                "env": {},
                "envFile": "${workspaceRoot}/.env",
                "debugOptions": [
                    "WaitOnAbnormalExit",
                    "WaitOnNormalExit",
                    "RedirectOutput"
                ]
            }
        ]
    }
    

    唯独这个 tasks.json,按道理说我已经指定路径了,可是我一 run 就读到系统的 python 路径了。

    给帮忙看看。谢谢

    11 条回复    2017-08-08 18:13:06 +08:00
    Tianny
        2
    Tianny  
    OP
       2017-08-08 12:42:52 +08:00
    问题已经解决了 。我装了个 Run Code 插件。这样就可以直接 run 起来了。当然前提还是要设置 python 解释器的路径,也就是我上文中说的 settting.json 里的内容。所以不需要设置那个 tasks.json。这个插件可以很方便的 run code。
    Tianny
        3
    Tianny  
    OP
       2017-08-08 12:43:07 +08:00
    ivechan
        4
    ivechan  
       2017-08-08 12:45:31 +08:00
    我记得 vscode 还可以随时切换 env 的, 这个也挺方便
    rwecho
        5
    rwecho  
       2017-08-08 14:20:31 +08:00
    F1> interpreter -> Python: Selelct Workspace Interpreter
    bestkayle
        6
    bestkayle  
       2017-08-08 15:00:06 +08:00
    还是 pycharm 好用,嘿嘿。
    Patrick95
        7
    Patrick95  
       2017-08-08 15:32:38 +08:00
    如果要用 VSCode 的断点调试,指定 Python Path,配置里这么写:
    "python.pythonPath": "python3"

    如果用 Code runner 插件:
    "code-runner.executorMap": {
    "python": "python3"
    },
    Patrick95
        8
    Patrick95  
       2017-08-08 15:36:46 +08:00
    补充一下,pythonPath 里写 Python 可执行文件的绝对路径比较好。
    Tianny
        9
    Tianny  
    OP
       2017-08-08 16:23:56 +08:00
    @rwecho 这个其实就会自动生成 setting.json 里的内容
    Tianny
        10
    Tianny  
    OP
       2017-08-08 16:26:20 +08:00
    @Patrick95 这个要看实际情况,因为我的每个项目都是用一个单独的 virtual env,所以写绝对路径就不太适用了
    glasslion
        11
    glasslion  
       2017-08-08 18:13:06 +08:00
    用 pythonVSCode 插件的话, 在已经激活的 virtualenv 里运行 code 会自动使用当前的 virtualenv
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5575 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 07:02 · PVG 15:02 · LAX 00:02 · JFK 03:02
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.