Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

run python command with alias in command line like npm

In node, you can define a package.json. Then define a script block as following:

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

So in root directory, I can just do yarn start to run concurrently -k -r -s first \"yarn test:watch\" \"yarn open:src\" \"yarn lint:watch\"

What is the equivalent of that in Python 3? If I want to have a script called python test to run python -m unittest discover -v

like image 789
leogoesger Avatar asked Jun 21 '18 21:06

leogoesger


3 Answers

Why don't you just use pipenv? It is the python's npm and you can add a [scripts] very similar to the one of npm on your Pipfile.

See this other question to discover more: pipenv stack overflow question

like image 193
EuberDeveloper Avatar answered Sep 30 '22 13:09

EuberDeveloper


Answer specifically for tests, create a setup.py like this within your package/folder:

from setuptools import setup

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

Files are structured like this:

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

Then run python ./setup.py test to run the tests. You need to install setuptools as well (as a default you can use distutils.core setup function but it doesn't include much options).

like image 43
Eric Burel Avatar answered Sep 30 '22 14:09

Eric Burel


Not the best solution really. This totally works if you already familiar with npm, but like others have suggested, use makefiles.

Well, this is a work around, but apparently you can just use npm if you have it installed. I created a file package.json in root directory of python app.

{
"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"
}

then I can just use npm install or yarn install to install all dependencies, and yarn test or npm test to run test scripts.

You can also do preinstall and postinstall hooks. For example, you may need to remove files or create folder structures.

Another benefit is this setup allows you to use any npm libraries like concurrently, so you can run multiple files together and etc.

like image 31
leogoesger Avatar answered Sep 30 '22 13:09

leogoesger