When I am using embedded python code in GAMS can I import python packages such as pandas some how? When I am just normally write :
$onEmbeddedCode Python:
import pandas as pd
I get an 865 Problem in Embedded Code Section
Thank you
Python Embedded in Gams - Import Python Package
Re: Python Embedded in Gams - Import Python Package
Yes you can. Either install the package manually by navigating to your GMSPython folder (i.e. <GAMSRoot>/GMSPython) and using the pip executable (python package management system). This tool should be located in a folder "Scripts" or something similar. (you can install pandas with pip with the command: "pip install pandas") Alternatively use the solution proposed here to import packages from within your code: https://stackoverflow.com/questions/123 ... ithin-code
Re: Python Embedded in Gams - Import Python Package
Hi
I had the facility for running embedded code with python up and running, but now I am on my new computer and my short term memory was also cleared: I haven't a clue how I got it working.
Embedded code runs fine, but if I add "import pandas as pd", I get the error that pandas is not installed.
pip install pandas
it uses my Anaconda python installation and tells me pandas is already installed.
I use Anaconda 3.6 on Windows 10. The path points to the anaconda installation.
I ran the command "python setup.py install" in the 3.6 api directory of Gams.
Any help would be appreciated.
Cheers
Renger
I had the facility for running embedded code with python up and running, but now I am on my new computer and my short term memory was also cleared: I haven't a clue how I got it working.
Embedded code runs fine, but if I add "import pandas as pd", I get the error that pandas is not installed.
If I go to the GMSPython directory and issue:--- Initialize embedded library embpycclib64.dll
--- Execute embedded library embpycclib64.dll
--- Exception from Python: No module named 'pandas'
*** Error executing Python embedded code section:
pip install pandas
it uses my Anaconda python installation and tells me pandas is already installed.
I use Anaconda 3.6 on Windows 10. The path points to the anaconda installation.
I ran the command "python setup.py install" in the 3.6 api directory of Gams.
Any help would be appreciated.
Cheers
Renger
____________________________________
Enjoy modeling even more: Read my blog on modeling at The lazy economist
Enjoy modeling even more: Read my blog on modeling at The lazy economist
Re: Python Embedded in Gams - Import Python Package
Renger, use the pip executable that comes with GMSPython. It is located in the Scripts subfolder:
This should do it.
-Michael
Code: Select all
c:\gams\win64\26.1\GMSPython\Scripts\pip install pandas
-Michael
Re: Python Embedded in Gams - Import Python Package
That worked. However, is it possible to use anaconda instead of Python coming with GAMS? I ask because I can't use matplotlib on Windows because of the missing module tkinter. It looks like Gams python is a scaled down version.
Cheers
Renger
Cheers
Renger
____________________________________
Enjoy modeling even more: Read my blog on modeling at The lazy economist
Enjoy modeling even more: Read my blog on modeling at The lazy economist
Re: Python Embedded in Gams - Import Python Package
Renger,
You are right. Tkinter is not part of GMSPython. If you only want to get matplotlib running, there is a workaround for this. You can use wxPython. Install it using: pip install wxPython and then in embeddedCode you can do the following:
Using a different Python interpreter is also possible but has some limitations. The most important one is that it only works with Python 3.6. You can find the details here: https://www.gams.com/latest/docs/UG_Emb ... onOfPython
Hope that helps,
Clemens
You are right. Tkinter is not part of GMSPython. If you only want to get matplotlib running, there is a workaround for this. You can use wxPython. Install it using: pip install wxPython and then in embeddedCode you can do the following:
Code: Select all
import matplotlib
matplotlib.use('WXAgg')
import matplotlib.pyplot as plt
...
Hope that helps,
Clemens
Re: Python Embedded in Gams - Import Python Package
Hi Clemens
Thanks, but I don't manage to get it running (the wxAgg doesn't do the job either as I want to have a plot showing up).
Could you help me out (all on Windows 10 x64):
- I ran the setup.py script in the 3.6 directory
- I have my Python 3.6 in c:\users\renger\anaconda (there is also the 3.6.dll) and added this to my path.
- I have no new user variables defined (I tried a few like PYTHONPATH set to either the anaconda directory or to C:\GAMS\apifiles\Python\api_36).
I tried running gams python.gms --pySetup=0
where python.gms is:
Could you point me to the correct settings?
Cheers
Renger
Thanks, but I don't manage to get it running (the wxAgg doesn't do the job either as I want to have a plot showing up).
Could you help me out (all on Windows 10 x64):
- I ran the setup.py script in the 3.6 directory
- I have my Python 3.6 in c:\users\renger\anaconda (there is also the 3.6.dll) and added this to my path.
- I have no new user variables defined (I tried a few like PYTHONPATH set to either the anaconda directory or to C:\GAMS\apifiles\Python\api_36).
I tried running gams python.gms --pySetup=0
Code: Select all
--- Execute embedded library embpycclib64.dll
--- Exception from Python: No module named 'tkinter'
--- python.gms(1) 3 Mb 1 Error
*** Status: Execution error(s)
Code: Select all
embeddedCode Python:
import matplotlib
import matplotlib.pyplot as plt
plt.plot([1,2,3,4])
plt.ylabel('some numbers')
plt.show()
endEmbeddedCode
Cheers
Renger
____________________________________
Enjoy modeling even more: Read my blog on modeling at The lazy economist
Enjoy modeling even more: Read my blog on modeling at The lazy economist
Re: Python Embedded in Gams - Import Python Package
Renger,
Regrading wxPython: I have attached the file which shows how to use Embedded Code and matplotlib/wxPython for generating a plot which shows up in a new window. Your Python interpreter needs to have both matplotlib and also wxPython installed.
Regarding PySetup=0: There seems to be a problem which might prevent this feature to work properly. The GAMS system directory contains a Python36.dll as well which is found by Embedded Code even though you specify PySetup=0. The only workaround that comes to my mind is to rename the Python36.dll in order to prevent it from being found. Note that this might introduce problems with certain tools (e.g. model2tex) which require the DLL in this location. Then modify your PATH environment variable before calling GAMS in the following way:
Please let me know if one of the suggestions work for you
Best,
Clemens
Regrading wxPython: I have attached the file which shows how to use Embedded Code and matplotlib/wxPython for generating a plot which shows up in a new window. Your Python interpreter needs to have both matplotlib and also wxPython installed.
Regarding PySetup=0: There seems to be a problem which might prevent this feature to work properly. The GAMS system directory contains a Python36.dll as well which is found by Embedded Code even though you specify PySetup=0. The only workaround that comes to my mind is to rename the Python36.dll in order to prevent it from being found. Note that this might introduce problems with certain tools (e.g. model2tex) which require the DLL in this location. Then modify your PATH environment variable before calling GAMS in the following way:
Code: Select all
set PATH=c:\users\renger\anaconda\;%PATH%
Best,
Clemens
Re: Python Embedded in Gams - Import Python Package
Hi Clemens
After installing wxPython, I can call the plot, so that is fine.
However, even after renaming the python3.6.dll in the Gams root directory, I still can't use my ananconda installation. For whatever reason, gams seems to still have problems with a missing tkinter.
Anyway, I can now work with the plots. Thanks!
Perhaps that the Python-Nerds are used to having virtual environments for every project and don't care about using several versions of installed versions (Gams, 3.6, 2.5, etc.). I am used to R and there you usually rely on just one installation. But it might be necessary for Python, as some packages don't run on the newest releases (which happens hardly using R...)
Cheers
Renger
After installing wxPython, I can call the plot, so that is fine.
However, even after renaming the python3.6.dll in the Gams root directory, I still can't use my ananconda installation. For whatever reason, gams seems to still have problems with a missing tkinter.
Anyway, I can now work with the plots. Thanks!
Perhaps that the Python-Nerds are used to having virtual environments for every project and don't care about using several versions of installed versions (Gams, 3.6, 2.5, etc.). I am used to R and there you usually rely on just one installation. But it might be necessary for Python, as some packages don't run on the newest releases (which happens hardly using R...)
Cheers
Renger
____________________________________
Enjoy modeling even more: Read my blog on modeling at The lazy economist
Enjoy modeling even more: Read my blog on modeling at The lazy economist
Re: Python Embedded in Gams - Import Python Package
Good to hear that the plotting is working.
Perhaps one could start from a setup where no Python is found at all. This means that the python36.dll in the GAMS system directory is renamed. Furthermore the PYTHONPATH and also the PATH should be empty or at least should not contain any PYTHON or GAMS specific information. A GAMS run with pysetup=0 should then fail in any case. As a second step, the location of the python36.dll in the Anaconda version of Python is added to the PATH which should result in embedded code to work. As a third step we can try to import Tkinter (which should work as well).
Best,
Clemens
Perhaps one could start from a setup where no Python is found at all. This means that the python36.dll in the GAMS system directory is renamed. Furthermore the PYTHONPATH and also the PATH should be empty or at least should not contain any PYTHON or GAMS specific information. A GAMS run with pysetup=0 should then fail in any case. As a second step, the location of the python36.dll in the Anaconda version of Python is added to the PATH which should result in embedded code to work. As a third step we can try to import Tkinter (which should work as well).
Best,
Clemens