7

I implemented a python web client that I would like to test.

The server is hosted in npm registry. The server gets ran locally with node before running my functional tests.

How can I install properly the npm module from my setup.py script?

Here is my current solution inspired from this post:

class CustomInstallCommand(install):
    def run(self):
        arguments = [
            'npm',
            'install',
            '--prefix',
            'test/functional',
            'promisify'
        ]
        subprocess.call(arguments, shell=True)
        install.run(self)

setup(
    cmdclass={'install': CustomInstallCommand},

2 Answers 2

10
+150
from setuptools.command.build_py import build_py

class NPMInstall(build_py):
    def run(self):
        self.run_command('npm install --prefix test/functional promisify')
        build_py.run(self)

OR

from distutils.command.build import build

class NPMInstall(build):
    def run(self):
        self.run_command("npm install --prefix test/functional promisify")
        build.run(self)

finally:

setuptools.setup(
    cmdclass={
        'npm_install': NPMInstall
    },
    # Usual setup() args.
    # ...
)

Also look here

Sign up to request clarification or add additional context in comments.

2 Comments

and if you'd like to do it as part of the build command, here's how: jichu4n.com/posts/… (same as this answer but use build_py as the keyword in cmdclass)
When using the above I get... running npm_install running npm install --prefix app/site/static error: invalid command 'npm install --prefix app/site/static' I already have a package.json file in the static directory. Any thoughts?
2

You are very close, Here is a simple function that does just that, you can remove "--global" option is you want to install the package for the current project only, keep in mind the the command shell=True could present security risks

import subprocess
def npm_install(args=["npm","--global", "install", "search-index"])
  subprocess.Popen(args, shell=True)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.