PyPSA / linopy

Linear optimization with N-D labeled arrays in Python
https://linopy.readthedocs.io
MIT License
160 stars 45 forks source link

Cannot run example (Command line tool 'which' not available on Windows) #27

Closed tburandt closed 2 years ago

tburandt commented 2 years ago

Hi,

first of all for this very promising looking package! However, I find myself unable to run the example, as I get the following error:

from linopy import Model

---------------------------------------------------------------------------
FileNotFoundError                         Traceback (most recent call last)
<ipython-input-1-91f35b98b0bb> in <module>
----> 1 from linopy import Model

~\anaconda3\lib\site-packages\linopy\__init__.py in <module>
      7 """
      8 
----> 9 from linopy import model
     10 from linopy.expressions import merge
     11 from linopy.io import read_netcdf

~\anaconda3\lib\site-packages\linopy\model.py in <module>
     18 from xarray import DataArray, Dataset
     19 
---> 20 from linopy import solvers
     21 from linopy.common import best_int, replace_by_map
     22 from linopy.constraints import Constraints

~\anaconda3\lib\site-packages\linopy\solvers.py in <module>
     18 
     19 
---> 20 if sub.run(["which", "glpsol"], stdout=sub.DEVNULL).returncode == 0:
     21     available_solvers.append("glpk")
     22 

~\anaconda3\lib\subprocess.py in run(input, capture_output, timeout, check, *popenargs, **kwargs)
    487         kwargs['stderr'] = PIPE
    488 
--> 489     with Popen(*popenargs, **kwargs) as process:
    490         try:
    491             stdout, stderr = process.communicate(input, timeout=timeout)

~\anaconda3\lib\subprocess.py in __init__(self, args, bufsize, executable, stdin, stdout, stderr, preexec_fn, close_fds, shell, cwd, env, universal_newlines, startupinfo, creationflags, restore_signals, start_new_session, pass_fds, encoding, errors, text)
    852                             encoding=encoding, errors=errors)
    853 
--> 854             self._execute_child(args, executable, preexec_fn, close_fds,
    855                                 pass_fds, cwd, env,
    856                                 startupinfo, creationflags, shell,

~\anaconda3\lib\subprocess.py in _execute_child(self, args, executable, preexec_fn, close_fds, pass_fds, cwd, env, startupinfo, creationflags, shell, p2cread, p2cwrite, c2pread, c2pwrite, errread, errwrite, unused_restore_signals, unused_start_new_session)
   1305             # Start the process
   1306             try:
-> 1307                 hp, ht, pid, tid = _winapi.CreateProcess(executable, args,
   1308                                          # no special security
   1309                                          None, None,

FileNotFoundError: [WinError 2] Das System kann die angegebene Datei nicht finden

I'm using python via anaconda on Windows 11.

Best regards

FabianHofmann commented 2 years ago

Indeed! Testing wasn't done on Windows, sorry for that. I think I have to come up with an alternative approach for checking whether a solver are installed. Do you have the glpk installed?

tburandt commented 2 years ago

No, I just recognized that I actually have not😅 However, I have gurobipy installed, which is not utilized/detected on startup. If glpk is required for linopy to run, maybe add it as a dependency as well?

FabianHofmann commented 2 years ago

The solvers are not required, and should be installed by the users, so that's fine. However the command for checking if glpk is installed fails. The error is the same when you write which glpsol in your console, which fails due to which not working on Windows. So probably even when you have glpk installed linopy still raises the same error (if I understand correctly).

tburandt commented 2 years ago

Ahh, that makes sense. For Windows, the keyword where is available, which should do the same trick. I will try to fix the issue tomorrow and make a pull-request

FabianHofmann commented 2 years ago

Great! I made a first attempt in https://github.com/PyPSA/linopy/tree/windows-compat, where I also added update the CI, let's see how that plays out.

tburandt commented 2 years ago

Yes, this one works like a charm so far!

FabianHofmann commented 2 years ago

Thanks for reporting @tburandt