如何在脚本所在的目录中使用python脚本安装npm模块

如何在脚本所在的目录中使用python脚本安装npm模块,python,python-3.x,npm,npm-install,Python,Python 3.x,Npm,Npm Install,文件结构应如下所示: test.py 节点\u模块(包含已安装的npm模块的文件夹) 我试过这个: 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) subpr

文件结构应如下所示:

test.py

节点\u模块
(包含已安装的npm模块的文件夹)

我试过这个:

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"])
错误

回溯(最近一次呼叫最后一次): 文件“c:\Users\Alifreeze.vscode\extensions\ms python.python-2020.1.58038\pythonFiles\ptvsd_launcher.py”,第43行,在 干管(ptvsdArgs) 文件“c:\Users\Alifreeze.vscode\extensions\ms python.python-2020.1.58038\pythonFiles\lib\python\old\u ptvsd\ptvsd\uuuuu main\uuuu.py”,第432行,在main
运行() 文件“c:\Users\Alifreeze.vscode\extensions\ms python.python-2020.1.58038\pythonFiles\lib\python\old\u ptvsd\ptvsd\uuuuuu main\uuuuuu.py”,第316行,在运行文件中 runpy.run_路径(目标,run_name='main) 文件“C:\Users\Alifreeze\AppData\Local\Programs\Python\Python37-32\lib\runpy.py”,第263行,在运行路径中 pkg_name=pkg_name,script_name=fname) 文件“C:\Users\Alifreeze\AppData\Local\Programs\Python\Python37-32\lib\runpy.py”,第96行,在运行模块代码中 模块名称、模块规格、组件名称、脚本名称) 文件“C:\Users\Alifreeze\AppData\Local\Programs\Python\Python37-32\lib\runpy.py”,第85行,在运行代码中 exec(代码、运行\全局) 文件“c:\Users\Alifreeze\Desktop\ShellScripts\npm.py”,第6行,在 子进程调用([“npm”,“init”]) 调用中第323行的文件“C:\Users\Alifreeze\AppData\Local\Programs\Python\Python37-32\lib\subprocess.py” 将Popen(*popenargs,**kwargs)作为p: 文件“C:\Users\Alifreeze\AppData\Local\Programs\Python\Python37-32\lib\subprocess.py”,第775行,位于init 恢复信号,启动新会话) 文件“C:\Users\Alifreeze\AppData\Local\Programs\Python\Python37-32\lib\subprocess.py”,第1178行,在执行子进程中 startupinfo) FileNotFoundError:[WinError 2]系统找不到指定的文件

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

在test.py脚本中尝试这段代码,package.json应该在运行python脚本时出现在同一个文件夹中

这对我很有效

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)

如果什么都没有呢?如何运行npm init,然后再运行npm安装?另外,我想安装say
aws sdk
我该怎么做?您可以在npm安装行上方添加子流程。调用(“npm”,“init”),这将向您询问节点应用程序的安装问题,您可以从中继续。好的,让我试试这个,我将验证答案,并在可行的情况下进行投票。。干杯:)你能分享详细的错误吗?我还在Ubuntu操作系统上尝试了这个解决方案,我想你正在使用Windows操作系统?干杯:)快乐的编码