Skip to main content

Setting up gurobipy in Spyder: ModuleNotFoundError: No module named 'gurobipy'

Answered

Comments

16 comments

  • Official comment
    Simranjit Kaur
    Gurobi Staff Gurobi Staff
    This post is more than three years old. Some information may not be up to date. For current information, please check the Gurobi Documentation or Knowledge Base. If you need more help, please create a new post in the community forum. Or why not try our AI Gurobot?.
  • Matthias Miltenberger
    Gurobi Staff Gurobi Staff

    Hi Michael,

    You need to have Gurobipy installed in the same environment that Spyder is using. Please have a look at this guide and try executing the commands in step 2 withing Spyder. This will install Gurobi to the Spyder environment.

    Best regards,
    Matthias

    0
  • Behrooz Pourghannad
    Gurobi-versary
    First Comment

    Hi Matthias,

    I have exactly the same problem. I followed the steps you mentioned (in the link). Still I get the same result. 

    On executing the command, I get the following, I am not sure what to do.

    Error processing line 7 of C:\Users\Behrooz\Anaconda3\lib\site-packages\pywin32.pth:

    Traceback (most recent call last):
    File "C:\Users\Behrooz\Anaconda3\lib\site.py", line 168, in addpackage
    exec(line)
    File "<string>", line 1, in <module>
    ModuleNotFoundError: No module named 'pywin32_bootstrap'

    Remainder of file ignored

    Building graph of deps: 0%| | 0/2 [00:00<?, ?it/s]
    Examining gurobi: 0%| | 0/2 [00:00<?, ?it/s]
    Examining python=3.7: 50%|##### | 1/2 [00:00<00:00, 45.48it/s]

    Determining conflicts: 0%| | 0/2 [00:00<?, ?it/s]
    Examining conflict for gurobi python: 0%| | 0/2 [00:00<?, ?it/s]

    UnsatisfiableError: The following specifications were found
    to be incompatible with the existing python installation in your environment:

    Specifications:

    - gurobi -> python[version='2.7.*|3.5.*|>=2.7,<2.8.0a0|>=3.6,<3.7.0a0']

    Your python: python=3.7

    If python is on the left-most side of the chain, that's the version you've asked for.
    When python appears to the right, that indicates that the thing on the left is somehow
    not available for the python version you are constrained to. Note that conda will not
    change your python version to a different minor version unless you explicitly specify
    that.

    0
  • Matthias Miltenberger
    Gurobi Staff Gurobi Staff

    Hi,

    Can you please have a look at this SO post? Please try running

    pip install pywin32

    or

    conda install pywin32

    Cheers,
    Matthias

    0
  • Behrooz Pourghannad
    Gurobi-versary
    First Comment

    Hi Matthias,

    I followed your suggestion, it still does not work. 

    pywin32 is already installed:

    and the channel is established:

    But still I get:

    Any other step I need to go through?

    0
  • GISELLE COUTO
    Gurobi-versary
    First Comment

    you need to run the prompt with the administrator. and
    in the folder where gurobi is, look for where you installed your win64 or win32 gurobi, find the setup.py file and


    then type the command

    python setup.py install

    that alone will solve your problem!

    0
  • Matthias Miltenberger
    Gurobi Staff Gurobi Staff

    There is a simpler option now with Gurobi 9.1 using the pip installer:

    python -m pip install -i https://pypi.gurobi.com gurobipy

    This will install the gurobipy module in your currently activated Python interpreter.

    Cheers,
    Matthias

    1
  • Doğanay Özese
    Gurobi-versary
    First Comment

    Hi Matthias, 

    I've been living same problem for many days, could not handle it whatever I did. Luckily, I saw this post of yours written a few hours ago, tried it and resolved the issue with just one line. Thank you very much for your support Matthias Miltenberger.

    Cheers.

    0
  • Diarmuid Henry
    Gurobi-versary
    First Comment

    I have the same problem:

    ModuleNotFoundError: No module named 'gurobipy'

    is returned whenever I try to use gurobipy.

    I have installed the software (gurobi911), installed my license, and used the pip command to install (swapping python with python3, as the former came up with an errror), which returned this (after running it for a second time):

    Looking in indexes: https://pypi.gurobi.com

    Requirement already satisfied: gurobipy in /Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages (9.1.1)

     

     

    I have also tried installing it using

    python setup.py install

    when in the directory, which returns

     

    running install

    running build

    running build_py

    running install_lib

    running install_egg_info

    Removing /Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/gurobipy-9.1.1-py3.9.egg-info

    Writing /Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/gurobipy-9.1.1-py3.9.egg-info

    removing /Library/gurobi911/mac64/build

     

     

    Yet I still get the aforementioned error when trying to import gurobipy. 

     

    I have literally spent about 10/15 hours in total now, scouring the internet for solutions, none of which have worked. Any ideas?

     

    I'm using Spyder5, if that helps/makes a difference.

     

    0
  • Eli Towle
    Gurobi Staff Gurobi Staff

    Do you encounter the \( \texttt{ModuleNotFoundError} \) within the Spyder IDE? It sounds like you are installing \(\texttt{gurobipy}\) into a Python environment that is different from the Python environment used by Spyder. You can check by running the following code in Spyder:

    import sys
    print(sys.path)

    I suspect the output does not include

    /Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages

    which is where you've installed the \( \texttt{gurobipy} \) package.

    Here are two options:

    • If your Spyder installation uses conda, you can install the \( \texttt{gurobipy} \) package by running \( \texttt{conda install gurobi} \) in Spyder's IPython console.
    • If you have a standalone Spyder installation (i.e., you separately downloaded the Spyder app), you can configure the IDE to use your system's default Python 3.9 environment. Follow the instructions from the Spyder IDE documentation. You probably want to configure Spyder to use the Python interpreter located at
      /Library/Frameworks/Python.framework/Versions/3.9/bin/python
      Note that you will also need to install the \( \texttt{spyder-kernels} \) package into this Python environment with \(\texttt{pip install spyder-kernels}\). Then, restart the IPython kernel and try importing \( \texttt{gurobipy} \).
    0
  • Diarmuid Henry
    Gurobi-versary
    First Comment

    Thanks Eli: this has partially solved the problem. I now receive the same error in Spyder, as I do when I run a python script (importing gurobipy) in Terminal. However, this is now the case whether I am in in the default Python environment, or the one you advised I should switch to. And, unfortunately, it seems Spyder has lost the ability to import any other packages whatsoever...

    The code I'm running starts with:

    import gurobipy as gp
    from gurobipy import GRB


    The error is returned at the second line, where I get the message:

    ImportError: cannot import name 'GRB' from 'gurobipy' (/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/gurobipy/__init__.py)

     

    And now when I try to run a simple script, without gurobipy, (fx something just importing numpy), I get the following error:

    from numpy import *

    ModuleNotFoundError: No module named 'numpy'

     

    I seem to be even further from a solution than I was when I started now...

    0
  • Eli Towle
    Gurobi Staff Gurobi Staff

    At the very least, it's good that you get the same output from Spyder and the Terminal. This suggests Spyder is properly configured to use your system's Python 3.9 distribution.

    Could you post the full output you see when you run \( \texttt{sudo python3 setup.py install} \) from \( \texttt{/Library/gurobi911/mac64} \)?

    About this error:

    ModuleNotFoundError: No module named 'numpy'

    I wouldn't expect \(\texttt{numpy}\) to be installed by default into the Python distribution you're using. Can you confirm you installed \(\texttt{numpy}\) with \( \texttt{pip3 install numpy} \)?

     

    0
  • Antonio Dominguez-Ramos

    Dear all,

    After using

    python -m pip install gurobipy 

    and getting 

    Requirement already satisfied: gurobipy in /home/antoniodominguezramos/miniconda3/lib/python3.8/site-packages (9.1.2)

    I type

    conda list

    in my active environment and it does not show up. Any clue?

    I have checked that gurobi is active after testing the coin.lp test example.

    Thanks in advance!!

    0
  • Matthias Miltenberger
    Gurobi Staff Gurobi Staff

    Hi Antonio,

    It seems you have installed gurobipy in the base environment of conda. It's actually best practice to leave the base env untouched and create new environments for every new use case or application. It is also not guaranteed that conda list will display packages that have been installed using pip - although, I do see this on my machine.

    When working with these virtual envs it is crucial to not lose track of the currently active env. Please note that this is not primarily a Gurobi issue, but a general Python/PIP/conda problem.

    Cheers,
    Matthias

    0
  • Hristo Vassilev
    Gurobi-versary
    First Comment

    Hi Matthias,

    I have the same problem using Spyder v.5. I am new to this, so I don't understand much, however when trying to instal gurobi through anaconda prompt or cmd with commands such as "conda install gurobi" or "python setup.py install" I get this:

    UnsatisfiableError: The following specifications were found
    to be incompatible with the existing python installation in your environment:

    Specifications:

    - gurobi -> python[version='2.7.*|3.5.*|>=2.7,<2.8.0a0|>=3.7,<3.8.0a0|>=3.8,<3.9.0a0|>=3.6,<3.7.0a0|3.4.*']

    Your python: python=3.9

    If python is on the left-most side of the chain, that's the version you've asked for.
    When python appears to the right, that indicates that the thing on the left is somehow
    not available for the python version you are constrained to. Note that conda will not
    change your python version to a different minor version unless you explicitly specify
    that.

    After hours of browsing I have no idea how to solve this, so I seek help. Thanks in advance.

    Kind regards

    0
  • Matthias Miltenberger
    Gurobi Staff Gurobi Staff

    Hi Hristo!

    We don't provide a Gurobi Anaconda package for Python 3.9. You can check the available packages here: Files :: Anaconda.org

    Please note that Gurobi 9.5 is compatible with Python 3.9. You can either use the package provided via pip (gurobipy · PyPI) using

    python -m pip install gurobipy

    or you run

    python setup.py install

    from within your Gurobi installation after you have completed the installation of our package.

    I hope that helps to solve your issue!

    Cheers,
    Matthias

    0

Post is closed for comments.