【问题标题】:run python command with alias in command line like npm在命令行中使用别名运行 python 命令,如 npm
【发布时间】:2018-06-21 21:36:44
【问题描述】:

在 node 中,你可以定义一个 package.json。然后定义一个script 块如下:

"scripts": {
    "start": "concurrently -k -r -s first \"yarn test:watch\" \"yarn open:src\" \"yarn lint:watch\"",
  },

所以在根目录下,我可以通过yarn start 来运行concurrently -k -r -s first \"yarn test:watch\" \"yarn open:src\" \"yarn lint:watch\"

在 Python 3 中相当于什么?如果我想要一个名为python test 的脚本来运行python -m unittest discover -v

【问题讨论】:

  • 查看setup.py test

标签: python node.js npm


【解决方案1】:

为什么不直接使用 pipenv?它是 python 的 npm,您可以在 Pipfile 上添加与 npm 非常相似的 [scripts]。

查看其他问题以了解更多信息:pipenv stack overflow question

【讨论】:

  • 这应该是最好的答案
【解决方案2】:

专门针对测试回答,在您的包/文件夹中创建一个像这样的setup.py:

from setuptools import setup

setup(name='Your app',
      version='1.0',
      description='A nicely tested app',
      packages=[],
      test_suite="test"
      )

文件的结构如下:

my-package/
  | setup.py
  | test/
  | some_code/
  | some_file.py

然后运行python ./setup.py test 运行测试。您还需要安装setuptools(默认情况下您可以使用distutils.core 设置功能,但它不包含太多选项)。

【讨论】:

    【解决方案3】:

    使用make,非常棒。

    创建一个Makefile,就像Dockerfile:

    
    install:
        pip install -r requirements.txt
    
    test:
        python -m unittest discover -v
    
    
    # and so on, you got the idea
    

    运行(假设Makefile 在当前目录中):

    make test
    

    【讨论】:

    • 现在我才意识到最好的答案是使用 pipenv 的答案,但是 make 老实说更容易输入。
    【解决方案4】:

    确实不是最好的解决方案。如果您已经熟悉 npm,这完全可行,但就像其他人建议的那样,使用 makefile。

    好吧,这是一个变通办法,但显然你可以使用npm,如果你安装了它。我在 python 应用程序的根目录中创建了一个文件package.json。

    {
    "name": "fff-connectors",
    "version": "1.0.0",
    "description": "fff project to UC Davis",
    "directories": {
        "test": "tests"
    },
    "scripts": {
        "install": "pip install -r requirements.txt",
        "test": "python -m unittest discover -v"
    },
    "keywords": [],
    "author": "Leo Qiu",
    "license": "ISC"
    }
    

    然后我可以使用npm install 或yarn install 安装所有依赖项,并使用yarn test 或npm test 运行测试脚本。

    您也可以使用preinstall 和postinstall 挂钩。例如,您可能需要删除文件或创建文件夹结构。

    另一个好处是此设置允许您使用任何 npm 库,例如 concurrently,因此您可以一起运行多个文件等等。

    【讨论】:

    • 我不喜欢这种解决方法,我的意思是,真的有人安装 npm 只是为了使用它的脚本功能并用 python 编写其他所有东西吗?
    猜你喜欢
    • 2018-12-28
    • 1970-01-01
    • 2019-10-04
    • 2014-06-01
    • 2016-10-17
    • 2022-12-15
    • 2020-07-12
    • 2017-01-11
    • 2012-10-05
    相关资源
    最近更新 更多