[英]How to run python module command from VSCode tasks
I am trying to run my alembic migration which is in a subdirectory so I am writing VSCode tasks for it but when I run it I get command not found
error:我正在尝试运行位于子目录中的 alembic 迁移,因此我正在为它编写 VSCode 任务,但是当我运行它时,我收到
command not found
错误:
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "Alembic autogenerated revision",
"type": "shell",
"command": "alembic revision --autogenerate -m init",
"options": {
"cwd": "${workspaceFolder}/my_sub_directory"
}
}
]
}
How can I run it under my virtual environment?如何在我的虚拟环境下运行它?
Activating the virtual environment, then installing alembic
, you can run alembic revision
directly, that's because it's in the virtual environment and able to call the module.激活虚拟环境,然后安装
alembic
,可以直接运行alembic revision
,因为它在虚拟环境中,可以调用模块。
So, when executing alembic
command in task, you also need to activate env first.所以,在task中执行
alembic
命令时,还需要先激活env 。
Copy the activation command and paste it before alembic and separated by ;
复制激活命令并将其粘贴在 alembic 之前并用
;
分隔, the task should be ran successfully: ,任务应该运行成功:
Unfortunately, I've not been able to find a solution that doesn't require hardcoding the virtualenv path.不幸的是,我找不到不需要硬编码 virtualenv 路径的解决方案。
I believe an extension would be able to solve this, possibly by adding support for python
in the tasks's type
field.我相信扩展能够解决这个问题,可能通过在任务的
type
字段中添加对python
的支持。
Use launch configurations instead of tasks for running python scripts.使用启动配置而不是任务来运行 python 脚本。 It is a bit weird, but it works.
这有点奇怪,但它有效。
Here's your code adapted to launch.json
schema:这是适用于
launch.json
架构的代码:
{
"version": "0.2.0",
"configurations": [
{
"name": "Alembic autogenerated revision",
"type": "python",
"request": "launch",
"module": "alembic",
"args": ["revision", "--autogenerate", "-m", "init"],
"cwd": "${workspaceFolder}/my_sub_directory"
}
]
}
Also, consider using input variables for specifying revision messages and other variable things.此外,考虑使用输入变量来指定修订消息和其他变量。
Thanks for your responses.感谢您的回复。 To resolve this we can use the selected interpreter path and run the module.
为了解决这个问题,我们可以使用选定的解释器路径并运行模块。
{
"label": "Alembic autogenerated revision",
"type": "shell",
"command": "${command:python.interpreterPath} -m alembic revision --autogenerate -m init",
"options": {
"cwd": "${workspaceFolder}/ts_tools"
}
},
Reference: https://github.com/microsoft/vscode-python/issues/18728参考: https://github.com/microsoft/vscode-python/issues/18728
Regards.问候。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.