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

Updates to NoTask to prevent argparse error #20

Open
ghackebeil opened this issue Feb 25, 2016 · 4 comments
Open

Updates to NoTask to prevent argparse error #20

ghackebeil opened this issue Feb 25, 2016 · 4 comments

Comments

@ghackebeil
Copy link
Member

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 added a commit that referenced this issue Feb 25, 2016
…ure that EmptyTask does not create an argument parser (gh-20). Also fixing an issue with EmptyClass where the overloaded __init__ method was not passing the id and name arguments down to the base class. This would have prevented the NoTask object from declaring itself with id=0. Just deleting the __init__ method rather than fixing it since it simply calls the base class __init__.
@ghackebeil
Copy link
Member Author

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

@sylvaticus
Copy link

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
Copy link
Member Author

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
Copy link

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

@whart222 whart222 added this to the PyUtilib 5.4 milestone Apr 7, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants