using io.popen

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
meteopath
Prole
Posts: 19
Joined: Sun May 15, 2022 12:41 am

using io.popen

Post by meteopath »

Hi, is there a trick to using io.popen in LOVE? Here are the lines in my main.lua love.load():

Code: Select all

    local handle = io.popen("python3 libLookupOne.py "..x) 
    local result = handle:read("*a")
    handle:close()
where x is an integer (1, fwiw, at game start). The python program (libLookupOne.py) is supposed to take the argument x and create a json file. But nothing happens. No error thrown. It's as if those lines are just skipped over.

I tried two different locations for the python file. First I had it in the same directory as main.lua. Then I put it in the root folder for filesystem (/Users/user/Library/Application Support/LOVE/). No effect.

The command itself ("local handle = io.popen("python 3...etc.") works outside of LOVE. I put it in a non-LOVE lua file. Then I ran that file, and it worked. The python program took the command and created the json file.

Any ideas appreciated. Doesn't have to be io.popen. Big picture, I have to get libLookupOne.py to run on command from my LOVE game.
User avatar
pgimeno
Party member
Posts: 3544
Joined: Sun Oct 18, 2015 2:58 pm

Re: using io.popen

Post by pgimeno »

The io library bypasses the love.filesystem library. If you zip your program into a .love file including the Python file, then io.popen can't see anything but the zipped file.

Take a look at the various getXXXDirectory() functions in love.filesystem. If you place the Python file alongside the .love file, then you can probably use love.filesystem.getSourceBaseDirectory() to find the path where the Python file is.
meteopath
Prole
Posts: 19
Joined: Sun May 15, 2022 12:41 am

Re: using io.popen

Post by meteopath »

Pre-fuse is there a way to do this?

Great to know the structure will work when I get to the fuse. But I'm not there. And for now, I'm still stuck. I tried a couple of things. I got the paths from getSaveDirectory() and getAppdataDirectory(). Then I placed the Python file in those directories and concatenated commands a few ways, like:

Code: Select all

    path = love.filesystem.getSaveDirectory()
    local handle = io.popen('cd '..path..'; python3 libLookupOne.py 1') 
    local result = handle:read("*a")
    handle:close()  
Tried os.execute too. Again, no errors and no effect. I feel like I'm missing something basic. Because going back to the original setup, I wasn't fused and I wasn't even calling love.filesystem. I just had libLookupOne.py in the same directory as main.lua.

Does io never work with LOVE because of the filesystem bypass, or is that only after fuse? Thank you, btw.
User avatar
pgimeno
Party member
Posts: 3544
Joined: Sun Oct 18, 2015 2:58 pm

Re: using io.popen

Post by pgimeno »

io works with Löve regardles of whether it's fused or not.

Print the path and see what you are getting, and if it matches the place where you have stored the file. Also, run Löve from a terminal in order to not miss any error messages that could be being printed.

This works for me in bash:

Code: Select all

local function bashEscape(s)
  return "'" .. s:gsub("'", "'\\''") .. "'"
end

local path = love.filesystem.getSource()
print(path)
local handle = io.popen('python3 '..bashEscape(path)..'/hello.py')
local result = handle:read("*a")
handle:close()
print(result)
I used getSource() because there's no function that will work indistinctly with both a Löve file and a source directory.

- If you are invoking Löve by passing it a directory where the Lua files are, with the Python file in the same place as main.lua/conf.lua, use getSource().
- If you are invoking Löve by passing it the name of a .love file, and the Python file is next to the .love file, use getSourceBaseDirectory(). In other words, before zipping your program into a .love file, change getSource() to getSourceBaseDirectory().
meteopath
Prole
Posts: 19
Joined: Sun May 15, 2022 12:41 am

Re: using io.popen

Post by meteopath »

Partly resolved.

Still have questions. First what went right:

Your code worked for me. I created a fresh Love project to test it. I put the code in main.lua. Inside the Python files, I changed all the relative paths to absolute paths. At that point, io.popen worked perfectly. It successfully called the Python file and passed an argument. The Python file then outputted a json file to the right directory.

Unfortunately, my full project doesn't run when I insert that same code. I don't know why because I'm not getting error messages. I'm not getting anything. I am launching Love in the only way I know how: cursor in main.lua, cmd+l. I can see it's starting to work because the Love icon in my dock is activated, but the game/error window does not appear on my screen.

How do you run Love from a terminal? Can it be done as main.lua or does it have to be fused first into a .love file?

The command lua main.lua doesn't work for me. It throws an immediate error, not recognizing the 'love' in function love.load(). I updated my PATH according to: https://love2d.org/wiki/Getting_Started, but that command seems to need a .love file or a zip folder? It gives me a 'No Game' window when I run it from the game directory (consisting of unzipped main.lua plus assets, etc.) and an error when I specify main.lua.

I know I'm probably off topic now. Should I have started a new topic?

Thanks again.
meteopath
Prole
Posts: 19
Joined: Sun May 15, 2022 12:41 am

Re: using io.popen

Post by meteopath »

Oops, figured out launching main.lua from terminal. ("$ love .")
I got the error messages and will get to look into them later. Sorry about the double post, just wanted to say!
User avatar
pgimeno
Party member
Posts: 3544
Joined: Sun Oct 18, 2015 2:58 pm

Re: using io.popen

Post by pgimeno »

Glad you found it by yourself. If you still need help, please post the error messages you're getting and some info on the folder structure you're using.
meteopath
Prole
Posts: 19
Joined: Sun May 15, 2022 12:41 am

Re: using io.popen

Post by meteopath »

Thank you. It was a typo, of course. Doh. Fixed, and working now :cool:
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 50 guests