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

Full Path to .py script fails #38

Closed
isa56k opened this issue Apr 19, 2016 · 11 comments
Closed

Full Path to .py script fails #38

isa56k opened this issue Apr 19, 2016 · 11 comments
Assignees
Labels
Milestone

Comments

@isa56k
Copy link

isa56k commented Apr 19, 2016

I'm using the following to call my python script:

var PythonShell = require('python-shell');
var pyshell = new PythonShell.run('/home/pi/Development/Sensor/python/SenseHat.py');

// get message back
pyshell.on('message', function (message) {
  // received a message sent from the Python script (a simple "print" statement) 
  console.log(message);
});

// end the input stream and allow the process to exit 
pyshell.end(function (err) {
  if (err) throw err;
  console.log('exit');
});

If I use the full path to the script it fails with the error below:

/home/pi/Development/Sensor/node/pythonTest.js:14
  if (err) throw err;
           ^
Error: python: can't open file 'home/pi/Development/Sensor/python/SenseHat.py': [Errno 2] No such file or directory

    at PythonShell.parseError (/home/pi/node_modules/python-shell/index.js:190:17)
    at terminateIfNeeded (/home/pi/node_modules/python-shell/index.js:98:28)
    at Socket.<anonymous> (/home/pi/node_modules/python-shell/index.js:78:9)
    at emitNone (events.js:72:20)
    at Socket.emit (events.js:166:7)
    at endReadableNT (_stream_readable.js:903:12)
    at doNTCallback2 (node.js:439:9)
    at process._tickCallback (node.js:353:17)

If I use the following path to the script it all works ok:
../python/SenseHat.py
and so does:
../../../../../home/pi/Development/Sensor/python/SenseHat.py

@extrabacon
Copy link
Owner

It'a a bug, absolute paths are not supported because of the handling of the scriptPath option (https://github.com/extrabacon/python-shell/blob/master/index.js#L53).

Quick workaround: set scriptPath option to directory containing your script (/home/pi/Development/Sensor/python), then run the script (SenseHat.py). This way the full path to the script will be built correctly.

@isa56k
Copy link
Author

isa56k commented Apr 19, 2016

var pyshell = new PythonShell.run('SenseHat.py', { scriptPath: '/home/pi/Development/Sensor/python/' } );

That works, sorry... didn't see that option!

@isa56k isa56k closed this as completed Apr 19, 2016
@extrabacon
Copy link
Owner

It's still a workaround, python-shell should support absolute paths. Reopening so I can fix this in a future release.

@phtdacosta
Copy link

Just to know, is it fixed yet? Thanks

@maziarz
Copy link

maziarz commented Jan 20, 2018

Heres a workaroun for OSX. I recommmend using os lib to determine the OS (darwin, win32, etc.)

import path from 'path'

const somePath = '/some/path/here/script.py'
let fileName = path.dirname(somePath)
let filePath = path.basename(somePath)

PythonShell.run(fileName, { scriptPath: filePath }, function (err, results) {
    ...
})

@jzombie
Copy link

jzombie commented Mar 28, 2018

Just to know, is it fixed yet? Thanks.

(says it is closed; issue is not fixed for me)

@Almenon
Copy link
Collaborator

Almenon commented Mar 28, 2018

? The issue is still open - for now just use the workaround

@Trevirirus
Copy link

the workaround is not always working:

pyshell = new pyShell('main.py',{scriptPath: path.join(__dirname, '..', 'src', 'GNSS_Positioning')} , options)

also the scriptpath inside the options is resulting in the above described errors.

please support full paths

@KarthikSamaganam4

This comment has been minimized.

@Almenon
Copy link
Collaborator

Almenon commented Sep 10, 2018

@KarthikSamaganam4 this is unrelated to this issue - I've opened a new issue for you. See #151

@Almenon
Copy link
Collaborator

Almenon commented Sep 10, 2018

PythonShell should support both relative and absolute paths now. For example, the runString function works by passing an absolute path to the a file in the temp directory over to pythonshell. The function is unit tested in both windows and mac so it will work no matter the platform.

@Trevirirus, can you double check if it has been fixed in the latest version (1.0.4) of python-shell please? You should be able to pass the path into pythonshell without having to specify any options.

@extrabacon, I fixed it in 4ba8b5e by removing the ./ which was causing an error with absolute paths on mac. Was there any reason ./ was in there in the first place? Hopefully I didn't break some weird edge case not covered in the unit tests.

@Almenon Almenon closed this as completed Sep 10, 2018
@Almenon Almenon self-assigned this Sep 10, 2018
@Almenon Almenon added the bug label Sep 10, 2018
@Almenon Almenon added this to the v1.0.4 milestone Sep 10, 2018
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

8 participants