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.

No comments:

Post a Comment