简体   繁体   中英

invalid command 'npm run build' when running it from setup.py

I am trying to build the Javascript end of my app using npm run build via the setup.py config file. I am using the build class from distutils as suggested elsewhere, but I am getting an error when I run pip install.

from setuptools import setup
from distutils.command.build import build
import json
import os

class javascript_build(build):
    def run(self):
        self.run_command("npm run build")
        build.run(self)

if __name__ == "__main__":
    setup(
        cmdclass={'build': javascript_build},
         )

Does anyone know why is this happening?

 running npm run build
 error: invalid command 'npm run build'
 ----------------------------------------
 ERROR: Failed building wheel for chemiscope

EDIT 1: So it seems that instead of running npm run build , it is running python setup.py npm run build . So my question changes a little to how do I exactly force distutils to run npm run build ?

self.run_command("xxx") doesn't run a program — it calls another distutils / setuptools subcommand; something like calling python setup.py xxx but from the same process, not via the command line. So you can do self.run_command("sdist") but not self.run_command("npm") .

In your case you need os.system("npm run build") or subprocess.call("npm run build") .

I have managed to get this working by using subprocess.check_output() as shown below. I am not sure if this is ideal, but it does the job.

from setuptools import setup
from distutils.command.build import build
from distutils import log
import subprocess
import json
import os

class javascript_build(build):
    def run(self):
        log.info("running npm run build")
        subprocess.check_output(['npm', 'run', 'build'], shell=True)
        build.run(self)


if __name__ == "__main__":
    setup(
        cmdclass={
            'build': javascript_build,
            },
    )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM