首页 > 解决方案 > 如何在 nodejs 应用程序中使用 python 库

问题描述

我已经安装了我的 nodejs 应用程序所需的所有 Node.js 库。但是后端的 nodejs 应用程序使用 python 库,其中包含一些代码行,例如import nltk所有其他库。并且这些导入语句在heroku平台上上传时会引发错误。

Traceback (most recent call last): File "mainfile.py", line 2, in import functionfile as f File "/app/functionfile.py", line 1, in import nltk ModuleNotFoundError: No module named 'nltk'

我已经上传了所有文件,但仍然出现错误。

在 github 链接中提供了部署在 heroku 上的完整代码 - https://github.com/rinku16/patranker

nodejs 应用程序代码文件存在于 app 文件夹中

问题:如何在我的应用程序中使用这个 nltk 和其他 python 库。

下面是python文件中需要的python库

# Import function file as reference to use defined python functions
import functionfile as f
import sys 
import nltk
from nltk.corpus import stopwords
#for generating sentence token
from nltk.tokenize import sent_tokenize
#for generating word token
from nltk.tokenize import word_tokenize
# BeautifulSoup for scraping and Requests to make HTTP requests.
from bs4 import BeautifulSoup 
# BeautifulSoup is in bs4 package 
import requests
#Counter class For Getting Most Occurred Biwords
from collections import Counter
#FOR MAKING DATA FRAME AND WRITING THE PATENT EXTRACTED DATA TO CSV FILE
from pandas import DataFrame
import pandas as pd
import os
import os.path
from os import path
import re
from datetime import datetime, timedelta
from datetime import date
import math

例如,对于nltk下面的库是我得到的文件的路径

>>> import nltk 
>>> nltk.__file__ 'C:\\Users\\Carthaginian\\AppData\\Local\\Programs\\Python\\Python39\\lib\\site-packages\\nltk\\__init__.py'

我如何使用此路径在我的 nodejs 应用程序中克隆它。请看这个。

标签: pythonnode.js

解决方案


您可以创建一个 custom_lib 文件夹并克隆其中的所有库。在 Node.js 项目中使用 import 或 require 提供正确的路径。使用gulp runner 或任何 task runner 将这些 custom_lib 复制到您的构建文件夹,并记住在构建文件夹结构中遵循相同的层次结构。

您还可以编写自定义脚本将这些 python_lib 复制到构建文件夹。您可以在脚本中的 package.json 中添加此自定义脚本。

我们的项目中有同样的问题,它就像一个魅力。


推荐阅读