Python API picking up wrong GAMS version Topic is solved

Questions on using the GAMS APIs (Python, .NET, etc.)
Post Reply
akingCCC
User
User
Posts: 4
Joined: 1 year ago

Python API picking up wrong GAMS version

Post by akingCCC »

I'm trying to install the GAMS Python API using Anaconda, but it is picking up an old version of GAMS (38) that I have installed on my machine. I have GAMS 38-46 currently installed, and am trying to use the api with Gams 46.4.0, the latest version installed on my machine.

I started by creating a new environment in Anaconda, and in a command line entered:

Code: Select all

(gams_py46) C:\Users\myusername>pip install gamsapi[all]==46.4.0
which seemed to install successfully. Then (following the instructions in https://www.gams.com/latest/docs/API_PY ... ARTED.html), I entered

Code: Select all

python -c "import gams; print(f'API OK -- Version {gams.__version__}')"
which gave me

Code: Select all

Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "C:\GAMS\38\apifiles\Python\gams\gams\__init__.py", line 32, in <module>
    from .workspace import *
  File "C:\GAMS\38\apifiles\Python\gams\gams\workspace.py", line 32, in <module>
    from .database import *
  File "C:\GAMS\38\apifiles\Python\gams\gams\database.py", line 26, in <module>
    from gdxcc import *
ModuleNotFoundError: No module named 'gdxcc'
so clearly it is trying to get something from GAMS 38 instead of GAMS 46. So I tried

Code: Select all

(gams_py46) C:\Users\myusername>python -m site
and got

Code: Select all

sys.path = [
    'C:\\Users\\myusername',
    'C:\\GAMS\\38\\apifiles\\Python\\gams',
    'C:\\Users\\myusername\\%PYTHONPATH%',
    'C:\\Anaconda3\\envs\\gams_py46\\python311.zip',
    'C:\\Anaconda3\\envs\\gams_py46\\DLLs',
    'C:\\Anaconda3\\envs\\gams_py46\\Lib',
    'C:\\Anaconda3\\envs\\gams_py46',
    'C:\\Anaconda3\\envs\\gams_py46\\Lib\\site-packages',
]
USER_BASE: 'C:\\Users\\myusername\\AppData\\Roaming\\Python' (exists)
USER_SITE: 'C:\\Users\\myusername\\AppData\\Roaming\\Python\\Python311\\site-packages' (doesn't exist)
ENABLE_USER_SITE: True
The instructions tell me to correct the GAMS path in the file sitecustomize.py, but there is no such file in the USER_BASE folder (and the USER_SITE folder doesn't exist), and any other files I can find with this name don't have a C:\GAMS\38 path in them (or any other explicit GAMS path for that matter).

I don't really want to uninstall GAMS 38 as I need to retain that for the working version of the code I want to upgrade. How do I fix this path issue? Thanks!
User avatar
Clemens
Posts: 60
Joined: 7 years ago

Re: Python API picking up wrong GAMS version

Post by Clemens »

Hi,

Something seems to put in the GAMS 38 path into your configuration. Have you checked if the PYTHONPATH environment variable is set? Run from the Anaconda command prompt:

Code: Select all

echo %PYTHONPATH%
Best,
Clemens
User avatar
dirkse
Moderator
Moderator
Posts: 215
Joined: 7 years ago
Location: Fairfax, VA

Re: Python API picking up wrong GAMS version

Post by dirkse »

Hello,

It sounds to me like the GAMS 38 in the sitecustomize.py file is confusing your fresh conda install.

Many years ago I used the Python site package, but once I started to use miniconda I dropped it completely. I found that the two don't work well together. And at some level they are fundamental incompatible. With conda I want to create a self-contained environment that is independent of other Python stuff (conda or otherwise). But with the site package, I am sharing settings between Python versions.

-Steve
akingCCC
User
User
Posts: 4
Joined: 1 year ago

Re: Python API picking up wrong GAMS version

Post by akingCCC »

Clemens wrote: 1 week ago Hi,

Something seems to put in the GAMS 38 path into your configuration. Have you checked if the PYTHONPATH environment variable is set? Run from the Anaconda command prompt:

Code: Select all

echo %PYTHONPATH%
Best,
Clemens
Hi Clemens,

Yes, thanks, I think that's it! When I put that into the command prompt, I get

Code: Select all

C:\GAMS\38\apifiles\Python\gams;%PYTHONPATH%
and I remember changing something along those lines a year or two ago when we were setting up the original connection between GAMS and Python.

To remove it, in my Windows search I looked up "edit environment variables for your account", which opened the Environment Variables box (similar to but distinct from the System Environment Variables). In the top box User variables, I had a couple of variables that had values that included the path to GAMS 38. I deleted those and closed that box, closed and restarted Anaconda, then checked the echo %PYTHONPATH% command again, and the reference to GAMS 38 was gone. I then reinstalled the API using the commands above and checked the install, and this time it told me it was using the right version of GAMS:

Code: Select all

C:\Users\myusername>python -c "import gams; print(f'API OK -- Version {gams.__version__}')"
API OK -- Version 46.4.0
Now I just need to try using it...
Post Reply