PyUtilib / pyutilib

A collection of general Python utilities, including logging and file IO, subprocess management, plugin systems, and workflow management.
BSD 3-Clause "New" or "Revised" License
34 stars 21 forks source link

Updates to NoTask to prevent argparse error #20

Open ghackebeil opened 8 years ago

ghackebeil commented 8 years ago

This issue will track changes related to the thread here: https://groups.google.com/forum/#!topic/pyomo-forum/Att_92wrAoI

It looks like the attempt to create an argument parser by the global NoTask object in pyutilib.workflow.task is preventing a user from executing a Pyomo script from C code. I don't know whether or not this is highlighting a problem within the argparse module, but a reasonable change on our part would be to just not create an argument parser for the NoTask object (I can't see how it would be needed).

ghackebeil commented 8 years ago

I think 4462a334c574b02e1c5299271d5dfc4b95a79ec1 resolves this issue, but I'm going to leave it open so @whart222 can take a look.

sylvaticus commented 8 years ago

Sorry, I replaced /usr/local/lib/python3.4/dist-packages/pyutilib/workflow/task.py with the new task.py from your commit but I still have the error AttributeError: 'module' object has no attribute 'argv' (unless I comment row 189 self._parser = argparse.ArgumentParser() ) when I call from C, using the python C api, a function in a script that from pyomo.core imports * ... For the rest, I implement it in the way you suggested and it works, even passing containers.. thanks.

 $ ./cpp_program pythoncode mainFromC 350 600
Traceback (most recent call last):
  File "./pythoncode.py", line 5, in <module>
    from pyomo.core import *
  File "/usr/local/lib/python3.4/dist-packages/pyomo/core/__init__.py", line 16, in <module>
    import pyomo.core.preprocess
  File "/usr/local/lib/python3.4/dist-packages/pyomo/core/preprocess/__init__.py", line 10, in <module>
    import pyomo.core.preprocess.simple_preprocessor
  File "/usr/local/lib/python3.4/dist-packages/pyomo/core/preprocess/simple_preprocessor.py", line 17, in <module>
    def simple_preprocessor(data, model=None):
  File "/usr/local/lib/python3.4/dist-packages/pyomo/util/_task.py", line 328, in my_decorator
    return PyomoTask_tmp()
  File "/usr/local/lib/python3.4/dist-packages/pyomo/util/_task.py", line 261, in __init__
    PyomoTask.__init__(self, *args, **kwargs)
  File "/usr/local/lib/python3.4/dist-packages/pyomo/util/_task.py", line 121, in __init__
    PyomoTaskPlugin.__init__(self, *args, **kwargs)
  File "/usr/local/lib/python3.4/dist-packages/pyomo/util/_task.py", line 111, in __init__
    pyutilib.workflow.Task.__init__(self, *args, **kwds)
  File "/usr/local/lib/python3.4/dist-packages/pyutilib/workflow/task.py", line 42, in __init__
    self._create_parser(parser)
  File "/usr/local/lib/python3.4/dist-packages/pyutilib/workflow/task.py", line 189, in _create_parser
    self._parser = argparse.ArgumentParser()
  File "/usr/lib/python3.4/argparse.py", line 1630, in __init__
    prog = _os.path.basename(_sys.argv[0])
AttributeError: 'module' object has no attribute 'argv'
Failed to load "pythoncode"

/Antonello

ghackebeil commented 8 years ago

Thanks for letting us know. I guess this won't be such a simple fix on our part. Can you work around the issue using the approach outlined in the StackOverflow answer in the following link?

https://stackoverflow.com/questions/12230210/attributeerror-module-object-has-no-attribute-argv-when-using-python-h

sylvaticus commented 8 years ago

Hi.. thanks.. I did already tried that, but not being really sure it was the right approach and having a small problem with it, I didn't implemented it. Actually it solves the problem with both the original and trunk task.py.

So, to call a pyomo optimisation routine from C is enough to wrap the model in a function and call from C that function after you run:

    Py_Initialize();
    int dummy_argc=0;
    wchar_t *dummy_argv[0];
    PySys_SetArgv(dummy_argc, dummy_argv);

Thanks again for the support.. /Antonello