Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TypeError: PythonShell.run is not a function #148

Closed
sni-J opened this issue Sep 6, 2018 · 4 comments
Closed

TypeError: PythonShell.run is not a function #148

sni-J opened this issue Sep 6, 2018 · 4 comments
Assignees
Labels

Comments

@sni-J
Copy link

sni-J commented Sep 6, 2018

Today, my node modules have been reset for somewhat reason(idk) and I re-installed it. It worked properly until yesterday, but this error suddenly occurred.
+) I'm using ExpressJS

Error Message

TypeError: PythonShell.run is not a function
    at getKeyword (/home/joon/Desktop/server_study/db.js:313:17)

db.js

const PythonShell = require('python-shell');

var pyOptions = {
    mode: 'text',
    pythonPath: '/usr/bin/python3.5',
    pythonOptions: ['-u'],
    scriptPath: './python',
    args: ['title', 'researcher', 'path_of_txt']
};

...

function pythonTest(cb){
    PythonShell.run("x=1;print(x)", null, (err, results)=>{
        console.log(results);
    });
}

package.json

{
  "name": "server_study",
  "version": "1.0.0",
  "description": "",
  "main": "app.js",
  "dependencies": {
    "body-parser": "^1.18.3",
    "curl": "^0.1.4",
    "ejs": "^2.6.1",
    "express": "^4.16.2",
    "express-session": "^1.15.6",
    "i18n": "^0.8.3",
    "jquery": "^3.3.1",
    "jsdom": "^11.10.0",
    "multer": "^1.3.0",
    "mysql": "^2.16.0",
    "nodemailer": "^4.6.8",
    "pdf2json": "^1.1.7",
    "python-shell": "^1.0.3",
    "serve-index": "^1.9.1"
  },
  "devDependencies": {},
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC",
  "private": true
}
@Almenon
Copy link
Collaborator

Almenon commented Sep 6, 2018 via email

@Almenon Almenon self-assigned this Sep 6, 2018
@Almenon
Copy link
Collaborator

Almenon commented Sep 6, 2018

I Looked it up and apparently it's not that simple. Imports are a es6 feature so I thought they would work in node, given that even the outdated node 6.14.4 supports the vast majority of es6 features. But apparently only the latest edition of node supports imports.

See https://stackoverflow.com/questions/45854169/how-can-i-use-an-es6-import-in-node

So if you want to use imports you have four options:

  1. Use latest version of node with --experimental-modules flag. Node 8.5 might also work.
  2. use babel
  3. use esm
  4. use typescript (highly reccomend this)

Or you can use require, like so:

let {PythonShell} = require('python-shell')

Repl.it Example

@Almenon Almenon closed this as completed Sep 6, 2018
@sni-J
Copy link
Author

sni-J commented Sep 6, 2018

Thanks for your help!

@Almenon
Copy link
Collaborator

Almenon commented Sep 15, 2018

@sni-J I found out you can still use require - see updated comment above.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants