Friday, June 13, 2014

Sublime Text 3, SublimeREPL, and Python modules on Mac OS X 10.7

Setting:
  • Mac OS X 10.7.5
  • Sublime Text 3
  • SublimeREPL 2.1.1
Problem:
  • I have globally-installed Python modules (e.g. Pylab, Matplotlib, Scipy) that I cannot import when using SublimeREPL: "no module named pylab".  I can import pylab from Python REPL from the Terminal just fine.
  • I tried adding the path to the module, but it complains with RuntimeError:
* I noticed this is the exact Python version referred to by the asker in this StackOverflow thread

Issue:

SublimeREPL seems to have some weird default configuration (honestly, I still don't quite understand why this is so) which makes it unable to point to my PYTHONPATH correctly. For example, I don't understand why in the screenshot above it said "module compiled against API version 6 but version of numpy is 4" while I have no problem importing matplotlib when using Python REPL in the Terminal. If anyone have a good explanation what that is so, that would be very much appreciated.

Solution:

Change the path to the Python executable.
  1. Open the SublimeREPL settings file for Python (it doesn't need to be done from Sublime Text, but it's convenient):
  2. Around line 22, add the "cmd" value with the desired Python executable. The path for my system is: "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/bin/python" (from: this SO answer)
  3. The file is located in: /Users//Library/Application Support/Sublime Text 3/Packages/SublimeREPL/config/Python/Main.sublime-menu
    (Remember, this is Mac OS X 10.7.5)



  4. This is weird; if I comment out line 22: "cmd": ["python", "-i", "-u"], SublimeREPL won't launch. Both lines must be used. Again, I apologize for not having an explanation why it has to be this way.
  5. Save the file.
  6. Try opening the SublimeREPL for Python
  7. Now I was able to import all the modules I need:


    Notice how it now says "Python 2.7.6".
Hope that helps.



Resources/Similar Issues:

3 comments:

  1. This is a very useful article that explains how to configure Sublime Text 3 and SublimeREPL to work correctly with Python modules on Mac OS X. The troubleshooting steps for resolving Python path issues, managing interpreter configurations, and importing third-party libraries such as NumPy, SciPy, and Matplotlib provide practical guidance for developers working in Python development environments. The article is especially valuable for learners who want to streamline their coding workflow and avoid environment-related issues.

    ReplyDelete
  2. Configuring development environments properly is an essential skill for building reliable Python applications and working with scientific computing libraries efficiently. These concepts are highly relevant for students and developers exploring Python Projects For Final Year, where programming, package management, and application development using Python frameworks are widely practiced.

    ReplyDelete
  3. Mastering Python tools, editors, and package ecosystems can significantly improve productivity and software development skills. Learning these technologies through structured programs such as Python Training Courses enables students to gain hands-on experience with Python programming, scientific libraries, and modern development environments used across various industries.

    ReplyDelete