Using Gurobi 8.1.0 in PyCharm

已回答
Hello everyone:)
 
I need Gurobi for some optimization problems and downloaded version 8.1.0 and installed the package in my terminal (I am using  Mac). In the terminal Gurobi seems to work. I would like to use it within Pycharm though.
My first command  "from gurobipy import *" leads to the follwoing error:
 
ModuleNotFoundError: No module named 'gurobipy'
 
In Jupyter Notebook the code works fine...
 
Can anyone help me with this?
 
Thank you in advance and kind regards
Kristina
 
 
 
 
1

Hello Kristina,

Do you have it listed among other packages in your Project Interpreter (Preferences | Project | Project Interpreter)?

If not, you are probably using different interpreters in PyCharm and in the system terminal.

0

Hello Sergey,

thank you very much for your reply. Where and how do I change that? I am still very new to PyCharm and Gurobi.

Thanks in advance for your help!

0

First of all, you need to know which interpreter is currently used in your system terminal (the one with Gurobi installed). To do so, you can run which python in system terminal  and then go to Preferences | Project | Project Interpreter in PyCharm and add this interpreter (here is documentation on how to configure interpreters).

Another way is to install Gurobi to your current project interpreter in PyCharm. Please see our tutorial on how to install/uninstall/upgrade packages in PyCharm.

I suppose you need to install gurobipy package.

3

After running "which python" it showed the following:  /Applications/anaconda3/bin/python

When I put that into the interpreter of PyCharm it says /Applications/anaconda3/bin/python: can't open file 'create': [Errno 2] No such file or directory

What did I do wrong? Would it help to unistall Anaconda and Gurobi and download again? What do I have to keep in mind when doing so to make sure they all use the same interpreter?

Thank you very much for your patience!

0

>When I put that into the interpreter of PyCharm it says /Applications/anaconda3/bin/python: can't open file 'create': [Errno 2] No such file or directory

Where did you paste that?

I guess you should just browse to that python executable like this:

By the way, have you tried to install Gurobi to your current Project Interpreter?

1

Thank you so much! I followed the link and it is now working!

Gurobi opens perfectly in PyCharm now. I started working with the optimization by trying a very simple example:

from gurobipy import *

m = model()
x = m.addVar (vtype=GRB.BINARY, name="x")
y = m.addVar (vtype=GRB.BINARY, name="y")
z = m.addVar (vtype=GRB.BINARY, name="z")

m.setObjective(x+y+2*z, GRB.MAXIMIZE)
c1=m.addConstr(x+2*y+3*z<=4)
c2=m.addConstr(x+y>=1)

m.optimize()

I get the following mistake in the m=model() line: NameError: name 'model' is not defined

Can you help me with that as well?

Thank you and kind regards :)

 

0

The error message indicates that model() is not defined in your code.

If you would like to return something from model() function and put it inside variable, then you should obviously write this function beforehand.

Something like this:

test_var = 12


def model():
return test_var


m = model()

It's rather Python related question than PyCharm.

1

Perfect, thank you!

I will try implementing that into my model.

Where can I post questions like this concering the code?

Thank you for all your time!

0

You are welcome!

Actually, any python related forum. You could use https://python-forum.io. You may also find useful Python FAQ.

0

Hi @Hamedjabarian,

Please open the terminal in PyCharm and try to install manually with `pip install <module_name>`,

If you'll see the same error, it's probably package specific and not IDE-related, in which case you should ask the module developer/community for help.

0

Hi

i am having the same problem as @Hamedjabarian, i've been struggling to install the gurobipy package on pycharm but it didn't work it shows me the same error message.

0

Hi Marouane,

Have you tried to install it from the system terminal? Do you get the same error message?

0

yeah i have tried it from the system terminal and it's still the same error message

0

It means that the problem is not related to PyCharm. Please search the package-related forums for similar problems.

0

what i knew is that this package cannot be installed using pip

0

I would like to follow up on this one. I think the problem is related to PyCharm.

1. On my Ubuntu, I have installed Gurobi (https://www.gurobi.com/documentation/8.1/quickstart_linux/software_installation_guid.html) and I am able to verify that installation is correct as I am able to solve some of small models that are included as part documentation. Even the simple snippet below will verify whether import is successful:

from gurobipy import *
m = Model("model")

2. I created virtual environment using `python3 -m --system-site-packages venv_gurobi`. Using terminal I activate my venv. Then using `which python3` I verify that it indeed points to python executable located inside virtual environment folder.

3. I can run the script pasted above and it passes through.

4. I open the same script in PyCharm, add a new configuration, make sure that interpreter is set as created virtual environment but I am getting

ImportError: libgurobi81.so: cannot open shared object file: No such file or directory

5. Oddly enough, when I open terminal window inside PyCharm and run script there, it runs through fine. So there seems to be some discrepancy between how terminal is setup and PyCharm.

6. I setup the following env vars in PyCharm configuration `LD_LIBRARY_PATH=/opt/gurobi811/linux64/lib;GUROBI_HOME=/opt/gurobi811/linux64` and restarted PyCharm and it seemed to help. And I needed to past full paths as it does not work with substituting variables as indicated in Gurobi documentation. However I would like to understand why PyCharm is not picking up settings that I set in terminal using `.bashrc`.

0

We have a known issue with LD_LIBRARY_PATH variable setting https://youtrack.jetbrains.com/issue/PY-29580, feel free to vote and leave comments.

0

Dear Sergey

indeed thanks for you helps

my problem is somehow peculiar as some friends never faced that

I am using Pycharm IDE and it works great unless using Gurobi attributes and predefined functions the auto completion fails.  for example:

m = Model()

in the nest line  I want to add the first constraint and i expect to receive suggestions after typing m

but only these common attributes are available:

if, ifn, ifnn, main, ....

 

any help please

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

0

Hamedjabarian

Your problem seems to be different from the one in the post description.

Please create a ticket in our issue tracker https://youtrack.jetbrains.com/issues/PY and provide a code snippet that we can use to reproduce the issue.

0

请先登录再写评论。