Error occurs when running import cobra

1,331 views
Skip to first unread message

Yu Chen

unread,
Aug 7, 2017, 12:00:56 PM8/7/17
to cobra pie
Hi,

I installed cobra 0.8.1 following the procedure here (https://github.com/opencobra/cobrapy/blob/master/INSTALL.rst), but I got the error "ImportError: No module named test" when I ran either "import cobra" or "import cobra.test". Could someone help to fix this?

I am using Python 2.7.13 and on macOS Sierra 10.12.6.

Thanks in advance!

Yu

henning.red

unread,
Aug 7, 2017, 12:59:20 PM8/7/17
to cobra pie
Hi Yu,

Seems the installation did not succeed, can you post the whole output you get when you run `pip install cobra`? Are you using a virtual environment (see http://docs.python-guide.org/en/latest/dev/virtualenvs/)?

Cheers, Henning

Yu Chen

unread,
Aug 8, 2017, 4:49:08 AM8/8/17
to cobra pie
Thanks for your reply! Below is the output of "pip install cobra", can you tell me if the installation is fine? I did not use a virtual environment but I will try then.

Requirement already satisfied: cobra in /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages
Requirement already satisfied: swiglpk in /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages (from cobra)
Requirement already satisfied: pandas>=0.17.0 in /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages (from cobra)
Requirement already satisfied: future in /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages (from cobra)
Requirement already satisfied: numpy>=1.6 in /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages (from cobra)
Requirement already satisfied: optlang>=1.2.1 in /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages (from cobra)
Requirement already satisfied: tabulate in /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages (from cobra)
Requirement already satisfied: ruamel.yaml<0.15 in /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages (from cobra)
Requirement already satisfied: python-dateutil in /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages (from pandas>=0.17.0->cobra)
Requirement already satisfied: pytz>=2011k in /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages (from pandas>=0.17.0->cobra)
Requirement already satisfied: six>=1.9.0 in /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages (from optlang>=1.2.1->cobra)
Requirement already satisfied: sympy>=1.0.0 in /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages (from optlang>=1.2.1->cobra)
Requirement already satisfied: ruamel.ordereddict in /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages (from ruamel.yaml<0.15->cobra)
Requirement already satisfied: mpmath>=0.19 in /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages (from sympy>=1.0.0->optlang>=1.2.1->cobra)

Yu

Yu Chen

unread,
Aug 8, 2017, 5:36:26 AM8/8/17
to cobra pie
I tried a virtual environment, i.e. uninstalled the previous cobra package and then installed it inside a virtual folder. Still I got the same error when running "import cobra" in python. And below is the output of "pip install cobra" in the virtual environment.

Requirement already satisfied: cobra in ./VirtualENV/lib/python2.7/site-packages
Requirement already satisfied: swiglpk in ./VirtualENV/lib/python2.7/site-packages (from cobra)
Requirement already satisfied: pandas>=0.17.0 in ./VirtualENV/lib/python2.7/site-packages (from cobra)
Requirement already satisfied: future in ./VirtualENV/lib/python2.7/site-packages (from cobra)
Requirement already satisfied: numpy>=1.6 in ./VirtualENV/lib/python2.7/site-packages (from cobra)
Requirement already satisfied: optlang>=1.2.1 in ./VirtualENV/lib/python2.7/site-packages (from cobra)
Requirement already satisfied: tabulate in ./VirtualENV/lib/python2.7/site-packages (from cobra)
Requirement already satisfied: ruamel.yaml<0.15 in ./VirtualENV/lib/python2.7/site-packages (from cobra)
Requirement already satisfied: python-dateutil in ./VirtualENV/lib/python2.7/site-packages (from pandas>=0.17.0->cobra)
Requirement already satisfied: pytz>=2011k in ./VirtualENV/lib/python2.7/site-packages (from pandas>=0.17.0->cobra)
Requirement already satisfied: six>=1.9.0 in ./VirtualENV/lib/python2.7/site-packages (from optlang>=1.2.1->cobra)
Requirement already satisfied: sympy>=1.0.0 in ./VirtualENV/lib/python2.7/site-packages (from optlang>=1.2.1->cobra)
Requirement already satisfied: ruamel.ordereddict in ./VirtualENV/lib/python2.7/site-packages (from ruamel.yaml<0.15->cobra)
Requirement already satisfied: mpmath>=0.19 in ./VirtualENV/lib/python2.7/site-packages (from sympy>=1.0.0->optlang>=1.2.1->cobra)

Yu

henning.red

unread,
Aug 8, 2017, 5:51:46 AM8/8/17
to cobra pie
The installations look good (both with and without the virtual env), then it is just for some reason the wrong search path, you are sure you activated your virtual environment? What is the output of
`which python` ?

And in python, what is the output of 
```
import sys
sys.path
```
?

Yu Chen

unread,
Aug 8, 2017, 7:25:18 AM8/8/17
to cobra pie
Yes, I think I activated the virtual environment.

The output of "which python" is:

/Users/cheyu/VirtualENV/bin/python


The output of "import sys" and then "sys.path" is:

['', '/Users/cheyu/VirtualENV/lib/python27.zip', '/Users/cheyu/VirtualENV/lib/python2.7', '/Users/cheyu/VirtualENV/lib/python2.7/plat-darwin', '/Users/cheyu/VirtualENV/lib/python2.7/plat-mac', '/Users/cheyu/VirtualENV/lib/python2.7/plat-mac/lib-scriptpackages', '/Users/cheyu/VirtualENV/lib/python2.7/lib-tk', '/Users/cheyu/VirtualENV/lib/python2.7/lib-old', '/Users/cheyu/VirtualENV/lib/python2.7/lib-dynload', '/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7', '/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-darwin', '/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk', '/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac', '/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac/lib-scriptpackages', '/Users/cheyu/VirtualENV/lib/python2.7/site-packages']

henning.red

unread,
Aug 8, 2017, 10:10:40 AM8/8/17
to cobra pie
That's bizarre.. so just to be sure, can you

source activate VirtualEnv/bin/activate
python -c 'import cobra; print(cobra)'

again and then post here the entire input/output, should look something like this

(py36)   proj source cobrapy/py36/bin/activate

(py36)   proj python -c 'import cobra; print(cobra)'

<module 'cobra' from '/Users/henred/proj/cobrapy/py36/lib/python3.6/site-packages/cobra/__init__.py'>


//Henning

Yu Chen

unread,
Aug 8, 2017, 11:03:21 AM8/8/17
to cobra pie
Please see input and output below:

xxxx:~ cheyu$ source VirtualENV/bin/activate

(VirtualENV) xxxx:~ cheyu$ python -c "import cobra; print cobra"

Traceback (most recent call last):

  File "<string>", line 1, in <module>

  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/cobra.py", line 1, in <module>

ImportError: No module named test


Besides, I tried another package installed together with cobra, e.g. pandas, it worked:

(VirtualENV) xxxx:~ cheyu$ python -c "import pandas; print pandas"

<module 'pandas' from '/Users/cheyu/VirtualENV/lib/python2.7/site-packages/pandas/__init__.pyc'>


Then I tried "pip list", I found both cobra and pandas are in the activated virtual environment:


(VirtualENV) xxxx:~ cheyu$ pip list

cobra (0.8.1)

future (0.16.0)

mpmath (0.19)

numpy (1.13.1)

optlang (1.2.1)

pandas (0.20.3)

pip (9.0.1)

python-dateutil (2.6.1)

pytz (2017.2)

ruamel.ordereddict (0.4.13)

ruamel.yaml (0.14.12)

setuptools (36.2.7)

six (1.10.0)

swiglpk (1.4.3)

sympy (1.1.1)

tabulate (0.7.7)

wheel (0.29.0)

Yu Chen

unread,
Aug 8, 2017, 11:40:57 AM8/8/17
to cobra pie
I think I have fixed this already.

I just deleted a file named "cobra.pyc" in the folder "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7", and then cobra worked. The file could be moved to that folder by accident. But I don't know why this leads to the error.

henning.red

unread,
Aug 8, 2017, 11:56:53 AM8/8/17
to cobra pie

This is a clue:


  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/cobra.py", line 1, in <module>


Python tries to load cobra from outside your virtual env so something is wrong in the order of sys.path. Removing it from there completely should sort it but not sure why the path problem to begin with... Anyway, great you managed to get around it.

Yu Chen

unread,
Aug 9, 2017, 3:36:42 AM8/9/17
to cobra pie
Thanks Henning for your kind help and explanation.

Yu
Reply all
Reply to author
Forward
0 new messages