Search code examples
pythonjupytergams-math

Gams_magic does not work in Jupyter notebook and returns ModuleNotFoundError


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.


Solution

  • 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