0

I just wanted to use gams inside Jupyter notebook in order to use data frames of pandas in gams codes.

I followed the instructions of this page to get started with the gams in Jupyter. However, I can not call gams and when I run this line

%load_ext gams_magic

I get the following error:

    ---------------------------------------------------------------------------
ModuleNotFoundError                       Traceback (most recent call last)
Input In [1], in <cell line: 1>()
----> 1 get_ipython().run_line_magic('load_ext', 'gams_magic')

File C:\ProgramData\Anaconda3\lib\site-packages\IPython\core\interactiveshell.py:2294, in InteractiveShell.run_line_magic(self, magic_name, line, _stack_depth)
   2292     kwargs['local_ns'] = self.get_local_scope(stack_depth)
   2293 with self.builtin_trap:
-> 2294     result = fn(*args, **kwargs)
   2295 return result

File C:\ProgramData\Anaconda3\lib\site-packages\IPython\core\magics\extension.py:33, in ExtensionMagics.load_ext(self, module_str)
     31 if not module_str:
     32     raise UsageError('Missing module name.')
---> 33 res = self.shell.extension_manager.load_extension(module_str)
     35 if res == 'already loaded':
     36     print("The %s extension is already loaded. To reload it, use:" % module_str)

File C:\ProgramData\Anaconda3\lib\site-packages\IPython\core\extensions.py:76, in ExtensionManager.load_extension(self, module_str)
     69 """Load an IPython extension by its module name.
     70 
     71 Returns the string "already loaded" if the extension is already loaded,
     72 "no load function" if the module doesn't have a load_ipython_extension
     73 function, or None if it succeeded.
     74 """
     75 try:
---> 76     return self._load_extension(module_str)
     77 except ModuleNotFoundError:
     78     if module_str in BUILTINS_EXTS:

File C:\ProgramData\Anaconda3\lib\site-packages\IPython\core\extensions.py:92, in ExtensionManager._load_extension(self, module_str)
     90 if module_str not in sys.modules:
     91     with prepended_to_syspath(self.ipython_extension_dir):
---> 92         mod = import_module(module_str)
     93         if mod.__file__.startswith(self.ipython_extension_dir):
     94             print(("Loading extensions from {dir} is deprecated. "
     95                    "We recommend managing extensions like any "
     96                    "other Python packages, in site-packages.").format(
     97                   dir=compress_user(self.ipython_extension_dir)))

File C:\ProgramData\Anaconda3\lib\importlib\__init__.py:127, in import_module(name, package)
    125             break
    126         level += 1
--> 127 return _bootstrap._gcd_import(name[level:], package, level)

File <frozen importlib._bootstrap>:1030, in _gcd_import(name, package, level)

File <frozen importlib._bootstrap>:1007, in _find_and_load(name, import_)

File <frozen importlib._bootstrap>:984, in _find_and_load_unlocked(name, import_)

ModuleNotFoundError: No module named 'gams_magic'

I really do not know where is the problem. Moreover, there is not any source to address this issue.

afterburner
  • 2,552
  • 13
  • 21
Math
  • 157
  • 7

1 Answers1

1

It looks like the gams_magic package can not be found. The installation procedure for the GAMS Python API requires the environment variable SETUPTOOLS_USE_DISTUTILS to be set to stdlib in case the Python installation uses setuptools>=60.0.0. In order to check this you can run:

python -c"import setuptools; print(setuptools.__version__)"

If this is the case, you can run the following command before running python setup.py install:

Windows: set SETUPTOOLS_USE_DISTUTILS=stdlib
macOS/Linux: export SETUPTOOLS_USE_DISTUTILS=stdlib

See also the latest documentation for installing the GAMS Python API here: https://www.gams.com/latest/docs/API_PY_TUTORIAL.html#PY_COPY_GAMS_FILES_TO_PYTHON_INSTALLATION

Lutz
  • 2,197
  • 1
  • 10
  • 12
  • Thanks for he answer. The api file for my GAMS is 26 whereas my python version is 3.9. What can I do without installing newer version of GAMS? – Math Sep 09 '22 at 13:03
  • Sorry, I am not sure, what you mean by that. Which version do you use? You wrote GAMS 26? Or which one? GAMS magic should not be part of GAMS 26. Wasn't that introduced with GAMS 33? In general, it is always advisable to use the most recent version, which is GAMS 40 right now. – Lutz Sep 09 '22 at 13:32
  • Sorry for replying lately. I had not had concentration enough to keep tracking the problem. My GAMS version is 24.1.2. Based on your comments, I need to use recently released versions. Right? – Math Sep 14 '22 at 05:49
  • Yes. At least GAMS 33 is required to use GAMS_py. But I would rather use the latest version available (GAMS 40) – Lutz Sep 14 '22 at 07:26