I made a chatbot in Node.js with IBM Watson. I'm trying to run a python script in it but I'm not able to. I tried browserify and bundled up all dependencies in a .js file and called the script in the html page but still it isn't working.
var PythonShell = require('python-shell');
var options = { mode: 'text', args: 765 };
PythonShell.run('pyt.py', options, function (err, results) {
if (err) throw err;
console.log('results: %j', results[0].toString());
});
It's running on locally between a node.js and a python file. But, doesn't work when I deploy the chatbot on a server.
Answer by @chughts (12979) | Apr 28, 2018 at 04:20 AM
Standard issues when you see 'it is running locally but doesn't work when I deploy to a server'. There will be a discrepancy between the two runtimes.
Make sure that you have included all dependencies in your package.json
file.
Make sure that you specify a node.js version in your package.json
file.
If the default set of node.js build packs do not support your version of node, then force to an older build pack by specifying it in your manifest.yml
file.
If it still doesn't work, then force version dependencies by running npm module npm-shrinkwrap
Learn to search the forum for similar questions, as this issue has been answered several times across multiple forums, over many years.
I'm running it on the localhost:3000 for now. The issue is not about deployment. It's about being able to transfer data from node.js to python on a server or not.
As I mentioned, I'm able to transfer data from node to python locally(as in running node filename.js in cmd). But, the python-shell/any require method does not seem to work when deployed on a server.
I've tried browserify. I've even mentioned it in package.json.
And this node.js file is an IBM Watson conversation(chatbot) file mind you. I was just wondering if there is any restriction from Watson's end. For browserify, I've mentioned the bundle.js file name in script tag in the index.html(The main chatbot page).
I see. In that case the question relates to the path of the file pyt.py
. In node.js you should use __dirname
to help locate the direction of the path. Get __dirname
to work locally, before deploying to the cloud.
Can't seem to display __dirname in the chatbot. If I'm running the chatbot in the http://localhost:3000/ and in the local folder my file is in public/js folder, would the directory name be http://localhost:3000/public/js ?
Yes, but everything that you put in the public directory is accessible by anyone over http. So if its a file that shouldn't be accessible by http then it shouldn't be in public. So if its in the directory scripts
Normally you use _dirname with path, eg.
path.join(__dirname, '/scripts')
so the directory would be ./scripts
when running in localhost.