Nick jones
Nick jones

Reputation: 83

How to use python libraries in nodejs application

I have installed all the Node.js libraries as required for my nodejs application. But the nodejs application at the backend uses python libraries which has some lines of code such as import nltk and all other libraries. and these import statements when uploaded on a heroku platform is raising an error.

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'

I have uploaded all the files but still it is raising an error.

complete code deployed on heroku is available in github link - https://github.com/rinku16/patranker

nodejs application code files is present in app folder

Issue: How can i use this nltk and other python libraries in my application.

Below are the python libraries required in the python file

# 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

For example, for nltk library below is path of the file i got

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

how i use this path to clone it in my nodejs application. please see this.

Upvotes: 2

Views: 2073

Answers (2)

Vasiliy vvscode Vanchuk
Vasiliy vvscode Vanchuk

Reputation: 7159

You can use JSPyBridge https://github.com/extremeheat/JSPyBridge (in readme there are plenty of examples)

Upvotes: 1

Apoorva Chikara
Apoorva Chikara

Reputation: 8773

You can create a custom_lib folder and clone all the libraries in it. Provide the correct path using import or require in Node.js project. Use gulp runner or any task runner to copy these custom_lib to your build folder and remember follow the same hierarchy in the build folder structure.

You can also write a custom script to copy these python_lib to the build folder. You can add this custom script in package.json in scripts.

We have the same issue in our project and it work like a charm.

Upvotes: 1

Related Questions