0

The file structure should look like :

test.py

node_modules (The folder containing installed npm modules)

I have tried this :

import subprocess
import os
dir_path = os.path.dirname(os.path.realpath(__file__)) #holds the directory where python script is located
directory where python script is located
os.chdir(dir_path)
subprocess.call(["npm","init"])
subprocess.call(["npm","install"])

Error :

Traceback (most recent call last): File "c:\Users\Alifreeze.vscode\extensions\ms-python.python-2020.1.58038\pythonFiles\ptvsd_launcher.py", line 43, in main(ptvsdArgs) File "c:\Users\Alifreeze.vscode\extensions\ms-python.python-2020.1.58038\pythonFiles\lib\python\old_ptvsd\ptvsd__main__.py", line 432, in main
run() File "c:\Users\Alifreeze.vscode\extensions\ms-python.python-2020.1.58038\pythonFiles\lib\python\old_ptvsd\ptvsd__main__.py", line 316, in run_file runpy.run_path(target, run_name='main') File "C:\Users\Alifreeze\AppData\Local\Programs\Python\Python37-32\lib\runpy.py", line 263, in run_path pkg_name=pkg_name, script_name=fname) File "C:\Users\Alifreeze\AppData\Local\Programs\Python\Python37-32\lib\runpy.py", line 96, in _run_module_code mod_name, mod_spec, pkg_name, script_name) File "C:\Users\Alifreeze\AppData\Local\Programs\Python\Python37-32\lib\runpy.py", line 85, in _run_code exec(code, run_globals) File "c:\Users\Alifreeze\Desktop\ShellScripts\npm.py", line 6, in subprocess.call(["npm","init"]) File "C:\Users\Alifreeze\AppData\Local\Programs\Python\Python37-32\lib\subprocess.py", line 323, in call with Popen(*popenargs, **kwargs) as p: File "C:\Users\Alifreeze\AppData\Local\Programs\Python\Python37-32\lib\subprocess.py", line 775, in init restore_signals, start_new_session) File "C:\Users\Alifreeze\AppData\Local\Programs\Python\Python37-32\lib\subprocess.py", line 1178, in _execute_child startupinfo) FileNotFoundError: [WinError 2] The system cannot find the file specified

2 Answers 2

1
import subprocess
subprocess.call(["npm","init"])
subprocess.call(["npm","install"])

try this code in the test.py script, package.json should be present in the same folder while running the python script

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

7 Comments

what if nothing was there ? how do i run npm init and then npm install ? Also i want to install say aws-sdk how should i do that
you can add subprocess.call("npm","init") above the npm install line, this will ask you setup question for node application and you can proceed from there
Okay let me try this i will verify the answer and upvote it if this works.. cheers :)
Can you share detailed error ? Also I tried this solution with Ubuntu OS, I think you are using Windows OS ?
Cheers :) Happy Coding
|
0

This worked for me!

import subprocess
import os
dir_path = os.path.dirname(os.path.realpath(__file__)) #holds the directory where python script is located
os.chdir(dir_path)
subprocess.check_call('npm init', 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.