Reputation: 134
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
Upvotes: 0
Views: 2250
Reputation: 134
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)
Upvotes: 0
Reputation: 347
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
Upvotes: 1