Wednesday, June 25, 2014

Using Virtualenv with SublimeREPL

Setup:

  • Sublime Text 3
  • SublimeREPL (2.1.1) 
  • Virtualenv (1.11.6)
  • Mac OS X 10.7.5
  • System Python (2.7.1) vs. Macports-installed Python (2.7.7)
Problem:
  1. I want the SublimeREPL: Python to use a virtual environment
  2. I want to use a different version of Python for my virtual environment
Note:
If you're a complete beginner at this, anything I wrote with ~$ means that's something done in Terminal.

Solution:

For problem #1: I want the SublimeREPL: Python to use a virtual environment
  • From the SublimeREPL docs, it says you *must* use some Virtualenv wrapper for SublimeREPL to recognize your virtual environments.
  • The docs suggested either virtualenvwrapper or venv.  I used venv, which works for me. I didn't try virtualenvwrapper.
  • venv was easy to use. Just download the venv.bash file and save it anywhere you like and run it.
    • For example, if you saved it in your home directory ~/, make sure the file is executable. If not, make it executable by: ~$ chmod +x venv.bash  To run it, do:

      ~$ source venv.bash
  • Now, you can create your virtual environment. Let's say we want to make the environment called 'venv0':

    ~$ venv create venv0

    Notice, it will also automatically activate the environment:

    (venv0) ~$_
  • The environment resides in:

    /Users/your_username/.venv/venv0

    *keep in mind I'm using OS X 10.7
  • To start using the myenv environment from SublimeREPL (assuming it's already installed. If not, follow the SublimeREPL link on top):
    • Launch Sublime Text 3
    • Do: CMD+SHIFT+p
    • Find the "SublimeREPL: Python - virtualenv" item from the list. (Shortcut: in the text field, start typing "virtualenv")
    • Select "SublimeREPL: Python - virtualenv" (click, or use arrow key and hit enter if it's highlighted)
    • Now venv0 ("/Users/your_username/.venv/venv0/bin/") should be showing in the view.
    • Select venv0, and SublimeREPL will launch with the environment activated.

For problem #2: I want to use a different version of Python for my virtual environment
  • Note: you cannot change your Python version if you already created your virtual environment. You will have to create a new environment, and reinstall all your module in the new environment.
  • I have the default system Python (2.7.1), and just recently installed a newer version of Python (2.7.7) using Macports.
  • The Macports' version of Python is installed in:

    /opt/local/bin/python2.7
  • To use this alternative Python in your virtual environment, use the argument -p [path_to_python] with Virtualenv. For example

    ~$ virtualenv -p /opt/local/bin/python2.7 myenv

    Don't worry - it doesn't mean that the environment will start using a 'global' Python; it will still use the Python located in myenv/bin, but now that Python is the same version as the one in the path.
  • To do this with venv, just modify the venv.bash file (using your text editor of choice). Find the line:

    virtualenv $@ --no-site-packages --distribute --prompt='$(__venv_prompt)'  "$VENV_DIR/$env"

    and change it to (emphasis added):

    virtualenv -p /opt/local/bin/python2.7 $@ --no-site-packages --distribute --prompt='$(__venv_prompt)'  "$VENV_DIR/$env"

    And save the file.
  • For the changes to take effect, re-run the venv.bash again.

    ~$ source venv.bash
  • Now, create another environment:

    ~$ venv create venv1
    (venv1) ~$_


    Run Python:

    (venv1) ~$ python
  • You should see now venv1 is using the alternative Python version (in this case: 2.7.7). If you use the other environment (venv0), you can check that it's using 2.7.1.
  • Finally, you can use venv1 from SublimeREPL as well.

Additional notes:
  • To quit/exit/deactivate the virtual environment from venv:

    (venv0) ~$ deactivate
  • To use/activate a virtual environment (e.g. venv0) using venv:

    ~$ venv use venv0
  • To delete a virtual environment (e.g. venv0) using venv:

    ~$ venv destroy venv0

CAVEAT:
The big 'gotcha' here is that Sublime/SublimeREPL has its own weird way with the environment path. If you're using a virtual environment, SublimeREPL have no problem accessing any Python modules installed within the virtual environment.

If you have a global module you want to use, or a custom module you just made, you would have to add the path manually. I.e. (in SublimeREPL):

>>>  import sys
>>>  sys.path.append('/your_path/to/the/module')

I have not found a way to set the path to the libraries outside of the virtual environment to be included when launching SublimeREPL. Please let me know if you do.

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: