0

I have downloaded the rpy2 package using MacPorts, but every time I try to import it using my IDE (Enthought Canopy version 1.5.4), I get the following error:

ImportError: No module named rpy2 

However, when I run Python in my terminal and run import rpy2 there, it imports without any issue.

I did some googling on the issue, and it appears that Python's environmental variables in the terminal are not the same as the Python's environmental variables in the IDE, particularly the PYTHONPATH. I checked my PYTHONPATH in my terminal by running import sys; sys.path and got the following file paths:

/usr/local/lib/python2.7/site-packages
/Users/bob
/Library/Frameworks/Python.framework/Versions/2.7/lib/python27.zip
/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/plat-mac
/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac/lib-scriptpackages
/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk
/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-old
/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload
/Users/bob/Library/Python/2.7/lib/python/site-packages
/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages
/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/rpy2-2.8.6-py2.7-macosx-10.6-intel.egg

The very last file path points to the location of the module that won't be imported in my IDE.

I then ran the same commands on a script in my IDE and got the following file paths:

/Applications/Canopy.app/appdata/canopy-1.5.4.3105.macosx-x86_64/Canopy.app/Contents/lib/python27.zip
/Applications/Canopy.app/appdata/canopy-1.5.4.3105.macosx-x86_64/Canopy.app/Contents/lib/python2.7
/Applications/Canopy.app/appdata/canopy-1.5.4.3105.macosx-x86_64/Canopy.app/Contents/lib/python2.7/plat-darwin
/Applications/Canopy.app/appdata/canopy-1.5.4.3105.macosx-x86_64/Canopy.app/Contents/lib/python2.7/plat-mac
/Applications/Canopy.app/appdata/canopy-1.5.4.3105.macosx-x86_64/Canopy.app/Contents/lib/python2.7/plat-mac/lib-scriptpackages
/Applications/Canopy.app/appdata/canopy-1.5.4.3105.macosx-x86_64/Canopy.app/Contents/lib/python2.7/lib-tk
/Applications/Canopy.app/appdata/canopy-1.5.4.3105.macosx-x86_64/Canopy.app/Contents/lib/python2.7/lib-old
/Applications/Canopy.app/appdata/canopy-1.5.4.3105.macosx-x86_64/Canopy.app/Contents/lib/python2.7/lib-dynload
/Users/bob/Library/Enthought/Canopy_64bit/User/lib/python2.7/site-packages
/Users/bob/Library/Python/2.7/lib/python/site-packages
/Applications/Canopy.app/appdata/canopy-1.5.4.3105.macosx-x86_64/Canopy.app/Contents/lib/python2.7/site-packages
/Applications/Canopy.app/appdata/canopy-1.5.4.3105.macosx-x86_64/Canopy.app/Contents/lib/python2.7/site-packages/IPython/extensions
/Users/bob/.ipython

The location of rpy2 module is not present in the above block of file paths. So, it seemed logical to me to simply add its file path to the PYTHONPATH environmental variable.

However, an answer to this post (Using MacPorts to install modules via a certain path) mentions that it is potentially dangerous for the following command to be carried out:

 sys.path.insert(-1, '/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/rpy2-2.8.6-py2.7-macosx-10.6-intel.egg')

Can anybody support/gainsay this claim? Any additional explanation would be most helpful.

UPDATE

So, it turns out, it actually is BAD for the script. I decided to try running some examples from the tutorials, and the kernel just crashes every time, and must be restarted. How can I remedy this?

SECOND UPDATE

After following the advice set out in How to: Macports select python , I decided to run the following command in the terminal in order for MacPorts to select the version of Python that was being used by my Canopy IDE:

sudo port select --set python python27

This seems to have solved the issue, as my kernel does not crash anymore when I run basic commands from the rpy2 module. But I remain skeptical as to whether this is a long term solution or not, as I am still confused behind the discrepancies of installing a module via MacPorts versus the IDE's package manager.

Bob McBobson
  • 743
  • 1
  • 9
  • 29
  • What explanation in addition to "[If the] module contains compiled code it is not certain that the two installations are binary-compatible. In the best case, loading the module will fail if it isn't compatible, in the worst case, it will randomly crash." do you need? Compiled modules call functions in the Python library. This won't work if the functions it was compiled against are not those it is run against. If you want the module available for Canopy, you should compile it against the Python shipped with Canopy. MacPorts it not set up to do that – you'll have to compile it on your own. – neverpanic Jul 31 '17 at 06:18
  • @neverpanic I'm sorry, but this is a topic I really don't understand. How can I compile the module with the Python that shipped with Canopy? This was a module that, for various reasons, could only be installed via MacPorts in the terminal. – Bob McBobson Jul 31 '17 at 10:03
  • You'll have to compile the module against the headers and libraries shipped by Canopy (if they even ship headers at all). You will likely have to adjust include paths and library search paths of the rpy2 build system to achieve that. Since `rpy2` will also need the R headers and libraries you will likely have to add both Canopy Python's and MacPorts' include/library paths, in that order. I suggest you contact the developers of Canopy and ask for documentation on how to compile Python modules against their distribution of Python. – neverpanic Jul 31 '17 at 11:40
  • @neverpanic Ok, I'll try and contact them and see what I can do to get the package functioning properly in their IDE. – Bob McBobson Jul 31 '17 at 13:15

0 Answers0