Python Embedded in Gams - Import Python Package

Questions on using the GAMS APIs (Python, .NET, etc.)
User avatar
Renger
Posts: 263
Joined: 2 years ago

Re: Python Embedded in Gams - Import Python Package

Post by Renger » 2 weeks ago

Hi Clemens

The story continues: I now have Gams on my Ubuntu 18 notebook installed and run in the same kind of problems. I use Anaconda and the following Gams-code:

Code: Select all

embeddedCode Python:
  import matplotlib
  import matplotlib.pyplot as plt
  matplotlib.use('WXagg')
  plt.plot([1,2,3,4])
  plt.show()
endEmbeddedCode
This gives me following error:

Code: Select all

--- Initialize embedded library libembpycclib64.so
--- Execute embedded library libembpycclib64.so
--- Exception from Python: No module named 'wx'
*** Error at line 1: Error executing "embeddedCode" section: Check log above
--- python.gms(1) 3 Mb 1 Error
*** Status: Execution error(s)
--- Job python.gms Stop 03/04/19 17:20:58 elapsed 0:00:00.857
When I check the version and which python, I have this:

Code: Select all

(base) renger@ubu:~$ which python
/home/renger/anaconda3/bin/python
I installed wxPython for Ananaconda using: conda install -c anaconda wxpython
I ran python3 setup.py install (and all was installed in the anaconda directory: home/renger/anaconda3/lib/python3.6/site-packages/

Any idea how to resolve this?

Cheers
Renger

PS. I am already looking out for the experience of installing this on the ETH mainframe...

User avatar
Clemens
Posts: 38
Joined: 2 years ago

Re: Python Embedded in Gams - Import Python Package

Post by Clemens » 2 weeks ago

Renger,

The mechanism on finding the Python interpreter is different on Linux. The shared object that is responsible for loading the Python interpreter uses @rpath to resolve its dependency to the Python shared object. Therefore it will be very hard to convince Embedded Code to use a different Python interpreter as long as the GMSPython directory is present. The solution for this is similar to the workaround on Windows. We need to make sure that the GMSPython directory is not found (e.g. by renaming). In addition we need to adjust the LD_LIBRARY_PATH (DYLD_LIBRARY_PATH on Mac OSX) to point to the location of libpython3.6m.so.1.0. In order to find required modules, setting the PYTHONPATH is perhaps required as well.

Let me know if you can get it running,
Clemens

User avatar
Renger
Posts: 263
Joined: 2 years ago

Re: Python Embedded in Gams - Import Python Package

Post by Renger » 2 weeks ago

Hi Clemens
I gave up as I wasn't able to get it running (Python in GAMS yes, but plotting no).
Even if I set the python path to GMSPython I run into lots of problems and did not manage to install wxpython.
My wishes:
  • - It would be great if the information on how to do this would be clear even for Linux-Dummies like me. Either using the GMSPython or the own-installed Python version. I prefer my "own" Python instead of having multiply Python versions lying around.

    - Or prepare the GAMS python setup in such a way that one could run (on-the-fly) plots in the embedded code (at least have some packages preinstalled for plotting).
Cheers
Renger

User avatar
Renger
Posts: 263
Joined: 2 years ago

Re: Python Embedded in Gams - Import Python Package

Post by Renger » 4 days ago

Hi Clemens
Here are my steps on my Windows machine to get the embedded code working with:
  • Install the python distribution of your liking (as long as it is version 3.6.x; not one of the versions 3.7 or 3.5)
  • Adjust your path by adding the path to the python3.dll (in my case: C:\Python36\python-3.6.8.amd64) and to the scripts directory of your distribution (e.g. C:\Python36\python-3.6.8.amd64\Scripts). This can be done under "Control panel" - "System" -> "Advanced system settings" -> "Environmental Variables" -> scroll to "path" and add these two new entries. If you don not have administrator rights you can add it to the user path in the upper window.
    image.png
  • Rename the file python3.dll in the Gams root directory, so Gams can't use it.
  • Open a dos box (cmd) and run in the directory of the 3.6 apifile (C:\GAMS\..\apifiles\Python\api_36) the following command "python setup.py install".
  • With this line in embedded code you can check if Gams is using your distribution and not the Gams version (3.6.1). It saves a file with the version number in the directory of your Gams program with embedded code.

    Code: Select all

    embeddedCode Python:
      import sys
      file = open('version.txt', 'w')
      version1 = str(sys.version_info[0])
      version2 = str(sys.version_info[1])
      version3 = str(sys.version_info[2])
      file.write(version1+'.'+version2 + '.' + version3)
      file.close()
    endEmbeddedCode
    
  • If it doesn't work, don't ask me, ask Clemens ;)
PS. Michael Bussieck informed me that in the new version of Gams, for Windows you only have to use the option pysetup=0. If you are a Mac or Linux user, rename/delete the GMSPython directory in the Gams root directory.
Cheers
Renger

Post Reply