run python command with alias in command line like npm
Asked Answered
S

4

22

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

Suffruticose answered 21/6, 2018 at 21:36 Comment(1)
look into setup.py testHarmaning
Z
26

use make, its great.

create a Makefile and add some targets to run specific shell commands:


install:
    pip install -r requirements.txt

test:
    python -m unittest discover -v


# and so on, you got the idea

run with (assuming that Makefile is in the current dir):

make test

NOTE: if you want to run more commands but in the same environment from within a target do this:

install:
    source ./venv/bin/activate; \
    pip install -r requirements.txt; \
    echo "do other stuff after in the same environment"

the key is the ;\ which puts the commands in a single run and make executes these commands as a single line because of the ;\. the space in ; \ its just for aesthetics.

Zaneta answered 1/1, 2022 at 0:20 Comment(1)
right now i just realised that best answer is the one with pipenv, but make its easier to type, to be honest.Zaneta
P
13

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

Piling answered 19/3, 2021 at 15:5 Comment(1)
this should be the best answerZaneta
S
6

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.

Suffruticose answered 21/6, 2018 at 22:7 Comment(2)
I don't like this workaround, I mean, should really anyone install npm only to use its scripts functionality and write everything else with python?Piling
Thanks. This was what I was looking for. My use case is mostly for deploying from local and I work with npm in js components. Therefore this is the solution that will eat less headspace since I can use the same command for deploying bothSelfimprovement
T
4

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).

Tajuanatak answered 20/10, 2021 at 8:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.