0

I have this code here in python

import subprocess

subprocess.Popen(["npm", "install", "express"], shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)

however it doesn't seem to work, it doesn't install anything. When I look at the error it tells me the proper usage of the npm command.

Keep in mind this is on Python 3.8.8 on Ubuntu 18.04

2
  • I think it might help answering the question if you had pasted the stdout and stderr of your result. Commented Jul 16, 2021 at 22:31
  • @BenY it tells you the usage of the npm command. To be exact it's something like this Usage: npm <command> npm install install all the dependencies in your project npm install <foo> add the <foo> dependency to your project you get the picture Commented Jul 16, 2021 at 22:35

3 Answers 3

1
import subprocess

proc = subprocess.Popen(["npm", "install", "express"])

I tested this on Ubuntu 20.04 and it ran the command as expected.

Only thing is that it gets stuck in the NPM process and I've had to press cancel to exit the process.

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

3 Comments

Hi there! This one actually worked for me if we change it to subprocess.Popen(["npm", "install", "express"], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
Seems the only difference from your code is the omission of the shell=True argument.
Yes, I have an article here that gives some insight into why you were encountering the original error!
1

Instead of using subprocess, Try using os like this:

import os
os.system("npm install express")
os.system("echo You can run commands from terminal using os.")

2 Comments

Hi there! This worked for me, however may I ask how this one works while the other one doesn't? I believe it's important for me that subprocess works.
Hello, I would like to say that you could import both os and subprocess, assuming that you need that exact line of code, I tested it and it worked for me. Make sure when you run the code you have internet access, but I did notice that it logs nothing when installing, so it might have installed even though it doesn't say anything. One more thing, if you are using an online IDE (Specifically repl.it since that website has many languages to program in), a lot of times they hide folders named node_modules, package.json, etc. I will look into the problem more though.
1

On my first post destroyer22719 commented:

Hi there! This worked for me, however may I ask how this one works while the other one doesn't? I believe it's important for me that subprocess works.

I would like to say, I never learned how to use subprocess, so I wouldn't know how it works or anything. But, with a little bit of looking around I think I found a solution.

I tested on the latest version of ubuntu as of a week ago (python 3.8.10) and on my windows 10 (python 3.9) and it fails on windows 10 but works on ubuntu.

Ubuntu Version:

import subprocess
from threading import Thread
def package():
    subprocess.Popen(["npm install express"], shell=True)
Thread(target=package).start()
print("This text will print if the command is finished or not.")

This is the one that works on windows:

import subprocess
from threading import Thread
def package():
    subprocess.Popen(["npm", "install", "express"], shell=True)
Thread(target=package).start()
print("This text will print if the command is finished or not.")

threading is imported because when you run the process it doesn't just stop, so I used Thread() to get around that.

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.