ImportError: No module named pymol (Mac)
1
1
Entering edit mode
9.6 years ago
lqdo2000 ▴ 20

Hi,

Being new to python scripting, I am facing a simple problem.

I am writing a python script "MyScript.py" in which I am calling the third-part program PyMOL through the import function.

Here is the simplified script (mainly inspired from this blog: http://doeidoei.wordpress.com/2009/02/11/pymol-api-simple-example/)

#! /usr/bin/python
# Usage: MyScript.py <input>

# launch PyMOL from the terminal
import __main__
__main__.pymol_argv = [ 'pymol', '-qc']

import sys
import pymol

pymol.finish_launching()

# Input
file = sys.argv[1]

#run PyMOL command
pymol.cmd.do("load %s" % file)

#Exit PyMOL
pymol.cmd.quit()

I get this error message:

Traceback (most recent call last):
  File "./MyScript.py", line 10, in <module>
    import pymol
ImportError: No module named pymol

My path to Python site-packages: /Library/Python/2.7/site-packages

My path to PyMOL: /Users/me/my_apps/MacPyMOL_1.3.app/Contents/MacOS/MacPyMOL

(I made an alias pymol='MacPyMOL' in .bash_profile)

I also tried to replace in the script:

import sys
import pymol

by

import sys
sys.path.append("/Users/me/my_apps/MacPyMOL_1.3.app/Contents/MacOS")
import MacPyMOL

but still the same.

I looked at other forums but it didn't solve the problem:

If someone could direct me to a solution...

Thanks!

pymol python • 16k views
ADD COMMENT
2
Entering edit mode
9.6 years ago
João Rodrigues ★ 2.5k

Hi,

In MacOSX with a default installation of MacPymol you cannot use the API because there is no _cmd.so file. What I usually do it to install pymol via fink/macports or compile it from source somewhere. Not the best solution but then you can get the file you are missing.

If you manage to find a better solution, it'd be great to share !

ADD COMMENT
0
Entering edit mode

Hi João,

Thanks for your help !

I followed your advice and installed PyMOL and all the dependencies with MacPort (it installed it in /opt/local/bin/pymol). I also linked pymol binary to /Library/Python2.7/site-packages. But still the same error (i.e. ImportError: no module named pymol). I am clearly missing something. Do I have to modify $PYMOL_PATH, $PYTHON_PATH or something?

Note1:

$ pymol -q
Detected OpenGL version 2.0 or greater. Shaders available.
Detected GLSL version 1.20.
Adjusting settings to improve performance for ATI cards.

It opens the windows (python icon is jumping on the dock), but I can write anything I want, nothing happens

Note2:

​In PyMOL source, there is a file "PACKAGING" mentioning the below information. Do I need to relocate the files after a MacPort installation?

Under MacOS X, PYMOL_PATH is self-defining.  Under Windows, this
environment variable is stored in the registry during installation.
Under Unix, PYMOL_PATH needs to be defined prior to startup, usually
by "pymol.com".

=======================================================
Suggested File Locations (for package managed installs)
=======================================================

== pymol package ==

modules/pymol  -> /usr/lib/pythonX.X/site-packages/pymol
modules/chempy -> /usr/lib/pythonX.X/site-packages/chempy
modules/pmg_tk -> /usr/lib/pythonX.X/site-packages/pmg_tk

scripts     -> /usr/share/pymol/scripts (RUNTIME REQUIREMENT)
data        -> /usr/share/pymol/data (RUNTIME REQUIREMENT)

examples    -> /usr/share/doc/pymol/examples
doc         -> /usr/share/doc/pymol/doc
LICENSE     -> /usr/share/doc/pymol/LICENSE
CHANGES     -> /usr/share/doc/pymol/CHANGES
README      -> /usr/share/doc/pymol/README

== pymol-devel package ==

test        -> /usr/share/pymol/test (TESTING RUNTIME REQUIREMENT)

DEVELOPERS  -> /usr/share/doc/pymol/DEVELOPERS
PACKAGING   -> /usr/share/doc/pymol/PACKAGING
ADD REPLY
0
Entering edit mode

It's not the binary you should be linking!

After you installed PyMOL from macports, look in /opt for the location of _cmd.so.

find /opt/ -name '_cmd.so'

This should be in a folder somewhere. The parent of this folder ( e.g. if /opt/xxx/pymol then /opt/xxx), should be added to the PYTHONPATH. Then try to import. Make sure that no other folder with pymol is in the PYTHONPATH, to avoid conflicts.

ADD REPLY
0
Entering edit mode

Fixed !

I exported the path to the parent folder of the one containing _cmd.so to PYTHONPATH in my ~/.bash_profile. Works like a charm !

Thanks a lot João for your help!

ADD REPLY

Login before adding your answer.

Traffic: 2966 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6