ModuleNotFoundError: No module named 'pyomo'

Question:

I am trying to run a jupyter notebook with pyomo, but get this error when trying to import from pyomo.environ.


!sapt-get install -y -qq coinor-cbc

from pyomo.environ import *

E: Could not open lock file /var/lib/dpkg/lock-frontend - open (13: Permission denied)
E: Unable to acquire the dpkg frontend lock (/var/lib/dpkg/lock-frontend), are you root?

---------------------------------------------------------------------------

ModuleNotFoundError                       Traceback (most recent call last)
Cell In [2], line 2
1 get_ipython().system('apt-get install -y -qq coinor-cbc')
----> 2 from pyomo.environ import *

ModuleNotFoundError: No module named 'pyomo'

I have already installed pyomo with the command !pip install Pyomo==5.7.1. It installs correctly — I get the message Successfully installed PyUtilib-6.0.0 Pyomo-5.7.1 nose-1.3.7 ply-3.11 and when I check the version with !pyomo --version I get Pyomo 5.7.1 (CPython 3.8.10 on Linux 5.10.16.3-microsoft-standard-WSL2).

Does anyone know what is wrong? I’ve already taken a look at this post.

Asked By: aa7

||

Answers:

First step: If you have recently installed or updated versions, etc., log out completely and log back in to ensure you are using current environment variables. Then…

It is likely that your jupyter module is running inside a different python framework than the one you installed pyomo in.

Check it within the notebook. I haven’t used mine in a while so it is a good example of mismatched frameworks…

In the notebook:

import sys
print(sys.version)

In a terminal window check the default version

which python3

These should match. Mine currently do not, but my older version had pyomo as well, so it doesn’t fail on the import… but look at the version…:

enter image description here

Terminal. This shows the framework that I’m currently using (and by default installing new modules into):

% which python3
/Library/Frameworks/Python.framework/Versions/3.11/bin/python3
% 

If this is the case, update your jupyter install with pip3 in the terminal:

% pip3 install --upgrade jupyter

Then log out and log back in, open a new notebook and re-check:

enter image description here

This should ensure that the modules you installed are in the same framework that jupyter is using.

Answered By: AirSquid